francois-rest-client 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +104 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/bin/restclient +87 -0
- data/lib/rest_client.rb +2 -0
- data/lib/restclient/exceptions.rb +88 -0
- data/lib/restclient/mixin/response.rb +43 -0
- data/lib/restclient/net_http_ext.rb +21 -0
- data/lib/restclient/payload.rb +206 -0
- data/lib/restclient/raw_response.rb +30 -0
- data/lib/restclient/request.rb +241 -0
- data/lib/restclient/resource.rb +146 -0
- data/lib/restclient/response.rb +20 -0
- data/lib/restclient.rb +107 -0
- data/spec/base.rb +10 -0
- data/spec/exceptions_spec.rb +65 -0
- data/spec/master_shake.jpg +0 -0
- data/spec/mixin/response_spec.rb +46 -0
- data/spec/payload_spec.rb +76 -0
- data/spec/raw_response_spec.rb +17 -0
- data/spec/request_spec.rb +484 -0
- data/spec/resource_spec.rb +75 -0
- data/spec/response_spec.rb +16 -0
- data/spec/restclient_spec.rb +53 -0
- metadata +85 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/base"
|
2
|
+
|
3
|
+
describe RestClient::Payload do
|
4
|
+
context "A regular Payload" do
|
5
|
+
it "should should default content-type to standard enctype" do
|
6
|
+
RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
|
7
|
+
should == 'application/x-www-form-urlencoded'
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should form properly encoded params" do
|
11
|
+
RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
|
12
|
+
should == "foo=bar"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "A multipart Payload" do
|
17
|
+
it "should should default content-type to standard enctype" do
|
18
|
+
m = RestClient::Payload::Multipart.new({})
|
19
|
+
m.stub!(:boundary).and_return(123)
|
20
|
+
m.headers['Content-Type'].should == 'multipart/form-data; boundary="123"'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should form properly seperated multipart data" do
|
24
|
+
m = RestClient::Payload::Multipart.new({:foo => "bar", :bar => "baz"})
|
25
|
+
m.to_s.should == <<-EOS
|
26
|
+
--#{m.boundary}\r
|
27
|
+
Content-Disposition: multipart/form-data; name="bar"\r
|
28
|
+
\r
|
29
|
+
baz\r
|
30
|
+
--#{m.boundary}\r
|
31
|
+
Content-Disposition: multipart/form-data; name="foo"\r
|
32
|
+
\r
|
33
|
+
bar\r
|
34
|
+
--#{m.boundary}--\r
|
35
|
+
EOS
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should form properly seperated multipart data" do
|
39
|
+
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
40
|
+
m = RestClient::Payload::Multipart.new({:foo => f})
|
41
|
+
m.to_s.should == <<-EOS
|
42
|
+
--#{m.boundary}\r
|
43
|
+
Content-Disposition: multipart/form-data; name="foo"; filename="master_shake.jpg"\r
|
44
|
+
Content-Type: image/jpeg\r
|
45
|
+
\r
|
46
|
+
#{IO.read(f.path)}\r
|
47
|
+
--#{m.boundary}--\r
|
48
|
+
EOS
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "Payload generation" do
|
53
|
+
it "should recognize standard urlencoded params" do
|
54
|
+
RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should recognize multipart params" do
|
58
|
+
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
59
|
+
|
60
|
+
RestClient::Payload.generate({"foo" => f}).should be_kind_of(RestClient::Payload::Multipart)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be multipart if forced" do
|
64
|
+
RestClient::Payload.generate({"foo" => "bar", :multipart => true}).should be_kind_of(RestClient::Payload::Multipart)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should return data if no of the above" do
|
68
|
+
RestClient::Payload.generate("data").should be_kind_of(RestClient::Payload::Base)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should recognize nested multipart payloads" do
|
72
|
+
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
73
|
+
RestClient::Payload.generate({"foo" => {"file" => f}}).should be_kind_of(RestClient::Payload::Multipart)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::RawResponse do
|
4
|
+
before do
|
5
|
+
@tf = mock("Tempfile", :read => "the answer is 42", :open => true)
|
6
|
+
@net_http_res = mock('net http response')
|
7
|
+
@response = RestClient::RawResponse.new(@tf, @net_http_res)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "behaves like string" do
|
11
|
+
@response.to_s.should == 'the answer is 42'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "exposes a Tempfile" do
|
15
|
+
@response.file.should == @tf
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,484 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Request do
|
4
|
+
before do
|
5
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
6
|
+
|
7
|
+
@uri = mock("uri")
|
8
|
+
@uri.stub!(:request_uri).and_return('/resource')
|
9
|
+
@uri.stub!(:host).and_return('some')
|
10
|
+
@uri.stub!(:port).and_return(80)
|
11
|
+
|
12
|
+
@net = mock("net::http base")
|
13
|
+
@http = mock("net::http connection")
|
14
|
+
Net::HTTP.stub!(:new).and_return(@net)
|
15
|
+
@net.stub!(:start).and_yield(@http)
|
16
|
+
@net.stub!(:use_ssl=)
|
17
|
+
@net.stub!(:verify_mode=)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "accept */* mimetype, preferring xml" do
|
21
|
+
@request.default_headers[:accept].should == '*/*; q=0.5, application/xml'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "decodes an uncompressed result body by passing it straight through" do
|
25
|
+
RestClient::Request.decode(nil, 'xyz').should == 'xyz'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "decodes a gzip body" do
|
29
|
+
RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "ingores gzip for empty bodies" do
|
33
|
+
RestClient::Request.decode('gzip', '').should be_empty
|
34
|
+
end
|
35
|
+
|
36
|
+
it "decodes a deflated body" do
|
37
|
+
RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "processes a successful result" do
|
41
|
+
res = mock("result")
|
42
|
+
res.stub!(:code).and_return("200")
|
43
|
+
res.stub!(:body).and_return('body')
|
44
|
+
res.stub!(:[]).with('content-encoding').and_return(nil)
|
45
|
+
@request.process_result(res).should == 'body'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "doesn't classify successful requests as failed" do
|
49
|
+
203.upto(206) do |code|
|
50
|
+
res = mock("result")
|
51
|
+
res.stub!(:code).and_return(code.to_s)
|
52
|
+
res.stub!(:body).and_return("")
|
53
|
+
res.stub!(:[]).with('content-encoding').and_return(nil)
|
54
|
+
@request.process_result(res).should be_empty
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "parses a url into a URI object" do
|
59
|
+
URI.should_receive(:parse).with('http://example.com/resource')
|
60
|
+
@request.parse_url('http://example.com/resource')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "adds http:// to the front of resources specified in the syntax example.com/resource" do
|
64
|
+
URI.should_receive(:parse).with('http://example.com/resource')
|
65
|
+
@request.parse_url('example.com/resource')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "extracts the username and password when parsing http://user:password@example.com/" do
|
69
|
+
URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
|
70
|
+
@request.parse_url_with_auth('http://joe:pass1@example.com/resource')
|
71
|
+
@request.user.should == 'joe'
|
72
|
+
@request.password.should == 'pass1'
|
73
|
+
end
|
74
|
+
|
75
|
+
it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
|
76
|
+
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
|
77
|
+
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
|
78
|
+
@request.parse_url_with_auth('http://example.com/resource')
|
79
|
+
@request.user.should == 'beth'
|
80
|
+
@request.password.should == 'pass2'
|
81
|
+
end
|
82
|
+
|
83
|
+
it "correctly formats cookies provided to the constructor" do
|
84
|
+
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
|
85
|
+
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
|
86
|
+
@request.should_receive(:default_headers).and_return({'foo' => 'bar'})
|
87
|
+
headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
|
88
|
+
end
|
89
|
+
|
90
|
+
it "determines the Net::HTTP class to instantiate by the method name" do
|
91
|
+
@request.net_http_request_class(:put).should == Net::HTTP::Put
|
92
|
+
end
|
93
|
+
|
94
|
+
it "merges user headers with the default headers" do
|
95
|
+
@request.should_receive(:default_headers).and_return({ '1' => '2' })
|
96
|
+
headers = @request.make_headers('3' => '4')
|
97
|
+
headers.should have_key('1')
|
98
|
+
headers['1'].should == '2'
|
99
|
+
headers.should have_key('3')
|
100
|
+
headers['3'].should == '4'
|
101
|
+
end
|
102
|
+
|
103
|
+
it "prefers the user header when the same header exists in the defaults" do
|
104
|
+
@request.should_receive(:default_headers).and_return({ '1' => '2' })
|
105
|
+
headers = @request.make_headers('1' => '3')
|
106
|
+
headers.should have_key('1')
|
107
|
+
headers['1'].should == '3'
|
108
|
+
end
|
109
|
+
|
110
|
+
it "converts header symbols from :content_type to 'Content-type'" do
|
111
|
+
@request.should_receive(:default_headers).and_return({})
|
112
|
+
headers = @request.make_headers(:content_type => 'abc')
|
113
|
+
headers.should have_key('Content-type')
|
114
|
+
headers['Content-type'].should == 'abc'
|
115
|
+
end
|
116
|
+
|
117
|
+
it "converts header values to strings" do
|
118
|
+
@request.make_headers('A' => 1)['A'].should == '1'
|
119
|
+
end
|
120
|
+
|
121
|
+
it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
|
122
|
+
@request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
|
123
|
+
klass = mock("net:http class")
|
124
|
+
@request.should_receive(:net_http_request_class).with(:put).and_return(klass)
|
125
|
+
klass.should_receive(:new).and_return('result')
|
126
|
+
@request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
|
127
|
+
@request.execute_inner
|
128
|
+
end
|
129
|
+
|
130
|
+
it "transmits the request with Net::HTTP" do
|
131
|
+
@http.should_receive(:request).with('req', 'payload')
|
132
|
+
@request.should_receive(:process_result)
|
133
|
+
@request.should_receive(:response_log)
|
134
|
+
@request.transmit(@uri, 'req', 'payload')
|
135
|
+
end
|
136
|
+
|
137
|
+
it "uses SSL when the URI refers to a https address" do
|
138
|
+
@uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
|
139
|
+
@net.should_receive(:use_ssl=).with(true)
|
140
|
+
@http.stub!(:request)
|
141
|
+
@request.stub!(:process_result)
|
142
|
+
@request.stub!(:response_log)
|
143
|
+
@request.transmit(@uri, 'req', 'payload')
|
144
|
+
end
|
145
|
+
|
146
|
+
it "sends nil payloads" do
|
147
|
+
@http.should_receive(:request).with('req', nil)
|
148
|
+
@request.should_receive(:process_result)
|
149
|
+
@request.stub!(:response_log)
|
150
|
+
@request.transmit(@uri, 'req', nil)
|
151
|
+
end
|
152
|
+
|
153
|
+
it "passes non-hash payloads straight through" do
|
154
|
+
@request.process_payload("x").should == "x"
|
155
|
+
end
|
156
|
+
|
157
|
+
it "converts a hash payload to urlencoded data" do
|
158
|
+
@request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
|
159
|
+
end
|
160
|
+
|
161
|
+
it "accepts nested hashes in payload" do
|
162
|
+
payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
|
163
|
+
payload.should include('user[name]=joe')
|
164
|
+
payload.should include('user[location][country]=USA')
|
165
|
+
payload.should include('user[location][state]=CA')
|
166
|
+
end
|
167
|
+
|
168
|
+
it "set urlencoded content_type header on hash payloads" do
|
169
|
+
@request.process_payload(:a => 1)
|
170
|
+
@request.headers[:content_type].should == 'application/x-www-form-urlencoded'
|
171
|
+
end
|
172
|
+
|
173
|
+
it "sets up the credentials prior to the request" do
|
174
|
+
@http.stub!(:request)
|
175
|
+
@request.stub!(:process_result)
|
176
|
+
@request.stub!(:response_log)
|
177
|
+
|
178
|
+
@request.stub!(:user).and_return('joe')
|
179
|
+
@request.stub!(:password).and_return('mypass')
|
180
|
+
@request.should_receive(:setup_credentials).with('req')
|
181
|
+
|
182
|
+
@request.transmit(@uri, 'req', nil)
|
183
|
+
end
|
184
|
+
|
185
|
+
it "does not attempt to send any credentials if user is nil" do
|
186
|
+
@request.stub!(:user).and_return(nil)
|
187
|
+
req = mock("request")
|
188
|
+
req.should_not_receive(:basic_auth)
|
189
|
+
@request.setup_credentials(req)
|
190
|
+
end
|
191
|
+
|
192
|
+
it "setup credentials when there's a user" do
|
193
|
+
@request.stub!(:user).and_return('joe')
|
194
|
+
@request.stub!(:password).and_return('mypass')
|
195
|
+
req = mock("request")
|
196
|
+
req.should_receive(:basic_auth).with('joe', 'mypass')
|
197
|
+
@request.setup_credentials(req)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "catches EOFError and shows the more informative ServerBrokeConnection" do
|
201
|
+
@http.stub!(:request).and_raise(EOFError)
|
202
|
+
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
|
203
|
+
end
|
204
|
+
|
205
|
+
it "execute calls execute_inner" do
|
206
|
+
@request.should_receive(:execute_inner)
|
207
|
+
@request.execute
|
208
|
+
end
|
209
|
+
|
210
|
+
it "class method execute wraps constructor" do
|
211
|
+
req = mock("rest request")
|
212
|
+
RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
|
213
|
+
req.should_receive(:execute)
|
214
|
+
RestClient::Request.execute(1 => 2)
|
215
|
+
end
|
216
|
+
|
217
|
+
it "raises a Redirect with the new location when the response is in the 30x range" do
|
218
|
+
res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
|
219
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
|
220
|
+
end
|
221
|
+
|
222
|
+
it "handles redirects with relative paths" do
|
223
|
+
res = mock('response', :code => '301', :header => { 'Location' => 'index' })
|
224
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
|
225
|
+
end
|
226
|
+
|
227
|
+
it "handles redirects with absolute paths" do
|
228
|
+
@request.instance_variable_set('@url', 'http://some/place/else')
|
229
|
+
res = mock('response', :code => '301', :header => { 'Location' => '/index' })
|
230
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
|
231
|
+
end
|
232
|
+
|
233
|
+
it "uses GET and clears payload when following 30x redirects" do
|
234
|
+
url = "http://example.com/redirected"
|
235
|
+
|
236
|
+
@request.should_receive(:execute_inner).once.ordered.and_raise(RestClient::Redirect.new(url))
|
237
|
+
|
238
|
+
@request.should_receive(:execute_inner).once.ordered do
|
239
|
+
@request.url.should == url
|
240
|
+
@request.method.should == :get
|
241
|
+
@request.payload.should be_nil
|
242
|
+
end
|
243
|
+
|
244
|
+
@request.execute
|
245
|
+
end
|
246
|
+
|
247
|
+
it "raises Unauthorized when the response is 401" do
|
248
|
+
res = mock('response', :code => '401')
|
249
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "raises ResourceNotFound when the response is 404" do
|
253
|
+
res = mock('response', :code => '404')
|
254
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
|
255
|
+
end
|
256
|
+
|
257
|
+
it "raises RequestFailed otherwise" do
|
258
|
+
res = mock('response', :code => '500')
|
259
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
|
260
|
+
end
|
261
|
+
|
262
|
+
it "creates a proxy class if a proxy url is given" do
|
263
|
+
RestClient.stub!(:proxy).and_return("http://example.com/")
|
264
|
+
@request.net_http_class.should include(Net::HTTP::ProxyDelta)
|
265
|
+
end
|
266
|
+
|
267
|
+
it "creates a non-proxy class if a proxy url is not given" do
|
268
|
+
@request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
|
269
|
+
end
|
270
|
+
|
271
|
+
it "logs a get request" do
|
272
|
+
RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
|
273
|
+
'RestClient.get "http://url"'
|
274
|
+
end
|
275
|
+
|
276
|
+
it "logs a post request with a small payload" do
|
277
|
+
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
|
278
|
+
'RestClient.post "http://url", "foo"'
|
279
|
+
end
|
280
|
+
|
281
|
+
it "logs a post request with a large payload" do
|
282
|
+
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
|
283
|
+
'RestClient.post "http://url", "(1000 byte payload)"'
|
284
|
+
end
|
285
|
+
|
286
|
+
it "logs input headers as a hash" do
|
287
|
+
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
|
288
|
+
'RestClient.get "http://url", :accept=>"text/plain"'
|
289
|
+
end
|
290
|
+
|
291
|
+
it "logs a response including the status code, content type, and result body size in bytes" do
|
292
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
293
|
+
res.stub!(:[]).with('Content-type').and_return('text/html')
|
294
|
+
@request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
|
295
|
+
end
|
296
|
+
|
297
|
+
it "logs a response with a nil Content-type" do
|
298
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
299
|
+
res.stub!(:[]).with('Content-type').and_return(nil)
|
300
|
+
@request.response_log(res).should == "# => 200 OK | 4 bytes"
|
301
|
+
end
|
302
|
+
|
303
|
+
it "logs a response with a nil body" do
|
304
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
|
305
|
+
res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
306
|
+
@request.response_log(res).should == "# => 200 OK | text/html 0 bytes"
|
307
|
+
end
|
308
|
+
|
309
|
+
it "strips the charset from the response content type" do
|
310
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
311
|
+
res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
312
|
+
@request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
|
313
|
+
end
|
314
|
+
|
315
|
+
it "displays the log to stdout" do
|
316
|
+
RestClient.stub!(:log).and_return('stdout')
|
317
|
+
STDOUT.should_receive(:puts).with('xyz')
|
318
|
+
@request.display_log('xyz')
|
319
|
+
end
|
320
|
+
|
321
|
+
it "displays the log to stderr" do
|
322
|
+
RestClient.stub!(:log).and_return('stderr')
|
323
|
+
STDERR.should_receive(:puts).with('xyz')
|
324
|
+
@request.display_log('xyz')
|
325
|
+
end
|
326
|
+
|
327
|
+
it "append the log to the requested filename" do
|
328
|
+
RestClient.stub!(:log).and_return('/tmp/restclient.log')
|
329
|
+
f = mock('file handle')
|
330
|
+
File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
|
331
|
+
f.should_receive(:puts).with('xyz')
|
332
|
+
@request.display_log('xyz')
|
333
|
+
end
|
334
|
+
|
335
|
+
it "set read_timeout" do
|
336
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
|
337
|
+
@http.stub!(:request)
|
338
|
+
@request.stub!(:process_result)
|
339
|
+
@request.stub!(:response_log)
|
340
|
+
|
341
|
+
@net.should_receive(:read_timeout=).with(123)
|
342
|
+
|
343
|
+
@request.transmit(@uri, 'req', nil)
|
344
|
+
end
|
345
|
+
|
346
|
+
it "set open_timeout" do
|
347
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
|
348
|
+
@http.stub!(:request)
|
349
|
+
@request.stub!(:process_result)
|
350
|
+
@request.stub!(:response_log)
|
351
|
+
|
352
|
+
@net.should_receive(:open_timeout=).with(123)
|
353
|
+
|
354
|
+
@request.transmit(@uri, 'req', nil)
|
355
|
+
end
|
356
|
+
|
357
|
+
it "should default to not verifying ssl certificates" do
|
358
|
+
@request.verify_ssl.should == false
|
359
|
+
end
|
360
|
+
|
361
|
+
it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
|
362
|
+
@net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
363
|
+
@http.stub!(:request)
|
364
|
+
@request.stub!(:process_result)
|
365
|
+
@request.stub!(:response_log)
|
366
|
+
@request.transmit(@uri, 'req', 'payload')
|
367
|
+
end
|
368
|
+
|
369
|
+
it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
|
370
|
+
@request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
|
371
|
+
@net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
372
|
+
@http.stub!(:request)
|
373
|
+
@request.stub!(:process_result)
|
374
|
+
@request.stub!(:response_log)
|
375
|
+
@request.transmit(@uri, 'req', 'payload')
|
376
|
+
end
|
377
|
+
|
378
|
+
it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
|
379
|
+
mode = OpenSSL::SSL::VERIFY_PEER |
|
380
|
+
OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
381
|
+
@request = RestClient::Request.new( :method => :put,
|
382
|
+
:url => 'https://some/resource',
|
383
|
+
:payload => 'payload',
|
384
|
+
:verify_ssl => mode )
|
385
|
+
@net.should_receive(:verify_mode=).with(mode)
|
386
|
+
@http.stub!(:request)
|
387
|
+
@request.stub!(:process_result)
|
388
|
+
@request.stub!(:response_log)
|
389
|
+
@request.transmit(@uri, 'req', 'payload')
|
390
|
+
end
|
391
|
+
|
392
|
+
it "should default to not having an ssl_client_cert" do
|
393
|
+
@request.ssl_client_cert.should be(nil)
|
394
|
+
end
|
395
|
+
|
396
|
+
it "should set the ssl_client_cert if provided" do
|
397
|
+
@request = RestClient::Request.new(
|
398
|
+
:method => :put,
|
399
|
+
:url => 'https://some/resource',
|
400
|
+
:payload => 'payload',
|
401
|
+
:ssl_client_cert => "whatsupdoc!"
|
402
|
+
)
|
403
|
+
@net.should_receive(:cert=).with("whatsupdoc!")
|
404
|
+
@http.stub!(:request)
|
405
|
+
@request.stub!(:process_result)
|
406
|
+
@request.stub!(:response_log)
|
407
|
+
@request.transmit(@uri, 'req', 'payload')
|
408
|
+
end
|
409
|
+
|
410
|
+
it "should not set the ssl_client_cert if it is not provided" do
|
411
|
+
@request = RestClient::Request.new(
|
412
|
+
:method => :put,
|
413
|
+
:url => 'https://some/resource',
|
414
|
+
:payload => 'payload'
|
415
|
+
)
|
416
|
+
@net.should_not_receive(:cert=).with("whatsupdoc!")
|
417
|
+
@http.stub!(:request)
|
418
|
+
@request.stub!(:process_result)
|
419
|
+
@request.stub!(:response_log)
|
420
|
+
@request.transmit(@uri, 'req', 'payload')
|
421
|
+
end
|
422
|
+
|
423
|
+
it "should default to not having an ssl_client_key" do
|
424
|
+
@request.ssl_client_key.should be(nil)
|
425
|
+
end
|
426
|
+
|
427
|
+
it "should set the ssl_client_key if provided" do
|
428
|
+
@request = RestClient::Request.new(
|
429
|
+
:method => :put,
|
430
|
+
:url => 'https://some/resource',
|
431
|
+
:payload => 'payload',
|
432
|
+
:ssl_client_key => "whatsupdoc!"
|
433
|
+
)
|
434
|
+
@net.should_receive(:key=).with("whatsupdoc!")
|
435
|
+
@http.stub!(:request)
|
436
|
+
@request.stub!(:process_result)
|
437
|
+
@request.stub!(:response_log)
|
438
|
+
@request.transmit(@uri, 'req', 'payload')
|
439
|
+
end
|
440
|
+
|
441
|
+
it "should not set the ssl_client_key if it is not provided" do
|
442
|
+
@request = RestClient::Request.new(
|
443
|
+
:method => :put,
|
444
|
+
:url => 'https://some/resource',
|
445
|
+
:payload => 'payload'
|
446
|
+
)
|
447
|
+
@net.should_not_receive(:key=).with("whatsupdoc!")
|
448
|
+
@http.stub!(:request)
|
449
|
+
@request.stub!(:process_result)
|
450
|
+
@request.stub!(:response_log)
|
451
|
+
@request.transmit(@uri, 'req', 'payload')
|
452
|
+
end
|
453
|
+
|
454
|
+
it "should default to not having an ssl_ca_file" do
|
455
|
+
@request.ssl_ca_file.should be(nil)
|
456
|
+
end
|
457
|
+
|
458
|
+
it "should set the ssl_ca_file if provided" do
|
459
|
+
@request = RestClient::Request.new(
|
460
|
+
:method => :put,
|
461
|
+
:url => 'https://some/resource',
|
462
|
+
:payload => 'payload',
|
463
|
+
:ssl_ca_file => "Certificate Authority File"
|
464
|
+
)
|
465
|
+
@net.should_receive(:ca_file=).with("Certificate Authority File")
|
466
|
+
@http.stub!(:request)
|
467
|
+
@request.stub!(:process_result)
|
468
|
+
@request.stub!(:response_log)
|
469
|
+
@request.transmit(@uri, 'req', 'payload')
|
470
|
+
end
|
471
|
+
|
472
|
+
it "should not set the ssl_ca_file if it is not provided" do
|
473
|
+
@request = RestClient::Request.new(
|
474
|
+
:method => :put,
|
475
|
+
:url => 'https://some/resource',
|
476
|
+
:payload => 'payload'
|
477
|
+
)
|
478
|
+
@net.should_not_receive(:ca_file=).with("Certificate Authority File")
|
479
|
+
@http.stub!(:request)
|
480
|
+
@request.stub!(:process_result)
|
481
|
+
@request.stub!(:response_log)
|
482
|
+
@request.transmit(@uri, 'req', 'payload')
|
483
|
+
end
|
484
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Resource do
|
4
|
+
before do
|
5
|
+
@resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
|
6
|
+
end
|
7
|
+
|
8
|
+
context "Resource delegation" do
|
9
|
+
it "GET" do
|
10
|
+
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
11
|
+
@resource.get
|
12
|
+
end
|
13
|
+
|
14
|
+
it "POST" do
|
15
|
+
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
16
|
+
@resource.post 'abc', :content_type => 'image/jpg'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "PUT" do
|
20
|
+
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
21
|
+
@resource.put 'abc', :content_type => 'image/jpg'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "DELETE" do
|
25
|
+
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
26
|
+
@resource.delete
|
27
|
+
end
|
28
|
+
|
29
|
+
it "overrides resource headers" do
|
30
|
+
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
|
31
|
+
@resource.get 'X-Something' => '2'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "can instantiate with no user/password" do
|
36
|
+
@resource = RestClient::Resource.new('http://some/resource')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "is backwards compatible with previous constructor" do
|
40
|
+
@resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
|
41
|
+
@resource.user.should == 'user'
|
42
|
+
@resource.password.should == 'pass'
|
43
|
+
end
|
44
|
+
|
45
|
+
it "concatinates urls, inserting a slash when it needs one" do
|
46
|
+
@resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "concatinates urls, using no slash if the first url ends with a slash" do
|
50
|
+
@resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "concatinates urls, using no slash if the second url starts with a slash" do
|
54
|
+
@resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "concatinates even non-string urls, :posts + 1 => 'posts/1'" do
|
58
|
+
@resource.concat_urls(:posts, 1).should == 'posts/1'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "offers subresources via []" do
|
62
|
+
parent = RestClient::Resource.new('http://example.com')
|
63
|
+
parent['posts'].url.should == 'http://example.com/posts'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "transports options to subresources" do
|
67
|
+
parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
|
68
|
+
parent['posts'].user.should == 'user'
|
69
|
+
parent['posts'].password.should == 'password'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "prints its url with to_s" do
|
73
|
+
RestClient::Resource.new('x').to_s.should == 'x'
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Response do
|
4
|
+
before do
|
5
|
+
@net_http_res = mock('net http response')
|
6
|
+
@response = RestClient::Response.new('abc', @net_http_res)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "behaves like string" do
|
10
|
+
@response.should == 'abc'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "accepts nil strings and sets it to empty for the case of HEAD" do
|
14
|
+
RestClient::Response.new(nil, @net_http_res).should == ""
|
15
|
+
end
|
16
|
+
end
|