paul-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.
- data/Manifest +30 -0
- data/Rakefile +44 -14
- data/lib/resourceful.rb +11 -21
- data/lib/resourceful/authentication_manager.rb +3 -2
- data/lib/resourceful/cache_manager.rb +58 -1
- data/lib/resourceful/exceptions.rb +34 -0
- data/lib/resourceful/header.rb +95 -0
- data/lib/resourceful/http_accessor.rb +0 -2
- data/lib/resourceful/memcache_cache_manager.rb +3 -13
- data/lib/resourceful/net_http_adapter.rb +15 -5
- data/lib/resourceful/request.rb +180 -18
- data/lib/resourceful/resource.rb +38 -141
- data/lib/resourceful/response.rb +142 -95
- data/resourceful.gemspec +9 -7
- data/spec/acceptance/authorization_spec.rb +16 -0
- data/spec/acceptance/caching_spec.rb +191 -0
- data/spec/acceptance/header_spec.rb +24 -0
- data/spec/acceptance/redirecting_spec.rb +12 -0
- data/spec/acceptance/resource_spec.rb +84 -0
- data/spec/acceptance_shared_specs.rb +12 -17
- data/spec/{acceptance_spec.rb → old_acceptance_specs.rb} +27 -57
- data/spec/simple_sinatra_server.rb +73 -0
- data/spec/simple_sinatra_server_spec.rb +98 -0
- data/spec/spec_helper.rb +21 -7
- metadata +55 -42
- data/spec/resourceful/authentication_manager_spec.rb +0 -249
- data/spec/resourceful/cache_manager_spec.rb +0 -223
- data/spec/resourceful/header_spec.rb +0 -38
- data/spec/resourceful/http_accessor_spec.rb +0 -164
- data/spec/resourceful/memcache_cache_manager_spec.rb +0 -111
- data/spec/resourceful/net_http_adapter_spec.rb +0 -96
- data/spec/resourceful/options_interpreter_spec.rb +0 -102
- data/spec/resourceful/request_spec.rb +0 -186
- data/spec/resourceful/resource_spec.rb +0 -600
- data/spec/resourceful/response_spec.rb +0 -238
- data/spec/resourceful/stubbed_resource_proxy_spec.rb +0 -58
- data/spec/simple_http_server_shared_spec.rb +0 -162
- data/spec/simple_http_server_shared_spec_spec.rb +0 -212
@@ -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
|
@@ -1,186 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require Pathname(__FILE__).dirname + '../spec_helper'
|
3
|
-
require 'rubygems'
|
4
|
-
require 'addressable/uri'
|
5
|
-
|
6
|
-
describe Resourceful::Request do
|
7
|
-
before do
|
8
|
-
@uri = Addressable::URI.parse('http://www.example.com')
|
9
|
-
@resource = mock('resource', :logger => Resourceful::BitBucketLogger.new)
|
10
|
-
@resource.stub!(:uri).and_return(@uri)
|
11
|
-
|
12
|
-
@request = Resourceful::Request.new(:get, @resource)
|
13
|
-
|
14
|
-
@cachemgr = mock('cache_mgr')
|
15
|
-
@cachemgr.stub!(:lookup).and_return(nil)
|
16
|
-
@cachemgr.stub!(:store)
|
17
|
-
@resource.stub!(:accessor).and_return(mock('accessor', :cache_manager => @cachemgr, :logger => Resourceful::BitBucketLogger.new))
|
18
|
-
end
|
19
|
-
|
20
|
-
describe 'init' do
|
21
|
-
|
22
|
-
it 'should be instantiatable' do
|
23
|
-
@request.should be_instance_of(Resourceful::Request)
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'should take an http method' do
|
27
|
-
@request.method.should == :get
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'should take a resource' do
|
31
|
-
@request.resource.should == @resource
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should take an optional body' do
|
35
|
-
req = Resourceful::Request.new(:get, @resource)
|
36
|
-
req.body.should be_nil
|
37
|
-
|
38
|
-
req = Resourceful::Request.new(:post, @resource, 'Hello from post!')
|
39
|
-
req.body.should == 'Hello from post!'
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should have a request_time' do
|
43
|
-
@request.should respond_to(:request_time)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
describe '#response' do
|
49
|
-
before do
|
50
|
-
@net_http_adapter_response = mock('net_http_adapter_response')
|
51
|
-
Resourceful::NetHttpAdapter.stub!(:make_request).and_return(@net_http_adapter_response)
|
52
|
-
|
53
|
-
@response = mock('response', :code => 200, :authoritative= => true, :was_unsuccessful? => false, :request_time= => nil)
|
54
|
-
Resourceful::Response.stub!(:new).and_return(@response)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'should be a method' do
|
58
|
-
@request.should respond_to(:response)
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'should return the Response object' do
|
62
|
-
@request.response.should == @response
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should set the request_time to now' do
|
66
|
-
now = Time.now
|
67
|
-
Time.stub!(:now).and_return(now)
|
68
|
-
|
69
|
-
@request.response
|
70
|
-
@request.request_time.should == now
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should set the response\'s request time' do
|
74
|
-
now = Time.now
|
75
|
-
Time.stub!(:now).and_return(now)
|
76
|
-
|
77
|
-
@response.should_receive(:request_time=).with(now)
|
78
|
-
@request.response
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe '#should_be_redirected?' do
|
83
|
-
before do
|
84
|
-
@net_http_adapter_response = mock('net_http_adapter_response')
|
85
|
-
Resourceful::NetHttpAdapter.stub!(:make_request).and_return(@net_http_adapter_response)
|
86
|
-
|
87
|
-
@response = mock('response', :code => 200, :authoritative= => true, :was_unsuccessful? => false, :request_time= => nil)
|
88
|
-
Resourceful::Response.stub!(:new).and_return(@response)
|
89
|
-
end
|
90
|
-
|
91
|
-
describe 'with no callback set' do
|
92
|
-
before do
|
93
|
-
@callback = nil
|
94
|
-
@resource.stub!(:on_redirect).and_return(@callback)
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'should be true for GET' do
|
98
|
-
request = Resourceful::Request.new(:get, @resource, @post_data)
|
99
|
-
|
100
|
-
request.should_be_redirected?.should be_true
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'should be false for POST, etc' do
|
104
|
-
request = Resourceful::Request.new(:post, @resource, @post_data)
|
105
|
-
|
106
|
-
request.should_be_redirected?.should be_false
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'should be true when callback returns true' do
|
112
|
-
@callback = lambda { true }
|
113
|
-
@resource.stub!(:on_redirect).and_return(@callback)
|
114
|
-
request = Resourceful::Request.new(:get, @resource, @post_data)
|
115
|
-
|
116
|
-
request.should_be_redirected?.should be_true
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'should be false when callback returns false' do
|
120
|
-
@callback = lambda { false }
|
121
|
-
@resource.stub!(:on_redirect).and_return(@callback)
|
122
|
-
request = Resourceful::Request.new(:get, @resource, @post_data)
|
123
|
-
|
124
|
-
request.should_be_redirected?.should be_false
|
125
|
-
end
|
126
|
-
|
127
|
-
end
|
128
|
-
|
129
|
-
describe "content coding" do
|
130
|
-
it "should set Accept-Encoding automatically" do
|
131
|
-
@request.header['Accept-Encoding'].should == 'gzip, identity'
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
describe '#set_validation_headers' do
|
136
|
-
before do
|
137
|
-
@cached_response = mock('cached_response')
|
138
|
-
|
139
|
-
@cached_response_header = mock('header', :[] => nil, :has_key? => false)
|
140
|
-
@cached_response.stub!(:header).and_return(@cached_response_header)
|
141
|
-
|
142
|
-
@cachemgr.stub!(:lookup).and_return(@cached_response)
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'should have an #set_validation_headers method' do
|
146
|
-
@request.should respond_to(:set_validation_headers)
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'should set If-None-Match to the cached response\'s ETag' do
|
150
|
-
@cached_response_header.should_receive(:[]).with('ETag').and_return('some etag')
|
151
|
-
@cached_response_header.should_receive(:has_key?).with('ETag').and_return(true)
|
152
|
-
@request.set_validation_headers(@cached_response)
|
153
|
-
|
154
|
-
@request.header['If-None-Match'].should == 'some etag'
|
155
|
-
end
|
156
|
-
|
157
|
-
it 'should not set If-None-Match if the cached response does not have an ETag' do
|
158
|
-
@request.set_validation_headers(@cached_response)
|
159
|
-
@request.header.should_not have_key('If-None-Match')
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'should set If-Modified-Since to the cached response\'s Last-Modified' do
|
163
|
-
@cached_response_header.should_receive(:[]).with('Last-Modified').and_return('some date')
|
164
|
-
@cached_response_header.should_receive(:has_key?).with('Last-Modified').and_return(true)
|
165
|
-
@request.set_validation_headers(@cached_response)
|
166
|
-
|
167
|
-
@request.header['If-Modified-Since'].should == 'some date'
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'should not set If-Modified-Since if the cached response does not have Last-Modified' do
|
171
|
-
@request.set_validation_headers(@cached_response)
|
172
|
-
@request.header.should_not have_key('If-Modified-Since')
|
173
|
-
end
|
174
|
-
|
175
|
-
it 'should add "Cache-Control: max-age=0" to the request when revalidating a response that has "Cache-Control: must-revalidate" set' do
|
176
|
-
@cached_response_header.should_receive(:[]).with('Cache-Control').and_return(['must-revalidate'])
|
177
|
-
@cached_response_header.should_receive(:has_key?).with('Cache-Control').and_return(true)
|
178
|
-
@request.set_validation_headers(@cached_response)
|
179
|
-
|
180
|
-
@request.header['Cache-Control'].should include('max-age=0')
|
181
|
-
end
|
182
|
-
|
183
|
-
end
|
184
|
-
|
185
|
-
end
|
186
|
-
|
@@ -1,600 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require Pathname(__FILE__).dirname + '../spec_helper'
|
3
|
-
|
4
|
-
require 'resourceful/resource'
|
5
|
-
|
6
|
-
describe Resourceful::Resource do
|
7
|
-
before do
|
8
|
-
@auth_manager = mock('auth_manager', :add_credentials => nil)
|
9
|
-
@cache_manager = mock('cache_manager', :lookup => nil, :store => nil, :invalidate => nil)
|
10
|
-
@logger = mock('logger', :debug => nil, :info => nil)
|
11
|
-
@accessor = mock('accessor', :auth_manager => @auth_manager,
|
12
|
-
:cache_manager => @cache_manager,
|
13
|
-
:logger => @logger)
|
14
|
-
|
15
|
-
@uri = 'http://www.example.com/'
|
16
|
-
@resource = Resourceful::Resource.new(@accessor, @uri)
|
17
|
-
|
18
|
-
@response = mock('response', :code => 200,
|
19
|
-
:is_redirect? => false,
|
20
|
-
:is_not_authorized? => false,
|
21
|
-
:is_success? => true,
|
22
|
-
:is_not_modified? => false)
|
23
|
-
|
24
|
-
@request = mock('request', :response => @response, :should_be_redirected? => true, :uri => @uri)
|
25
|
-
Resourceful::Request.stub!(:new).and_return(@request)
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'init' do
|
29
|
-
it 'should be instantiatable' do
|
30
|
-
@resource.should be_instance_of(Resourceful::Resource)
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should take an http_accessor' do
|
34
|
-
@resource.accessor.should == @accessor
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should take a uri' do
|
38
|
-
@resource.uri.should == @uri
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should take some default_options' do
|
42
|
-
r = Resourceful::Resource.new(@accessor, @uri, :foo => :bar)
|
43
|
-
r.default_options.should == {:foo => :bar}
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should default to an empty hash for options' do
|
47
|
-
@resource.default_options.should == {}
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#effective_uri' do
|
52
|
-
|
53
|
-
it 'should be the latest uri' do
|
54
|
-
@resource.effective_uri.should == @uri
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'should be aliased as #uri' do
|
58
|
-
@resource.uri.should == @resource.effective_uri
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
describe '#do_read_request' do
|
64
|
-
|
65
|
-
def make_request
|
66
|
-
@resource.do_read_request(:some_method)
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should make a new request object from the method' do
|
70
|
-
Resourceful::Request.should_receive(:new).with(:some_method, @resource, nil, {}).and_return(@request)
|
71
|
-
make_request
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'should set the header of the request from the header arg' do
|
75
|
-
Resourceful::Request.should_receive(:new).with(:some_method, @resource, nil, :foo => :bar).and_return(@request)
|
76
|
-
@resource.do_read_request(:some_method, :foo => :bar)
|
77
|
-
end
|
78
|
-
|
79
|
-
describe 'default options' do
|
80
|
-
before do
|
81
|
-
@resource.default_options = {:foo => :bar}
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'should merge the header with the default options' do
|
85
|
-
Resourceful::Request.should_receive(:new).with(anything, anything, anything, :foo => :bar).and_return(@request)
|
86
|
-
make_request
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'should override any default header with the request header' do
|
90
|
-
Resourceful::Request.should_receive(:new).with(anything, anything, anything, :foo => :baz).and_return(@request)
|
91
|
-
@resource.do_read_request(:some_method, :foo => :baz)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
describe 'non-success responses' do
|
96
|
-
before do
|
97
|
-
@uri = 'http://www.example.com/code/404'
|
98
|
-
@resource = Resourceful::Resource.new(@accessor, @uri)
|
99
|
-
|
100
|
-
@redirected_uri = 'http://www.example.com/get'
|
101
|
-
@redirect_response = mock('redirect_response',
|
102
|
-
:header => {'Location' => [@redirected_uri]},
|
103
|
-
:is_redirect? => false,
|
104
|
-
:is_success? => false,
|
105
|
-
:is_not_authorized? => false,
|
106
|
-
:is_not_modified? => false,
|
107
|
-
:code => 404)
|
108
|
-
|
109
|
-
@request.stub!(:response).and_return(@redirect_response, @response)
|
110
|
-
@request.stub!(:method).and_return(:get)
|
111
|
-
@request.stub!(:uri).and_return('http://www.example.com/code/404')
|
112
|
-
end
|
113
|
-
|
114
|
-
it 'should raise UnsuccessfulHttpRequestError' do
|
115
|
-
lambda {
|
116
|
-
@resource.do_read_request(:get)
|
117
|
-
}.should raise_error(Resourceful::UnsuccessfulHttpRequestError)
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'should give a reasonable error message' do
|
121
|
-
lambda {
|
122
|
-
@resource.do_read_request(:get)
|
123
|
-
}.should raise_error("get request to <http://www.example.com/code/404> failed with code 404")
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe 'with redirection' do
|
128
|
-
before do
|
129
|
-
@uri = 'http://www.example.com/redirect/301?http://www.example.com/get'
|
130
|
-
@resource = Resourceful::Resource.new(@accessor, @uri)
|
131
|
-
|
132
|
-
@redirected_uri = 'http://www.example.com/get'
|
133
|
-
@redirect_response = mock('redirect_response',
|
134
|
-
:header => {'Location' => [@redirected_uri]},
|
135
|
-
:is_redirect? => true,
|
136
|
-
:is_permanent_redirect? => true,
|
137
|
-
:is_not_modified? => false)
|
138
|
-
|
139
|
-
@request.stub!(:response).and_return(@redirect_response, @response)
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'should check if the response was a redirect' do
|
144
|
-
@redirect_response.should_receive(:is_redirect?).and_return(true)
|
145
|
-
make_request
|
146
|
-
end
|
147
|
-
|
148
|
-
it 'should check if the request should be redirected' do
|
149
|
-
@request.should_receive(:should_be_redirected?).and_return(true)
|
150
|
-
make_request
|
151
|
-
end
|
152
|
-
|
153
|
-
describe 'permanent redirect' do
|
154
|
-
before do
|
155
|
-
@redirect_response.stub!(:is_permanent_redirect?).and_return(true)
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'should check if the response was a permanent redirect' do
|
159
|
-
@redirect_response.should_receive(:is_permanent_redirect?).and_return(true)
|
160
|
-
make_request
|
161
|
-
end
|
162
|
-
|
163
|
-
it 'should add the new location as the effective uri' do
|
164
|
-
make_request
|
165
|
-
@resource.effective_uri.should == @redirected_uri
|
166
|
-
end
|
167
|
-
|
168
|
-
it 'should remake the request with the new uri' do
|
169
|
-
Resourceful::Request.should_receive(:new).twice.and_return(@request)
|
170
|
-
@request.should_receive(:response).twice.and_return(@redirect_response, @response)
|
171
|
-
make_request
|
172
|
-
end
|
173
|
-
|
174
|
-
end
|
175
|
-
|
176
|
-
describe 'temporary redirect' do
|
177
|
-
before do
|
178
|
-
@redirect_response.stub!(:is_permanent_redirect?).and_return(false)
|
179
|
-
end
|
180
|
-
|
181
|
-
it 'should check if the response was not a permanent redirect' do
|
182
|
-
@redirect_response.should_receive(:is_permanent_redirect?).and_return(false)
|
183
|
-
make_request
|
184
|
-
end
|
185
|
-
|
186
|
-
it 'should not add the new location as the effective uri' do
|
187
|
-
make_request
|
188
|
-
@resource.effective_uri.should == @uri
|
189
|
-
end
|
190
|
-
|
191
|
-
it 'should make a new resource from the new location' do
|
192
|
-
new_resource = mock('resource', :do_read_request => @response, :uri => @uri)
|
193
|
-
Resourceful::Resource.should_receive(:new).with(@accessor, @redirected_uri).and_return(new_resource)
|
194
|
-
make_request
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
end # read with redirection
|
200
|
-
|
201
|
-
describe 'with authorization' do
|
202
|
-
before do
|
203
|
-
@authmgr = mock('auth_manager')
|
204
|
-
@authmgr.stub!(:add_credentials)
|
205
|
-
@authmgr.stub!(:associate_auth_info).and_return(true)
|
206
|
-
|
207
|
-
@accessor.stub!(:auth_manager).and_return(@authmgr)
|
208
|
-
end
|
209
|
-
|
210
|
-
it 'should attempt to add credentials to the request' do
|
211
|
-
@authmgr.should_receive(:add_credentials).with(@request)
|
212
|
-
make_request
|
213
|
-
end
|
214
|
-
|
215
|
-
it 'should check if the response was not authorized' do
|
216
|
-
@response.should_receive(:is_not_authorized?).and_return(false)
|
217
|
-
make_request
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'should associate the auth info in the response if it was not authorized' do
|
221
|
-
@authmgr.should_receive(:associate_auth_info).with(@response).and_return(true)
|
222
|
-
@response.stub!(:is_not_authorized?).and_return(true)
|
223
|
-
make_request
|
224
|
-
end
|
225
|
-
|
226
|
-
it 'should re-make the request only once if it was not authorized the first time' do
|
227
|
-
Resourceful::Request.should_receive(:new).with(:some_method, @resource, nil, {}).twice.and_return(@request)
|
228
|
-
@response.stub!(:is_not_authorized?).and_return(true)
|
229
|
-
make_request
|
230
|
-
end
|
231
|
-
|
232
|
-
end
|
233
|
-
|
234
|
-
describe 'with caching' do
|
235
|
-
before do
|
236
|
-
@cached_response = mock('cached response', :is_redirect? => false,
|
237
|
-
:is_not_authorized? => false,
|
238
|
-
:is_success? => true,
|
239
|
-
:stale? => false)
|
240
|
-
@cache_manager.stub!(:lookup).and_return(@cached_response)
|
241
|
-
end
|
242
|
-
|
243
|
-
it 'should lookup the request in the cache' do
|
244
|
-
@cache_manager.should_receive(:lookup).with(@request)
|
245
|
-
make_request
|
246
|
-
end
|
247
|
-
|
248
|
-
it 'should check if the cached response is stale' do
|
249
|
-
@cached_response.should_receive(:stale?).and_return(false)
|
250
|
-
make_request
|
251
|
-
end
|
252
|
-
|
253
|
-
describe 'in cache' do
|
254
|
-
|
255
|
-
end
|
256
|
-
|
257
|
-
describe 'in cache but stale' do
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
describe 'not in cache' do
|
262
|
-
|
263
|
-
end
|
264
|
-
|
265
|
-
end
|
266
|
-
|
267
|
-
end
|
268
|
-
|
269
|
-
describe '#do_write_request' do
|
270
|
-
|
271
|
-
def make_request
|
272
|
-
@resource.do_write_request(:some_method, "data")
|
273
|
-
end
|
274
|
-
|
275
|
-
it 'should make a new request object from the method' do
|
276
|
-
Resourceful::Request.should_receive(:new).with(:some_method, @resource, "data", anything).and_return(@request)
|
277
|
-
@resource.do_write_request(:some_method, "data")
|
278
|
-
end
|
279
|
-
|
280
|
-
describe 'default options' do
|
281
|
-
before do
|
282
|
-
@resource.default_options = {:foo => :bar}
|
283
|
-
end
|
284
|
-
|
285
|
-
it 'should merge the header with the default options' do
|
286
|
-
Resourceful::Request.should_receive(:new).with(anything, anything, anything, :foo => :bar).and_return(@request)
|
287
|
-
make_request
|
288
|
-
end
|
289
|
-
|
290
|
-
it 'should override any default header with the request header' do
|
291
|
-
Resourceful::Request.should_receive(:new).with(anything, anything, anything, :foo => :baz).and_return(@request)
|
292
|
-
@resource.do_write_request(:some_method, "data", :foo => :baz)
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
describe 'non-success responses' do
|
297
|
-
before do
|
298
|
-
@uri = 'http://www.example.com/code/404'
|
299
|
-
@resource = Resourceful::Resource.new(@accessor, @uri)
|
300
|
-
|
301
|
-
@redirected_uri = 'http://www.example.com/get'
|
302
|
-
@response = mock('response',
|
303
|
-
:header => {'Location' => [@redirected_uri]},
|
304
|
-
:is_redirect? => false,
|
305
|
-
:is_success? => false,
|
306
|
-
:is_not_authorized? => false,
|
307
|
-
:code => 404)
|
308
|
-
|
309
|
-
@request.stub!(:response).and_return(@response)
|
310
|
-
@request.stub!(:method).and_return(:post)
|
311
|
-
@request.stub!(:uri).and_return('http://www.example.com/code/404')
|
312
|
-
end
|
313
|
-
|
314
|
-
it 'should raise UnsuccessfulHttpRequestError' do
|
315
|
-
lambda {
|
316
|
-
@resource.do_write_request(:post, "data")
|
317
|
-
}.should raise_error(Resourceful::UnsuccessfulHttpRequestError)
|
318
|
-
end
|
319
|
-
|
320
|
-
it 'should give a reasonable error message' do
|
321
|
-
lambda {
|
322
|
-
@resource.do_write_request(:post, "data")
|
323
|
-
}.should raise_error("post request to <http://www.example.com/code/404> failed with code 404")
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
describe 'with redirection' do
|
328
|
-
before do
|
329
|
-
@uri = 'http://www.example.com/redirect/301?http://www.example.com/get'
|
330
|
-
@resource = Resourceful::Resource.new(@accessor, @uri)
|
331
|
-
|
332
|
-
@redirected_uri = 'http://www.example.com/get'
|
333
|
-
@redirect_response = mock('redirect_response',
|
334
|
-
:header => {'Location' => [@redirected_uri]},
|
335
|
-
:is_redirect? => true,
|
336
|
-
:is_permanent_redirect? => true)
|
337
|
-
|
338
|
-
@request.stub!(:response).and_return(@redirect_response, @response)
|
339
|
-
|
340
|
-
end
|
341
|
-
|
342
|
-
it 'should check if the response was a redirect' do
|
343
|
-
@redirect_response.should_receive(:is_redirect?).and_return(true)
|
344
|
-
make_request
|
345
|
-
end
|
346
|
-
|
347
|
-
it 'should check if the request should be redirected' do
|
348
|
-
@request.should_receive(:should_be_redirected?).and_return(true)
|
349
|
-
make_request
|
350
|
-
end
|
351
|
-
|
352
|
-
describe 'permanent redirect' do
|
353
|
-
before do
|
354
|
-
@redirect_response.stub!(:is_permanent_redirect?).and_return(true)
|
355
|
-
end
|
356
|
-
|
357
|
-
it 'should check if the response was a permanent redirect' do
|
358
|
-
@redirect_response.should_receive(:is_permanent_redirect?).and_return(true)
|
359
|
-
make_request
|
360
|
-
end
|
361
|
-
|
362
|
-
it 'should add the new location as the effective uri' do
|
363
|
-
make_request
|
364
|
-
@resource.effective_uri.should == @redirected_uri
|
365
|
-
end
|
366
|
-
|
367
|
-
it 'should remake the request with the new uri' do
|
368
|
-
Resourceful::Request.should_receive(:new).twice.and_return(@request)
|
369
|
-
@request.should_receive(:response).twice.and_return(@redirect_response, @response)
|
370
|
-
make_request
|
371
|
-
end
|
372
|
-
|
373
|
-
end
|
374
|
-
|
375
|
-
describe 'temporary redirect' do
|
376
|
-
before do
|
377
|
-
@redirect_response.stub!(:is_permanent_redirect?).and_return(false)
|
378
|
-
@redirect_response.stub!(:code).and_return(302)
|
379
|
-
end
|
380
|
-
|
381
|
-
it 'should check if the response was not a permanent redirect' do
|
382
|
-
@redirect_response.should_receive(:is_permanent_redirect?).and_return(false)
|
383
|
-
make_request
|
384
|
-
end
|
385
|
-
|
386
|
-
it 'should not add the new location as the effective uri' do
|
387
|
-
make_request
|
388
|
-
@resource.effective_uri.should == @uri
|
389
|
-
end
|
390
|
-
|
391
|
-
it 'should make a new resource from the new location' do
|
392
|
-
new_resource = mock('resource', :do_write_request => @response)
|
393
|
-
Resourceful::Resource.should_receive(:new).with(@accessor, @redirected_uri).and_return(new_resource)
|
394
|
-
make_request
|
395
|
-
end
|
396
|
-
|
397
|
-
describe '302 Found' do
|
398
|
-
before do
|
399
|
-
@new_resource = mock('resource')
|
400
|
-
Resourceful::Resource.should_receive(:new).with(@accessor, @redirected_uri).and_return(@new_resource)
|
401
|
-
@redirect_response.stub!(:code).and_return(303)
|
402
|
-
end
|
403
|
-
|
404
|
-
it 'should redirect to the new location with a GET request, regardless of the original method' do
|
405
|
-
@new_resource.should_receive(:do_read_request).with(:get, {}).and_return(@response)
|
406
|
-
make_request
|
407
|
-
end
|
408
|
-
end
|
409
|
-
|
410
|
-
end
|
411
|
-
|
412
|
-
end # write with redirection
|
413
|
-
|
414
|
-
describe 'with authorization' do
|
415
|
-
before do
|
416
|
-
@authmgr = mock('auth_manager')
|
417
|
-
@authmgr.stub!(:add_credentials)
|
418
|
-
@authmgr.stub!(:associate_auth_info).and_return(true)
|
419
|
-
|
420
|
-
@accessor.stub!(:auth_manager).and_return(@authmgr)
|
421
|
-
end
|
422
|
-
|
423
|
-
it 'should attempt to add credentials to the request' do
|
424
|
-
@authmgr.should_receive(:add_credentials).with(@request)
|
425
|
-
make_request
|
426
|
-
end
|
427
|
-
|
428
|
-
it 'should check if the response was not authorized' do
|
429
|
-
@response.should_receive(:is_not_authorized?).and_return(false)
|
430
|
-
make_request
|
431
|
-
end
|
432
|
-
|
433
|
-
it 'should associate the auth info in the response if it was not authorized' do
|
434
|
-
@authmgr.should_receive(:associate_auth_info).with(@response).and_return(true)
|
435
|
-
@response.stub!(:is_not_authorized?).and_return(true)
|
436
|
-
make_request
|
437
|
-
end
|
438
|
-
|
439
|
-
it 'should re-make the request only once if it was not authorized the first time' do
|
440
|
-
Resourceful::Request.should_receive(:new).with(:some_method, @resource, "data", {}).twice.and_return(@request)
|
441
|
-
@response.stub!(:is_not_authorized?).and_return(true)
|
442
|
-
make_request
|
443
|
-
end
|
444
|
-
end
|
445
|
-
|
446
|
-
end
|
447
|
-
|
448
|
-
describe 'callback registration' do
|
449
|
-
before do
|
450
|
-
@callback = mock('callback')
|
451
|
-
@callback.stub!(:call).and_return(true)
|
452
|
-
|
453
|
-
@resource.on_redirect { @callback.call }
|
454
|
-
end
|
455
|
-
|
456
|
-
it 'should store the callback when called with a block' do
|
457
|
-
@resource.on_redirect { true }
|
458
|
-
|
459
|
-
callback = @resource.instance_variable_get(:@on_redirect)
|
460
|
-
callback.should be_kind_of(Proc)
|
461
|
-
end
|
462
|
-
|
463
|
-
it 'should return the callback when called without a block' do
|
464
|
-
callback = lambda { "foo" }
|
465
|
-
@resource.on_redirect(&callback)
|
466
|
-
@resource.on_redirect.should == callback
|
467
|
-
end
|
468
|
-
|
469
|
-
end
|
470
|
-
|
471
|
-
describe '#get' do
|
472
|
-
|
473
|
-
it 'should be a method' do
|
474
|
-
@resource.should respond_to(:get)
|
475
|
-
end
|
476
|
-
|
477
|
-
it 'should pass :get to the #do_read_request method' do
|
478
|
-
@resource.should_receive(:do_read_request).with(:get, {}).and_return(@response)
|
479
|
-
@resource.get
|
480
|
-
end
|
481
|
-
|
482
|
-
it 'should return the response of making the request' do
|
483
|
-
@resource.get.should == @response
|
484
|
-
end
|
485
|
-
|
486
|
-
end
|
487
|
-
|
488
|
-
describe "#delete" do
|
489
|
-
|
490
|
-
it 'should be a method' do
|
491
|
-
@resource.should respond_to(:delete)
|
492
|
-
end
|
493
|
-
|
494
|
-
it 'should return the response of making the request' do
|
495
|
-
@resource.delete.should == @response
|
496
|
-
end
|
497
|
-
|
498
|
-
end
|
499
|
-
|
500
|
-
describe "#post(body_data, :content_type => content-type)" do
|
501
|
-
before do
|
502
|
-
@resource = Resourceful::Resource.new(@accessor, 'http://foo.invalid/')
|
503
|
-
@response = mock('response', :is_redirect? => false, :is_success? => true, :is_not_authorized? => false, :code => 200)
|
504
|
-
@request = mock('request', :response => @response)
|
505
|
-
Resourceful::Request.stub!(:new).and_return(@request)
|
506
|
-
end
|
507
|
-
|
508
|
-
it "should get the response from the request" do
|
509
|
-
@request.should_receive(:response).and_return(@response)
|
510
|
-
|
511
|
-
@resource.post("a body", :content_type => 'text/plain')
|
512
|
-
end
|
513
|
-
|
514
|
-
it 'should put the content type in the header' do
|
515
|
-
Resourceful::Request.should_receive(:new).
|
516
|
-
with(anything,
|
517
|
-
anything,
|
518
|
-
anything,
|
519
|
-
hash_including(:content_type =>'text/plain')).
|
520
|
-
and_return(@request)
|
521
|
-
|
522
|
-
@resource.post("a body", :content_type => 'text/plain')
|
523
|
-
end
|
524
|
-
|
525
|
-
it 'should create a post request' do
|
526
|
-
Resourceful::Request.should_receive(:new).
|
527
|
-
with(:post, anything, anything, anything).
|
528
|
-
and_return(@request)
|
529
|
-
|
530
|
-
@resource.post("a body", :content_type => 'text/plain')
|
531
|
-
end
|
532
|
-
|
533
|
-
it 'should pass body to the request object' do
|
534
|
-
Resourceful::Request.should_receive(:new).
|
535
|
-
with(anything, anything, "a body", anything).
|
536
|
-
and_return(@request)
|
537
|
-
|
538
|
-
@resource.post("a body", :content_type => 'text/plain')
|
539
|
-
end
|
540
|
-
|
541
|
-
it 'should pass self to the request object' do
|
542
|
-
Resourceful::Request.should_receive(:new).
|
543
|
-
with(anything, @resource, anything, anything).
|
544
|
-
and_return(@request)
|
545
|
-
|
546
|
-
@resource.post("a body", :content_type => 'text/plain')
|
547
|
-
end
|
548
|
-
end
|
549
|
-
|
550
|
-
describe "#put(body_data, :content_type => content_type)" do
|
551
|
-
before do
|
552
|
-
@resource = Resourceful::Resource.new(@accessor, 'http://foo.invalid/')
|
553
|
-
@response = mock('response', :is_redirect? => false, :is_success? => true, :is_not_authorized? => false, :code => 200)
|
554
|
-
@request = mock('request', :response => @response)
|
555
|
-
Resourceful::Request.stub!(:new).and_return(@request)
|
556
|
-
end
|
557
|
-
|
558
|
-
it "should get the response from the request" do
|
559
|
-
@request.should_receive(:response).and_return(@response)
|
560
|
-
|
561
|
-
@resource.put("a body", :content_type => 'text/plain')
|
562
|
-
end
|
563
|
-
|
564
|
-
it 'should put the content type in the header' do
|
565
|
-
Resourceful::Request.should_receive(:new).
|
566
|
-
with(anything,
|
567
|
-
anything,
|
568
|
-
anything,
|
569
|
-
hash_including(:content_type =>'text/plain')).
|
570
|
-
and_return(@request)
|
571
|
-
|
572
|
-
@resource.put("a body", :content_type => 'text/plain')
|
573
|
-
end
|
574
|
-
|
575
|
-
it 'should create a put request' do
|
576
|
-
Resourceful::Request.should_receive(:new).
|
577
|
-
with(:put, anything, anything, anything).
|
578
|
-
and_return(@request)
|
579
|
-
|
580
|
-
@resource.put("a body", :content_type => 'text/plain')
|
581
|
-
end
|
582
|
-
|
583
|
-
it 'should pass body to the request object' do
|
584
|
-
Resourceful::Request.should_receive(:new).
|
585
|
-
with(anything, anything, "a body", anything).
|
586
|
-
and_return(@request)
|
587
|
-
|
588
|
-
@resource.put("a body", :content_type => 'text/plain')
|
589
|
-
end
|
590
|
-
|
591
|
-
it 'should pass self to the request object' do
|
592
|
-
Resourceful::Request.should_receive(:new).
|
593
|
-
with(anything, @resource, anything, anything).
|
594
|
-
and_return(@request)
|
595
|
-
|
596
|
-
@resource.put("a body", :content_type => 'text/plain')
|
597
|
-
end
|
598
|
-
end
|
599
|
-
|
600
|
-
end
|