resourceful 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. data/Manifest +24 -28
  2. data/Rakefile +44 -14
  3. data/lib/resourceful.rb +11 -21
  4. data/lib/resourceful/authentication_manager.rb +3 -2
  5. data/lib/resourceful/cache_manager.rb +58 -1
  6. data/lib/resourceful/exceptions.rb +34 -0
  7. data/lib/resourceful/header.rb +95 -0
  8. data/lib/resourceful/http_accessor.rb +0 -2
  9. data/lib/resourceful/memcache_cache_manager.rb +3 -13
  10. data/lib/resourceful/net_http_adapter.rb +15 -5
  11. data/lib/resourceful/request.rb +180 -18
  12. data/lib/resourceful/resource.rb +38 -141
  13. data/lib/resourceful/response.rb +142 -95
  14. data/resourceful.gemspec +9 -7
  15. data/spec/acceptance/authorization_spec.rb +16 -0
  16. data/spec/acceptance/caching_spec.rb +192 -0
  17. data/spec/acceptance/header_spec.rb +24 -0
  18. data/spec/acceptance/redirecting_spec.rb +12 -0
  19. data/spec/acceptance/resource_spec.rb +84 -0
  20. data/spec/acceptance_shared_specs.rb +12 -17
  21. data/spec/{acceptance_spec.rb → old_acceptance_specs.rb} +27 -57
  22. data/spec/simple_sinatra_server.rb +74 -0
  23. data/spec/simple_sinatra_server_spec.rb +98 -0
  24. data/spec/spec_helper.rb +21 -7
  25. metadata +50 -42
  26. data/spec/resourceful/authentication_manager_spec.rb +0 -249
  27. data/spec/resourceful/cache_manager_spec.rb +0 -223
  28. data/spec/resourceful/header_spec.rb +0 -38
  29. data/spec/resourceful/http_accessor_spec.rb +0 -164
  30. data/spec/resourceful/memcache_cache_manager_spec.rb +0 -111
  31. data/spec/resourceful/net_http_adapter_spec.rb +0 -96
  32. data/spec/resourceful/options_interpreter_spec.rb +0 -102
  33. data/spec/resourceful/request_spec.rb +0 -186
  34. data/spec/resourceful/resource_spec.rb +0 -600
  35. data/spec/resourceful/response_spec.rb +0 -238
  36. data/spec/resourceful/stubbed_resource_proxy_spec.rb +0 -58
  37. data/spec/simple_http_server_shared_spec.rb +0 -162
  38. data/spec/simple_http_server_shared_spec_spec.rb +0 -212
