sevenwire-http_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/../base'
2
+
3
+ class MockResponse
4
+ include HttpClient::Mixin::Response
5
+
6
+ def initialize(body, res)
7
+ @net_http_res = res
8
+ @body = @body
9
+ end
10
+ end
11
+
12
+ describe HttpClient::Mixin::Response do
13
+ before do
14
+ @net_http_res = mock('net http response')
15
+ @response = MockResponse.new('abc', @net_http_res)
16
+ end
17
+
18
+ it "fetches the numeric response code" do
19
+ @net_http_res.should_receive(:code).and_return('200')
20
+ @response.code.should == 200
21
+ end
22
+
23
+ it "beautifies the headers by turning the keys to symbols" do
24
+ h = HttpClient::Response.beautify_headers('content-type' => [ 'x' ])
25
+ h.keys.first.should == :content_type
26
+ end
27
+
28
+ it "beautifies the headers by turning the values to strings instead of one-element arrays" do
29
+ h = HttpClient::Response.beautify_headers('x' => [ 'text/html' ] )
30
+ h.values.first.should == 'text/html'
31
+ end
32
+
33
+ it "fetches the headers" do
34
+ @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
35
+ @response.headers.should == { :content_type => 'text/html' }
36
+ end
37
+
38
+ it "extracts cookies from response headers" do
39
+ @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
40
+ @response.cookies.should == { 'session_id' => '1' }
41
+ end
42
+
43
+ it "can access the net http result directly" do
44
+ @response.net_http_res.should == @net_http_res
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe HttpClient::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 = HttpClient::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,431 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe HttpClient::Request do
4
+ before do
5
+ @request = HttpClient::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 "requests xml mimetype" do
21
+ @request.default_headers[:accept].should == 'application/xml'
22
+ end
23
+
24
+ it "decodes an uncompressed result body by passing it straight through" do
25
+ @request.decode(nil, 'xyz').should == 'xyz'
26
+ end
27
+
28
+ it "decodes a gzip body" do
29
+ @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
+ @request.decode('gzip', '').should be_empty
34
+ end
35
+
36
+ it "decodes a deflated body" do
37
+ @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 = HttpClient::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 = HttpClient::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
+ @request.make_headers('3' => '4').should == { '1' => '2', '3' => '4' }
97
+ end
98
+
99
+ it "prefers the user header when the same header exists in the defaults" do
100
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
101
+ @request.make_headers('1' => '3').should == { '1' => '3' }
102
+ end
103
+
104
+ it "converts header symbols from :content_type to 'Content-type'" do
105
+ @request.should_receive(:default_headers).and_return({})
106
+ @request.make_headers(:content_type => 'abc').should == { 'Content-type' => 'abc' }
107
+ end
108
+
109
+ it "converts header values to strings" do
110
+ @request.make_headers('A' => 1)['A'].should == '1'
111
+ end
112
+
113
+ it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
114
+ @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
115
+ klass = mock("net:http class")
116
+ @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
117
+ klass.should_receive(:new).and_return('result')
118
+ @request.should_receive(:transmit).with(@uri, 'result', 'payload')
119
+ @request.execute
120
+ end
121
+
122
+ it "transmits the request with Net::HTTP" do
123
+ @http.should_receive(:request).with('req', 'payload')
124
+ @request.should_receive(:process_result)
125
+ @request.should_receive(:response_log)
126
+ @request.transmit(@uri, 'req', 'payload')
127
+ end
128
+
129
+ it "uses SSL when the URI refers to a https address" do
130
+ @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
131
+ @net.should_receive(:use_ssl=).with(true)
132
+ @http.stub!(:request)
133
+ @request.stub!(:process_result)
134
+ @request.stub!(:response_log)
135
+ @request.transmit(@uri, 'req', 'payload')
136
+ end
137
+
138
+ it "sends nil payloads" do
139
+ @http.should_receive(:request).with('req', nil)
140
+ @request.should_receive(:process_result)
141
+ @request.stub!(:response_log)
142
+ @request.transmit(@uri, 'req', nil)
143
+ end
144
+
145
+ it "passes non-hash payloads straight through" do
146
+ @request.process_payload("x").should == "x"
147
+ end
148
+
149
+ it "converts a hash payload to urlencoded data" do
150
+ @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
151
+ end
152
+
153
+ it "accepts nested hashes in payload" do
154
+ payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
155
+ payload.should include('user[name]=joe')
156
+ payload.should include('user[location][country]=USA')
157
+ payload.should include('user[location][state]=CA')
158
+ end
159
+
160
+ it "set urlencoded content_type header on hash payloads" do
161
+ @request.process_payload(:a => 1)
162
+ @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
163
+ end
164
+
165
+ it "sets up the credentials prior to the request" do
166
+ @http.stub!(:request)
167
+ @request.stub!(:process_result)
168
+ @request.stub!(:response_log)
169
+
170
+ @request.stub!(:user).and_return('joe')
171
+ @request.stub!(:password).and_return('mypass')
172
+ @request.should_receive(:setup_credentials).with('req')
173
+
174
+ @request.transmit(@uri, 'req', nil)
175
+ end
176
+
177
+ it "does not attempt to send any credentials if user is nil" do
178
+ @request.stub!(:user).and_return(nil)
179
+ req = mock("request")
180
+ req.should_not_receive(:basic_auth)
181
+ @request.setup_credentials(req)
182
+ end
183
+
184
+ it "setup credentials when there's a user" do
185
+ @request.stub!(:user).and_return('joe')
186
+ @request.stub!(:password).and_return('mypass')
187
+ req = mock("request")
188
+ req.should_receive(:basic_auth).with('joe', 'mypass')
189
+ @request.setup_credentials(req)
190
+ end
191
+
192
+ it "catches EOFError and shows the more informative ServerBrokeConnection" do
193
+ @http.stub!(:request).and_raise(EOFError)
194
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(HttpClient::ServerBrokeConnection)
195
+ end
196
+
197
+ it "catches Errno::ECONNREFUSED and shows the more informative ConnectionRefused" do
198
+ @http.stub!(:request).and_raise(Errno::ECONNREFUSED)
199
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(HttpClient::ConnectionRefused)
200
+ end
201
+
202
+ it "class method execute wraps constructor" do
203
+ req = mock("http request")
204
+ HttpClient::Request.should_receive(:new).with(1 => 2).and_return(req)
205
+ req.should_receive(:execute)
206
+ HttpClient::Request.execute(1 => 2)
207
+ end
208
+
209
+ it "handles a 301" do
210
+ res = mock('response', :code => '301', :body => 'body', :[] => nil)
211
+ @request.process_result(res).should == res.body
212
+ end
213
+
214
+ it "handle a 401" do
215
+ res = mock('response', :code => '401', :body => 'body', :[] => nil)
216
+ @request.process_result(res).should == res.body
217
+ end
218
+
219
+ it "handle a 404" do
220
+ res = mock('response', :code => '404', :body => 'body', :[] => nil)
221
+ @request.process_result(res).should == res.body
222
+ end
223
+
224
+ it "handle a 500" do
225
+ res = mock('response', :code => '500', :body => 'body', :[] => nil)
226
+ @request.process_result(res).should == res.body
227
+ end
228
+
229
+ it "creates a proxy class if a proxy url is given" do
230
+ HttpClient.stub!(:proxy).and_return("http://example.com/")
231
+ @request.net_http_class.should include(Net::HTTP::ProxyDelta)
232
+ end
233
+
234
+ it "creates a non-proxy class if a proxy url is not given" do
235
+ @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
236
+ end
237
+
238
+ it "logs a get request" do
239
+ HttpClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
240
+ 'HttpClient.get "http://url"'
241
+ end
242
+
243
+ it "logs a post request with a small payload" do
244
+ HttpClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
245
+ 'HttpClient.post "http://url", "foo"'
246
+ end
247
+
248
+ it "logs a post request with a large payload" do
249
+ HttpClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
250
+ 'HttpClient.post "http://url", "(1000 byte payload)"'
251
+ end
252
+
253
+ it "logs input headers as a hash" do
254
+ HttpClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
255
+ 'HttpClient.get "http://url", :accept=>"text/plain"'
256
+ end
257
+
258
+ it "logs a response including the status code, content type, and result body size in bytes" do
259
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
260
+ res.stub!(:[]).with('Content-type').and_return('text/html')
261
+ @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
262
+ end
263
+
264
+ it "logs a response with a nil Content-type" do
265
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
266
+ res.stub!(:[]).with('Content-type').and_return(nil)
267
+ @request.response_log(res).should == "# => 200 OK | 4 bytes"
268
+ end
269
+
270
+ it "strips the charset from the response content type" do
271
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
272
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
273
+ @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
274
+ end
275
+
276
+ it "displays the log to stdout" do
277
+ HttpClient.stub!(:log).and_return('stdout')
278
+ STDOUT.should_receive(:puts).with('xyz')
279
+ @request.display_log('xyz')
280
+ end
281
+
282
+ it "displays the log to stderr" do
283
+ HttpClient.stub!(:log).and_return('stderr')
284
+ STDERR.should_receive(:puts).with('xyz')
285
+ @request.display_log('xyz')
286
+ end
287
+
288
+ it "append the log to the requested filename" do
289
+ HttpClient.stub!(:log).and_return('/tmp/http_client.log')
290
+ f = mock('file handle')
291
+ File.should_receive(:open).with('/tmp/http_client.log', 'a').and_yield(f)
292
+ f.should_receive(:puts).with('xyz')
293
+ @request.display_log('xyz')
294
+ end
295
+
296
+ it "set read_timeout" do
297
+ @request = HttpClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
298
+ @http.stub!(:request)
299
+ @request.stub!(:process_result)
300
+ @request.stub!(:response_log)
301
+
302
+ @net.should_receive(:read_timeout=).with(123)
303
+
304
+ @request.transmit(@uri, 'req', nil)
305
+ end
306
+
307
+ it "set open_timeout" do
308
+ @request = HttpClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
309
+ @http.stub!(:request)
310
+ @request.stub!(:process_result)
311
+ @request.stub!(:response_log)
312
+
313
+ @net.should_receive(:open_timeout=).with(123)
314
+
315
+ @request.transmit(@uri, 'req', nil)
316
+ end
317
+
318
+ it "should default to not verifying ssl certificates" do
319
+ @request.verify_ssl.should == false
320
+ end
321
+
322
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
323
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
324
+ @http.stub!(:request)
325
+ @request.stub!(:process_result)
326
+ @request.stub!(:response_log)
327
+ @request.transmit(@uri, 'req', 'payload')
328
+ end
329
+
330
+ it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
331
+ @request = HttpClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
332
+ @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
333
+ @http.stub!(:request)
334
+ @request.stub!(:process_result)
335
+ @request.stub!(:response_log)
336
+ @request.transmit(@uri, 'req', 'payload')
337
+ end
338
+
339
+ it "should default to not having an ssl_client_cert" do
340
+ @request.ssl_client_cert.should be(nil)
341
+ end
342
+
343
+ it "should set the ssl_client_cert if provided" do
344
+ @request = HttpClient::Request.new(
345
+ :method => :put,
346
+ :url => 'https://some/resource',
347
+ :payload => 'payload',
348
+ :ssl_client_cert => "whatsupdoc!"
349
+ )
350
+ @net.should_receive(:cert=).with("whatsupdoc!")
351
+ @http.stub!(:request)
352
+ @request.stub!(:process_result)
353
+ @request.stub!(:response_log)
354
+ @request.transmit(@uri, 'req', 'payload')
355
+ end
356
+
357
+ it "should not set the ssl_client_cert if it is not provided" do
358
+ @request = HttpClient::Request.new(
359
+ :method => :put,
360
+ :url => 'https://some/resource',
361
+ :payload => 'payload'
362
+ )
363
+ @net.should_not_receive(:cert=).with("whatsupdoc!")
364
+ @http.stub!(:request)
365
+ @request.stub!(:process_result)
366
+ @request.stub!(:response_log)
367
+ @request.transmit(@uri, 'req', 'payload')
368
+ end
369
+
370
+ it "should default to not having an ssl_client_key" do
371
+ @request.ssl_client_key.should be(nil)
372
+ end
373
+
374
+ it "should set the ssl_client_key if provided" do
375
+ @request = HttpClient::Request.new(
376
+ :method => :put,
377
+ :url => 'https://some/resource',
378
+ :payload => 'payload',
379
+ :ssl_client_key => "whatsupdoc!"
380
+ )
381
+ @net.should_receive(:key=).with("whatsupdoc!")
382
+ @http.stub!(:request)
383
+ @request.stub!(:process_result)
384
+ @request.stub!(:response_log)
385
+ @request.transmit(@uri, 'req', 'payload')
386
+ end
387
+
388
+ it "should not set the ssl_client_key if it is not provided" do
389
+ @request = HttpClient::Request.new(
390
+ :method => :put,
391
+ :url => 'https://some/resource',
392
+ :payload => 'payload'
393
+ )
394
+ @net.should_not_receive(:key=).with("whatsupdoc!")
395
+ @http.stub!(:request)
396
+ @request.stub!(:process_result)
397
+ @request.stub!(:response_log)
398
+ @request.transmit(@uri, 'req', 'payload')
399
+ end
400
+
401
+ it "should default to not having an ssl_ca_file" do
402
+ @request.ssl_ca_file.should be(nil)
403
+ end
404
+
405
+ it "should set the ssl_ca_file if provided" do
406
+ @request = HttpClient::Request.new(
407
+ :method => :put,
408
+ :url => 'https://some/resource',
409
+ :payload => 'payload',
410
+ :ssl_ca_file => "Certificate Authority File"
411
+ )
412
+ @net.should_receive(:ca_file=).with("Certificate Authority File")
413
+ @http.stub!(:request)
414
+ @request.stub!(:process_result)
415
+ @request.stub!(:response_log)
416
+ @request.transmit(@uri, 'req', 'payload')
417
+ end
418
+
419
+ it "should not set the ssl_ca_file if it is not provided" do
420
+ @request = HttpClient::Request.new(
421
+ :method => :put,
422
+ :url => 'https://some/resource',
423
+ :payload => 'payload'
424
+ )
425
+ @net.should_not_receive(:ca_file=).with("Certificate Authority File")
426
+ @http.stub!(:request)
427
+ @request.stub!(:process_result)
428
+ @request.stub!(:response_log)
429
+ @request.transmit(@uri, 'req', 'payload')
430
+ end
431
+ end