@@ -1,38 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname + '../spec_helper'
3
-
4
- require 'resourceful/header'
5
-
6
- describe Resourceful::Header do
7
-
8
- it "should capitalize on all accesses" do
9
- h = Resourceful::Header.new("foo" => "bar")
10
- h["foo"].should == "bar"
11
- h["Foo"].should == "bar"
12
- h["FOO"].should == "bar"
13
-
14
- h.to_hash.should == {"Foo" => "bar"}
15
-
16
- h["bar-zzle"] = "quux"
17
-
18
- h.to_hash.should == {"Foo" => "bar", "Bar-Zzle" => "quux"}
19
- end
20
-
21
- it "should capitalize correctly" do
22
- h = Resourceful::Header.new
23
-
24
- h.capitalize("foo").should == "Foo"
25
- h.capitalize("foo-bar").should == "Foo-Bar"
26
- h.capitalize("foo_bar").should == "Foo-Bar"
27
- h.capitalize("foo bar").should == "Foo Bar"
28
- h.capitalize("foo-bar-quux").should == "Foo-Bar-Quux"
29
- h.capitalize("foo-bar-2quux").should == "Foo-Bar-2quux"
30
- end
31
-
32
- it "should be converted to real Hash" do
33
- h = Resourceful::Header.new("foo" => "bar")
34
- h.to_hash.should be_instance_of(Hash)
35
- end
36
-
37
- end
38
-
@@ -1,164 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname + '../spec_helper'
3
-
4
- require 'resourceful/http_accessor'
5
-
6
- describe Resourceful::HttpAccessor, 'init' do
7
-
8
- it 'should be instantiatable' do
9
- Resourceful::HttpAccessor.new().should be_instance_of(Resourceful::HttpAccessor)
10
- end
11
-
12
- it 'should accept logger to new' do
13
- ha = Resourceful::HttpAccessor.new(:logger => (l = stub('logger')))
14
-
15
- ha.logger.should == l
16
- end
17
-
18
- it 'should provide logger object even when no logger is specified' do
19
- ha = Resourceful::HttpAccessor.new()
20
-
21
- ha.logger.should be_instance_of(Resourceful::BitBucketLogger)
22
- end
23
-
24
- it 'should raise arg error if unrecognized options are passed' do
25
- lambda {
26
- Resourceful::HttpAccessor.new(:foo => 'foo', :bar => 'bar')
27
- }.should raise_error(ArgumentError, /Unrecognized options: (foo, bar)|(bar, foo)/)
28
- end
29
-
30
- it 'should allow an additional user agent token to be passed at init' do
31
- ha = Resourceful::HttpAccessor.new(:user_agent => "Super/3000")
32
-
33
- ha.user_agent_string.should match(%r{^Super/3000})
34
- end
35
-
36
- it 'should allow multiple additional user agent tokens to be passed at init' do
37
- ha = Resourceful::HttpAccessor.new(:user_agent => ["Super/3000", "Duper/2.1"])
38
-
39
- ha.user_agent_string.should match(%r{^Super/3000 Duper/2\.1 })
40
- end
41
-
42
- it 'should allow cache manager to be provided at init' do
43
- ha = Resourceful::HttpAccessor.new(:cache_manager => :marker)
44
-
45
- ha.cache_manager.should equal(:marker)
46
- end
47
-
48
- end
49
-
50
- describe Resourceful::HttpAccessor do
51
- before do
52
- @logger = stub('logger')
53
- @accessor = Resourceful::HttpAccessor.new(:logger => @logger)
54
- @auth_manager = mock('authentication_manager')
55
- Resourceful::AuthenticationManager.stub!(:new).and_return(@auth_manager)
56
-
57
- @resource = mock('resource')
58
- Resourceful::Resource.stub!(:new).and_return(@resource)
59
- end
60
-
61
- it 'should have user agent string w/ just resourceful token by default' do
62
- @accessor.user_agent_string.should == "Resourceful/#{RESOURCEFUL_VERSION}(Ruby/#{RUBY_VERSION})"
63
- end
64
-
65
- it 'should add additional user agent tokens to beginning of user agent string' do
66
- @accessor.user_agent_tokens << 'FooBar/3000(special-version)'
67
-
68
- @accessor.user_agent_string.should match(%r{^FooBar\/3000\(special-version\) Resourceful/})
69
- end
70
-
71
- it 'should allow a logger to be specified' do
72
- l = stub('logger')
73
-
74
- @accessor.logger = l
75
- @accessor.logger.should == l
76
- end
77
-
78
- it 'should allow a logger to be removed' do
79
- l = stub('logger')
80
-
81
- @accessor.logger = l
82
- @accessor.logger = nil
83
- @accessor.logger.should be_nil
84
- end
85
-
86
- it 'should be able to return a particular resource (#[])' do
87
- @accessor['http://www.example/'].should == @resource
88
- end
89
-
90
- it 'should create resource if it does not already exist (#[])' do
91
- Resourceful::Resource.should_receive(:new).and_return(stub('resource'))
92
- @accessor['http://www.example/previously-unused-uri']
93
- end
94
-
95
- it 'should pass uri to resource upon creation (#[])' do
96
- Resourceful::Resource.should_receive(:new).with(anything, 'http://www.example/previously-unused-uri', anything).
97
- and_return(stub('resource'))
98
- @accessor['http://www.example/previously-unused-uri']
99
- end
100
-
101
- it 'should pass owning accessor to resource upon creation (#[])' do
102
- Resourceful::Resource.should_receive(:new).with(@accessor, anything, anything).and_return(stub('resource'))
103
- @accessor['http://www.example/previously-unused-uri']
104
- end
105
-
106
- it 'should be able to return a particular resource (#resource)' do
107
- @accessor.resource('http://www.example/').should == @resource
108
- end
109
-
110
- it 'should create resource if it does not already exist (#resource)' do
111
- Resourceful::Resource.should_receive(:new).and_return(stub('resource'))
112
- @accessor.resource('http://www.example/previously-unused-uri')
113
- end
114
-
115
- it 'should pass owning accessor to resource upon creation (#[])' do
116
- Resourceful::Resource.should_receive(:new).with(@accessor, anything, anything).and_return(stub('resource'))
117
- @accessor.resource('http://www.example/previously-unused-uri')
118
- end
119
-
120
- it 'should pass uri to resource upon creation (#resource)' do
121
- Resourceful::Resource.should_receive(:new).with(anything, 'http://www.example/previously-unused-uri', anything).
122
- and_return(stub('resource'))
123
- @accessor.resource('http://www.example/previously-unused-uri')
124
- end
125
-
126
- it 'should pass additional options to resource upon creation' do
127
- Resourceful::Resource.should_receive(:new).with(anything, anything, :foo => :bar).and_return(stub('resource'))
128
- @accessor.resource('http://example.com/', :foo => :bar)
129
- end
130
-
131
- end
132
-
133
- describe Resourceful::HttpAccessor, "(authentication)" do
134
- before do
135
- @auth_manager = stub('auth_manager', :add_auth_handler => nil)
136
- Resourceful::AuthenticationManager.stub!(:new).and_return(@auth_manager)
137
- @ha = Resourceful::HttpAccessor.new()
138
- end
139
-
140
- it 'should allow authenticators to be registered' do
141
- an_authenticator = stub('an_authenicator')
142
- @auth_manager.should_receive(:add_auth_handler).with(an_authenticator)
143
-
144
- @ha.add_authenticator(an_authenticator)
145
- end
146
-
147
- it 'should allow an authenicator to be specified at init' do
148
- an_authenticator = stub('an_authenicator')
149
- @auth_manager.should_receive(:add_auth_handler).with(an_authenticator)
150
-
151
- Resourceful::HttpAccessor.new(:authenticator => an_authenticator)
152
- end
153
-
154
- it 'should allow multiple authenicators to be specified at init' do
155
- authenticator1 = stub('authenicator1')
156
- authenticator2 = stub('authenicator2')
157
-
158
- @auth_manager.should_receive(:add_auth_handler).with(authenticator1)
159
- @auth_manager.should_receive(:add_auth_handler).with(authenticator2)
160
-
161
- Resourceful::HttpAccessor.new(:authenticators => [authenticator1, authenticator2])
162
- end
163
-
164
- end
@@ -1,111 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname + '../spec_helper'
3
-
4
- require 'resourceful/memcache_cache_manager'
5
- require 'resourceful/request'
6
-
7
- describe Resourceful::MemcacheCacheManager do
8
- before do
9
- @resource = stub('resource', :uri => 'http://foo.invalid/')
10
-
11
- @request = Resourceful::Request.new(:get, @resource)
12
- @response = Resourceful::Response.new('http://foo.invalid/', '200', {'Vary' => 'Accept'}, "a body")
13
-
14
- @memcache = stub('memcache', :get => nil)
15
- MemCache.stub!(:new).and_return(@memcache)
16
-
17
- @cache_mngr = Resourceful::MemcacheCacheManager.new('foobar:42')
18
- end
19
-
20
- describe "#store(request,response)" do
21
- it "should store the new pair in memcache" do
22
- @memcache.should_receive(:[]=).with do |key, collection|
23
- key.should == Digest::MD5.hexdigest('http://foo.invalid/')
24
- collection[@request].should == @response
25
- end
26
-
27
- @cache_mngr.store(@request, @response)
28
- end
29
-
30
- it "should replace existing values if they exist" do
31
- entries = Resourceful::CacheEntryCollection.new
32
- entries[@request] = @response
33
- @memcache.stub!(:[]=).and_return(entries)
34
-
35
- new_request = Resourceful::Request.new(:get, @resource)
36
- new_response = Resourceful::Response.new('http://foo.invalid/', '200', {}, "a different body")
37
-
38
- @memcache.should_receive(:[]=).with do |key, collection|
39
- collection[new_request].should == new_response
40
- end
41
-
42
- @cache_mngr.store(new_request, new_response)
43
- end
44
-
45
- it "should not store responses that are not cacheable" do
46
- @memcache.should_not_receive(:[]=)
47
-
48
- vary_star_response = Resourceful::Response.new('http://foo.invalid/', '200', {'Vary' => '*'}, "a different body")
49
-
50
- @cache_mngr.store(@request, vary_star_response)
51
- end
52
- end
53
-
54
- describe "#lookup" do
55
- before do
56
- @entries = Resourceful::CacheEntryCollection.new
57
- @entries[@request] = @response
58
- @memcache.stub!(:get).and_return(@entries)
59
- end
60
-
61
- it "should lookup the entry collection by the URI" do
62
- @memcache.should_receive(:get).with(Digest::MD5.hexdigest('http://foo.invalid/')).and_return(@entries)
63
-
64
- @cache_mngr.lookup(@request)
65
- end
66
-
67
- it "should retrieve responses that match request" do
68
- @cache_mngr.lookup(Resourceful::Request.new(:get, @resource)).should == @response
69
- end
70
-
71
- it "should return nil if no responses that match request are found" do
72
- @cache_mngr.lookup(Resourceful::Request.new(:get, @resource, "body", {'Accept' => 'text/plain'})).
73
- should be_nil
74
- end
75
-
76
- it "should return nil if no responses that resource are found" do
77
- @memcache.stub!(:get).and_return(nil)
78
-
79
- @cache_mngr.lookup(Resourceful::Request.new(:get, @resource)).should be_nil
80
- end
81
- end
82
-
83
- describe "#invalidate(url)" do
84
- it "should remove all cached responses for that resource from memcache" do
85
- @memcache.should_receive(:delete).with(Digest::MD5.hexdigest('http://foo.invalid/'))
86
-
87
- @cache_mngr.invalidate('http://foo.invalid/')
88
- end
89
- end
90
- end
91
-
92
- describe Resourceful::MemcacheCacheManager, 'init' do
93
- it 'should be createable with single memcache server' do
94
- MemCache.should_receive(:new).with(['foobar:42'], anything)
95
-
96
- Resourceful::MemcacheCacheManager.new('foobar:42')
97
- end
98
-
99
- it 'should be createable with multiple memcache servers' do
100
- MemCache.should_receive(:new).with(['foobar:42', 'baz:32'], anything)
101
-
102
- Resourceful::MemcacheCacheManager.new('foobar:42', 'baz:32')
103
- end
104
-
105
- it 'should create a thread safe memcache client' do
106
- MemCache.should_receive(:new).with(anything, {:multithread => true})
107
-
108
- Resourceful::MemcacheCacheManager.new('foobar:42')
109
- end
110
- end
111
-
@@ -1,96 +0,0 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname + '../spec_helper'
3
-
4
- require 'resourceful/net_http_adapter'
5
-
6
- describe Resourceful::NetHttpAdapter do
7
- describe '#make_request (mocked)' do
8
- it 'should enable ssl on the connection' do
9
- resp = stub('http_response', :code => 200, :header => {}, :body => "hello")
10
- conn = stub('http_conn', :request => resp, :finish => nil)
11
- Net::HTTP.should_receive(:new).and_return(conn)
12
- conn.should_receive(:use_ssl=).with(true).ordered
13
- conn.should_receive(:start).ordered
14
-
15
- Resourceful::NetHttpAdapter.make_request(:get, 'https://localhost:3000/get')
16
- end
17
- end
18
- end
19
-
20
- describe Resourceful::NetHttpAdapter do
21
- it_should_behave_like 'simple http server'
22
-
23
-
24
- describe '#make_request' do
25
- before do
26
- @response = Resourceful::NetHttpAdapter.make_request(:get, 'http://localhost:3000/get')
27
- end
28
-
29
- describe 'response' do
30
- it 'should be an array' do
31
- @response.should be_instance_of(Array)
32
- end
33
-
34
- it 'should have the numeric response code as the first element' do
35
- code = @response[0]
36
- code.should be_instance_of(Fixnum)
37
- code.should == 200
38
- end
39
-
40
- it 'should have the Header as the second element' do
41
- header = @response[1]
42
- header.should be_instance_of(Resourceful::Header)
43
- header['content-type'].should == ['text/plain']
44
- end
45
-
46
- it 'should have the body as the third and last element' do
47
- body = @response[2]
48
- body.should == "Hello, world!"
49
- end
50
-
51
- end
52
-
53
- end
54
-
55
- describe '#net_http_request_class' do
56
-
57
- it 'should provide Net::HTTP::Get for a get method' do
58
- Resourceful::NetHttpAdapter.send(:net_http_request_class, :get).should == Net::HTTP::Get
59
- end
60
-
61
- it 'should provide Net::HTTP::Post for a post method' do
62
- Resourceful::NetHttpAdapter.send(:net_http_request_class, :post).should == Net::HTTP::Post
63
- end
64
-
65
- it 'should provide Net::HTTP::Put for a put method' do
66
- Resourceful::NetHttpAdapter.send(:net_http_request_class, :put).should == Net::HTTP::Put
67
- end
68
-
69
- it 'should provide Net::HTTP::Delete for a delete method' do
70
- Resourceful::NetHttpAdapter.send(:net_http_request_class, :delete).should == Net::HTTP::Delete
71
- end
72
-
73
- end
74
-
75
- end
76
-
77
- describe Addressable::URI, '#absolute_path monkey patch' do
78
-
79
- it 'should have the path and any query parameters' do
80
- uri = Addressable::URI.parse('http://localhost/foo?bar=baz')
81
- uri.absolute_path.should == '/foo?bar=baz'
82
- end
83
-
84
- it 'should not have a ? if there are no query params' do
85
- uri = Addressable::URI.parse('http://localhost/foo')
86
- uri.absolute_path.should_not =~ /\?/
87
- uri.absolute_path.should == '/foo'
88
- end
89
-
90
- it 'should not add the query parameter twice' do
91
- uri = Addressable::URI.parse('http://localhost/foo?bar=baz')
92
- uri.absolute_path.should == '/foo?bar=baz'
93
- uri.absolute_path.should == '/foo?bar=baz'
94
- end
95
-
96
- end
@@ -1,102 +0,0 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
2
- require 'resourceful/options_interpreter'
3
-
4
- describe Resourceful::OptionsInterpreter, '#initialize' do
5
- it 'should be creatable block' do
6
- Resourceful::OptionsInterpreter.new() {}
7
- end
8
- end
9
-
10
-
11
- describe Resourceful::OptionsInterpreter, "#option()" do
12
- before do
13
- @interpreter = Resourceful::OptionsInterpreter.new()
14
- end
15
-
16
- it 'should take option name' do
17
- @interpreter.option(:test)
18
- @interpreter.supported_options.should include(:test)
19
- end
20
-
21
- it 'should take interpretation block' do
22
- @interpreter.option(:test) {"this"}
23
- @interpreter.supported_options.should include(:test)
24
- end
25
- end
26
-
27
- describe Resourceful::OptionsInterpreter, '#interpret(options)' do
28
- before do
29
- @interpreter = Resourceful::OptionsInterpreter.new()
30
- @interpreter.option(:foo)
31
- end
32
-
33
- it 'should return hash like structure of interpreted options' do
34
- opts = @interpreter.interpret(:foo => 'bar')
35
-
36
- opts.should have_key(:foo)
37
- opts[:foo].should == 'bar'
38
- end
39
-
40
- it 'should raise argument error if there is an unsupported option in src hash' do
41
- lambda {
42
- @interpreter.interpret(:bar => 'baz')
43
- }.should raise_error(ArgumentError, "Unrecognized options: bar")
44
- end
45
-
46
- it 'should list all unsupported options in the exception' do
47
- lambda {
48
- @interpreter.interpret(:bar => 'baz', :baz => 'bar')
49
- }.should raise_error(ArgumentError, /Unrecognized options: (bar, baz)|(baz, bar)/)
50
- end
51
-
52
- it 'should execute pass the options though the appropriate handling block' do
53
- @interpreter.option(:foo) {|foo| foo + " hello"}
54
-
55
- @interpreter.interpret(:foo => 'bar')[:foo].should == 'bar hello'
56
- end
57
-
58
- it 'should not include options that were not passed in resulting hash' do
59
- @interpreter = Resourceful::OptionsInterpreter.new()
60
- @interpreter.option(:foo)
61
-
62
- @interpreter.interpret({}).keys.should_not include(:foo)
63
- end
64
-
65
- it 'should not invoked option value munging block if option is not specified' do
66
- @interpreter = Resourceful::OptionsInterpreter.new()
67
- munging_block_executed = false
68
- @interpreter.option(:foo) { |a| munging_block_executed = true }
69
-
70
- lambda {
71
- @interpreter.interpret({})
72
- }.should_not change{munging_block_executed}
73
- end
74
-
75
- it 'should use default if option is not specified' do
76
- @interpreter = Resourceful::OptionsInterpreter.new()
77
- @interpreter.option(:foo, :default => 'hello')
78
-
79
- opts = @interpreter.interpret({})
80
- opts.should have_key(:foo)
81
- opts[:foo].should == 'hello'
82
- end
83
-
84
- it 'should use default value if option is specified as nil' do
85
- @interpreter = Resourceful::OptionsInterpreter.new()
86
- @interpreter.option(:foo, :default => 'hello')
87
-
88
- opts = @interpreter.interpret({:foo => nil})
89
- opts.should have_key(:foo)
90
- opts[:foo].should == 'hello'
91
- end
92
-
93
- it 'should not use default if option is specified ' do
94
- @interpreter = Resourceful::OptionsInterpreter.new()
95
- @interpreter.option(:foo, :default => 'hello')
96
-
97
- opts = @interpreter.interpret({:foo => 'bye'})
98
- opts.should have_key(:foo)
99
- opts[:foo].should == 'bye'
100
- end
101
-
102
- end