typhoeus 0.1.31 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,76 +30,6 @@ describe Typhoeus::Hydra do
30
30
  Typhoeus::Hydra.hydra = Typhoeus::Hydra.new
31
31
  end
32
32
 
33
- context "#stub" do
34
- before do
35
- @hydra = Typhoeus::Hydra.new
36
- @on_complete_handler_called = nil
37
- @request = Typhoeus::Request.new("http://localhost:3000/foo")
38
- @request.on_complete do |response|
39
- @on_complete_handler_called = true
40
- response.code.should == 404
41
- response.headers.should == "whatever"
42
- end
43
- @response = Typhoeus::Response.new(:code => 404, :headers => "whatever", :body => "not found", :time => 0.1)
44
- end
45
-
46
- it "stubs requests to a specific URI" do
47
- @hydra.stub(:get, "http://localhost:3000/foo").and_return(@response)
48
- @hydra.queue(@request)
49
- @hydra.run
50
- @on_complete_handler_called.should be_true
51
- @response.request.should == @request
52
- end
53
-
54
- it "stubs requests to URIs matching a pattern" do
55
- @hydra.stub(:get, /foo/).and_return(@response)
56
- @hydra.queue(@request)
57
- @hydra.run
58
- @on_complete_handler_called.should be_true
59
- @response.request.should == @request
60
- end
61
-
62
- it "can clear stubs" do
63
- @hydra.clear_stubs
64
- end
65
-
66
- it "clears out previously queued requests once they are called" do
67
- @hydra.stub(:get, "asdf").and_return(@response)
68
-
69
- call_count = 0
70
- request = Typhoeus::Request.new("asdf")
71
- request.on_complete do |response|
72
- call_count += 1
73
- end
74
- @hydra.queue(request)
75
- @hydra.run
76
- call_count.should == 1
77
- @hydra.run
78
- call_count.should == 1
79
- end
80
-
81
- it "calls stubs for requests that are queued up in the on_complete of a first stub" do
82
- @hydra.stub(:get, "asdf").and_return(@response)
83
- @hydra.stub(:get, "bar").and_return(@response)
84
-
85
- second_handler_called = false
86
- request = Typhoeus::Request.new("asdf")
87
- request.on_complete do |response|
88
- r = Typhoeus::Request.new("bar")
89
- r.on_complete do |res|
90
- second_handler_called = true
91
- end
92
- @hydra.queue(r)
93
- end
94
- @hydra.queue(request)
95
- @hydra.run
96
-
97
- second_handler_called.should be_true
98
- end
99
-
100
- it "matches a stub only when the HTTP method also matches"
101
- end
102
-
103
33
  it "queues a request" do
104
34
  hydra = Typhoeus::Hydra.new
105
35
  hydra.queue Typhoeus::Request.new("http://localhost:3000")
@@ -309,3 +239,218 @@ describe Typhoeus::Hydra do
309
239
  request.response.code.should == 302
310
240
  end
311
241
  end
242
+
243
+ describe Typhoeus::Hydra::Stubbing do
244
+ shared_examples_for "any stubbable target" do
245
+ before(:each) do
246
+ @on_complete_handler_called = nil
247
+ @request = Typhoeus::Request.new("http://localhost:3000/foo",
248
+ :user_agent => 'test')
249
+ @request.on_complete do |response|
250
+ @on_complete_handler_called = true
251
+ response.code.should == 404
252
+ response.headers.should == "whatever"
253
+ end
254
+ @response = Typhoeus::Response.new(:code => 404,
255
+ :headers => "whatever",
256
+ :body => "not found",
257
+ :time => 0.1)
258
+ end
259
+
260
+ after(:each) do
261
+ @stub_target.clear_stubs
262
+ end
263
+
264
+ it "should provide a stubs accessor" do
265
+ begin
266
+ @stub_target.stubs.should == []
267
+ @stub_target.stubs = [:foo]
268
+ ensure
269
+ @stub_target.clear_stubs
270
+ end
271
+ end
272
+
273
+ it "stubs requests to a specific URI" do
274
+ @stub_target.stub(:get, "http://localhost:3000/foo",
275
+ :headers => { 'user-agent' => 'test'}).
276
+ and_return(@response)
277
+
278
+ @hydra.queue(@request)
279
+ @hydra.run
280
+ @on_complete_handler_called.should be_true
281
+ @response.request.should == @request
282
+ end
283
+
284
+ it "stubs requests to URIs matching a pattern" do
285
+ @stub_target.stub(:get, /foo/,
286
+ :headers => { 'user-agent' => 'test' }).
287
+ and_return(@response)
288
+ @hydra.queue(@request)
289
+ @hydra.run
290
+ @on_complete_handler_called.should be_true
291
+ @response.request.should == @request
292
+ end
293
+
294
+ it "can clear stubs" do
295
+ @stub_target.clear_stubs
296
+ end
297
+
298
+ it "clears out previously queued requests once they are called" do
299
+ @stub_target.stub(:get, "http://localhost:3000/asdf",
300
+ :headers => { 'user-agent' => 'test' }).
301
+ and_return(@response)
302
+
303
+ call_count = 0
304
+ request = Typhoeus::Request.new("http://localhost:3000/asdf", :user_agent => 'test')
305
+ request.on_complete do |response|
306
+ call_count += 1
307
+ end
308
+ @hydra.queue(request)
309
+ @hydra.run
310
+ call_count.should == 1
311
+ @hydra.run
312
+ call_count.should == 1
313
+ end
314
+
315
+ it "calls stubs for requests that are queued up in the on_complete of a first stub" do
316
+ @stub_target.stub(:get, "http://localhost:3000/asdf").and_return(@response)
317
+ @stub_target.stub(:get, "http://localhost:3000/bar").and_return(@response)
318
+
319
+ second_handler_called = false
320
+ request = Typhoeus::Request.new("http://localhost:3000/asdf")
321
+ request.on_complete do |response|
322
+ r = Typhoeus::Request.new("http://localhost:3000/bar")
323
+ r.on_complete do |res|
324
+ second_handler_called = true
325
+ end
326
+ @hydra.queue(r)
327
+ end
328
+ @hydra.queue(request)
329
+ @hydra.run
330
+
331
+ second_handler_called.should be_true
332
+ end
333
+ end
334
+
335
+ describe "global (class-level) stubbing" do
336
+ before(:each) do
337
+ @hydra = Typhoeus::Hydra.new
338
+ @stub_target = Typhoeus::Hydra
339
+ end
340
+
341
+ it_should_behave_like "any stubbable target"
342
+ end
343
+
344
+ describe "instance stubbing" do
345
+ before(:each) do
346
+ @hydra = Typhoeus::Hydra.new
347
+ @stub_target = @hydra
348
+ end
349
+
350
+ it_should_behave_like "any stubbable target"
351
+ end
352
+ end
353
+
354
+ describe Typhoeus::Hydra::Callbacks do
355
+ before(:all) do
356
+ @klass = Typhoeus::Hydra
357
+ end
358
+
359
+ describe "#after_request_before_on_complete" do
360
+ it "should provide a global hook after a request" do
361
+ begin
362
+ http_method = nil
363
+ @klass.after_request_before_on_complete do |request|
364
+ http_method = request.method
365
+ end
366
+
367
+ hydra = @klass.new
368
+ request = Typhoeus::Request.new('http://localhost:3000',
369
+ :method => :get)
370
+ response = Typhoeus::Response.new(:code => 404,
371
+ :headers => "whatever",
372
+ :body => "not found",
373
+ :time => 0.1)
374
+ hydra.stub(:get, 'http://localhost:3000').
375
+ and_return(response)
376
+
377
+ hydra.queue(request)
378
+ hydra.run
379
+
380
+ http_method.should == :get
381
+ ensure
382
+ @klass.clear_global_hooks
383
+ end
384
+ end
385
+ end
386
+ end
387
+
388
+ describe Typhoeus::Hydra::ConnectOptions do
389
+ before(:all) do
390
+ @klass = Typhoeus::Hydra
391
+ end
392
+
393
+ describe "#ignore_localhost" do
394
+ it "should allow localhost requests over the wire if true" do
395
+ begin
396
+ old_net_connect = @klass.allow_net_connect
397
+ old_ignore_localhost = @klass.ignore_localhost
398
+ @klass.allow_net_connect = false
399
+ @klass.ignore_localhost = true
400
+
401
+ req = Typhoeus::Request.new("http://localhost:3000")
402
+ hydra = @klass.new
403
+
404
+ lambda {
405
+ hydra.queue(req)
406
+ }.should_not raise_error
407
+ ensure
408
+ @klass.allow_net_connect = old_net_connect
409
+ @klass.ignore_localhost = old_ignore_localhost
410
+ end
411
+ end
412
+ end
413
+
414
+ describe "#allow_net_connect" do
415
+ it "should be settable" do
416
+ begin
417
+ # make sure to not mess up other specs.
418
+ old = @klass.allow_net_connect
419
+ @klass.allow_net_connect = true
420
+ @klass.allow_net_connect.should be_true
421
+ ensure
422
+ @klass.allow_net_connect = old
423
+ end
424
+ end
425
+
426
+ it "should default to true" do
427
+ @klass.allow_net_connect.should be_true
428
+ end
429
+
430
+ it "should raise an error if we queue a request while its false" do
431
+ begin
432
+ request = Typhoeus::Request.new("http://localhost:3000")
433
+ old_net_connect = @klass.allow_net_connect
434
+ old_ignore_localhost = @klass.ignore_localhost
435
+
436
+ @klass.allow_net_connect = false
437
+ @klass.ignore_localhost = false
438
+
439
+ hydra = Typhoeus::Hydra.new
440
+
441
+ lambda {
442
+ hydra.queue(request)
443
+ }.should raise_error(Typhoeus::Hydra::NetConnectNotAllowedError)
444
+ ensure
445
+ @klass.allow_net_connect = old_net_connect
446
+ @klass.ignore_localhost = old_ignore_localhost
447
+ end
448
+ end
449
+ end
450
+
451
+ describe "#allow_net_connect?" do
452
+ it "should return true by default" do
453
+ @klass.allow_net_connect?.should be_true
454
+ end
455
+ end
456
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Typhoeus::NormalizedHeaderHash do
4
+ before(:all) do
5
+ @klass = Typhoeus::NormalizedHeaderHash
6
+ end
7
+
8
+ it "should normalize keys on assignment" do
9
+ hash = @klass.new
10
+ hash['Content-Type'] = 'text/html'
11
+ hash['content-type'].should == 'text/html'
12
+ hash[:content_type].should == 'text/html'
13
+ hash['Accepts'] = 'text/javascript'
14
+ hash['accepts'].should == 'text/javascript'
15
+ end
16
+
17
+ it "should normalize the keys on instantiation" do
18
+ hash = @klass.new('Content-Type' => 'text/html', :x_http_header => 'foo', 'X-HTTP-USER' => 'bar')
19
+ hash.keys.should =~ ['Content-Type', 'X-Http-Header', 'X-Http-User']
20
+ end
21
+
22
+ it "should merge keys correctly" do
23
+ hash = @klass.new
24
+ hash.merge!('Content-Type' => 'fdsa')
25
+ hash['content-type'].should == 'fdsa'
26
+ end
27
+
28
+ it "should allow any casing of keys" do
29
+ hash = @klass.new
30
+ hash['Content-Type'] = 'fdsa'
31
+ hash['content-type'].should == 'fdsa'
32
+ hash['cOnTent-TYPE'].should == 'fdsa'
33
+ hash['Content-Type'].should == 'fdsa'
34
+ end
35
+
36
+ it "should support has_key?" do
37
+ hash = @klass.new
38
+ hash['Content-Type'] = 'fdsa'
39
+ hash.has_key?('cOntent-Type').should be_true
40
+ end
41
+ end
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Typhoeus do
4
4
  it "should be deprecated" do
5
- fail "This entire interface is deprecated!"
5
+ pending "This entire interface is deprecated!"
6
6
  end
7
7
 
8
8
  # before(:each) do
@@ -1,6 +1,50 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Typhoeus::Request do
4
+ describe "#inspect" do
5
+ before(:each) do
6
+ @request = Typhoeus::Request.new('http://www.google.com/',
7
+ :body => "a=1&b=2",
8
+ :params => { :c => 'ok' },
9
+ :method => :get,
10
+ :headers => { 'Content-Type' => 'text/html' })
11
+ end
12
+
13
+ it "should dump out the URI" do
14
+ @request.inspect.should =~ /http:\/\/www\.google\.com/
15
+ end
16
+
17
+ it "should dump out the body" do
18
+ @request.inspect.should =~ /a=1&b=2/
19
+ end
20
+
21
+ it "should dump params" do
22
+ @request.inspect.should =~ /:c\s*=>\s*"ok"/
23
+ end
24
+
25
+ it "should dump the method" do
26
+ @request.inspect.should =~ /:get/
27
+ end
28
+
29
+ it "should dump out headers" do
30
+ @request.inspect.should =~ /"Content-Type"\s*=>\s*"text\/html"/
31
+ end
32
+ end
33
+
34
+ describe "#localhost?" do
35
+ %w(localhost 127.0.0.1 0.0.0.0).each do |host|
36
+ it "should be true for the #{host} host" do
37
+ req = Typhoeus::Request.new("http://#{host}")
38
+ req.should be_localhost
39
+ end
40
+ end
41
+
42
+ it "should be false for other domains" do
43
+ req = Typhoeus::Request.new("http://google.com")
44
+ req.should_not be_localhost
45
+ end
46
+ end
47
+
4
48
  describe "#params_string" do
5
49
  it "should dump a sorted string" do
6
50
  request = Typhoeus::Request.new(
@@ -24,6 +68,14 @@ describe Typhoeus::Request do
24
68
  })
25
69
  request.params_string.should == "a=jlk&b=fdsa&c=789"
26
70
  end
71
+
72
+ it "should translate params with values that are arrays to the proper format" do
73
+ request = Typhoeus::Request.new('http://google.com',
74
+ :params => {
75
+ :a => ['789','2434']
76
+ })
77
+ request.params_string.should == "a[]=789&a[]=2434"
78
+ end
27
79
  end
28
80
 
29
81
  describe "quick request methods" do
@@ -160,9 +212,9 @@ describe Typhoeus::Request do
160
212
  request.call_handlers
161
213
  good.should be_true
162
214
  end
163
-
215
+
164
216
  describe "authentication" do
165
-
217
+
166
218
  it "should allow to set username and password" do
167
219
  auth = { :username => 'foo', :password => 'bar' }
168
220
  e = Typhoeus::Request.get(
@@ -2,9 +2,35 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Typhoeus::Response do
4
4
  describe "initialize" do
5
+ it "should store headers_hash" do
6
+ response = Typhoeus::Response.new(:headers_hash => {})
7
+ response.headers_hash.should == {}
8
+ end
9
+
10
+ it "allows header access using a different casing of the header key" do
11
+ response = Typhoeus::Response.new(:headers_hash => { 'content-type' => 'text/html' } )
12
+ response.headers_hash['Content-Type'].should == 'text/html'
13
+ end
14
+
5
15
  it "should store response_code" do
6
16
  Typhoeus::Response.new(:code => 200).code.should == 200
7
17
  end
18
+
19
+ it "should store status_message" do
20
+ Typhoeus::Response.new(:status_message => 'Not Found').status_message.should == 'Not Found'
21
+ end
22
+
23
+ it "should return nil for status_message if none is given and no header is given" do
24
+ Typhoeus::Response.new.status_message.should be_nil
25
+ end
26
+
27
+ it "should store http_version" do
28
+ Typhoeus::Response.new(:http_version => '1.1').http_version.should == '1.1'
29
+ end
30
+
31
+ it "should return nil for http version if none is given and no header is given" do
32
+ Typhoeus::Response.new.http_version.should be_nil
33
+ end
8
34
 
9
35
  it "should store response_headers" do
10
36
  Typhoeus::Response.new(:headers => "a header!").headers.should == "a header!"
@@ -27,34 +53,84 @@ describe Typhoeus::Response do
27
53
  response = Typhoeus::Response.new(:requested_http_method => :delete)
28
54
  response.requested_http_method.should == :delete
29
55
  end
30
-
56
+
31
57
  it "should store an associated request object" do
32
58
  response = Typhoeus::Response.new(:request => "whatever")
33
59
  response.request.should == "whatever"
34
60
  end
61
+
62
+ it "should not default to be a mock response" do
63
+ response = Typhoeus::Response.new
64
+ response.should_not be_mock
65
+ end
35
66
  end
36
-
67
+
68
+ describe "#mock?" do
69
+ it "should be true if it's a mock response" do
70
+ response = Typhoeus::Response.new(:mock => true)
71
+ response.should be_mock
72
+ end
73
+ end
74
+
37
75
  describe "headers" do
38
- it "can parse the headers into a hash" do
39
- response = Typhoeus::Response.new(:headers => "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\nStatus: 200\r\nX-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.9\r\nX-Cache: miss\r\nX-Runtime: 184\r\nETag: e001d08d9354ab7bc7c27a00163a3afa\r\nCache-Control: private, max-age=0, must-revalidate\r\nContent-Length: 4725\r\nSet-Cookie: _some_session=BAh7CDoGciIAOg9zZXNzaW9uX2lkIiU1OTQ2OTcwMjljMWM5ZTQwODU1NjQwYTViMmQxMTkxMjoGcyIKL2NhcnQ%3D--b4c4663932243090c961bb93d4ad5e4327064730; path=/; HttpOnly\r\nServer: nginx/0.6.37 + Phusion Passenger 2.2.4 (mod_rails/mod_rack)\r\nSet-Cookie: foo=bar; path=/;\r\nP3P: CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"\r\n\r\n")
40
- response.headers_hash["Status"].should == "200"
41
- response.headers_hash["Set-Cookie"].should == ["_some_session=BAh7CDoGciIAOg9zZXNzaW9uX2lkIiU1OTQ2OTcwMjljMWM5ZTQwODU1NjQwYTViMmQxMTkxMjoGcyIKL2NhcnQ%3D--b4c4663932243090c961bb93d4ad5e4327064730; path=/; HttpOnly", "foo=bar; path=/;"]
42
- response.headers_hash["Content-Type"].should == "text/html; charset=utf-8"
76
+ it 'should return an empty hash from #headers_hash when no headers string is given' do
77
+ response = Typhoeus::Response.new.headers_hash.should == {}
43
78
  end
44
-
79
+
80
+ describe "basic parsing" do
81
+ before(:all) do
82
+ @response = Typhoeus::Response.new(:headers => "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\nStatus: 200\r\nX-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.9\r\nX-Cache: miss\r\nX-Runtime: 184\r\nETag: e001d08d9354ab7bc7c27a00163a3afa\r\nCache-Control: private, max-age=0, must-revalidate\r\nContent-Length: 4725\r\nSet-Cookie: _some_session=BAh7CDoGciIAOg9zZXNzaW9uX2lkIiU1OTQ2OTcwMjljMWM5ZTQwODU1NjQwYTViMmQxMTkxMjoGcyIKL2NhcnQ%3D--b4c4663932243090c961bb93d4ad5e4327064730; path=/; HttpOnly\r\nServer: nginx/0.6.37 + Phusion Passenger 2.2.4 (mod_rails/mod_rack)\r\nSet-Cookie: foo=bar; path=/;\r\nP3P: CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"\r\n\r\n")
83
+ end
84
+
85
+ it "can be accessed with lowercase keys" do
86
+ @response.headers_hash['content-type'].should == 'text/html; charset=utf-8'
87
+ end
88
+
89
+ it "can parse the headers into a hash" do
90
+ @response.headers_hash["Status"].should == "200"
91
+ @response.headers_hash["Set-Cookie"].should == ["_some_session=BAh7CDoGciIAOg9zZXNzaW9uX2lkIiU1OTQ2OTcwMjljMWM5ZTQwODU1NjQwYTViMmQxMTkxMjoGcyIKL2NhcnQ%3D--b4c4663932243090c961bb93d4ad5e4327064730; path=/; HttpOnly", "foo=bar; path=/;"]
92
+ @response.headers_hash["Content-Type"].should == "text/html; charset=utf-8"
93
+ end
94
+
95
+ it 'parses the status message' do
96
+ @response.status_message.should == 'OK'
97
+ end
98
+
99
+ it 'parses the HTTP version' do
100
+ @response.http_version.should == '1.1'
101
+ end
102
+
103
+ it 'parses all header keys except HTTP version declaration' do
104
+ @response.headers_hash.keys.should =~ %w[
105
+ X-Powered-By
106
+ P3p
107
+ X-Cache
108
+ Etag
109
+ X-Runtime
110
+ Content-Type
111
+ Content-Length
112
+ Server
113
+ Set-Cookie
114
+ Cache-Control
115
+ Connection
116
+ Status
117
+ ]
118
+ end
119
+ end
120
+
45
121
  it "parses a header key that appears multiple times into an array" do
46
122
  response = Typhoeus::Response.new(:headers => "HTTP/1.1 302 Found\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\nStatus: 302\r\nX-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.9\r\nLocation: http://mckenzie-greenholt1512.myshopify.com/cart\r\nX-Runtime: 22\r\nCache-Control: no-cache\r\nContent-Length: 114\r\nSet-Cookie: cart=8fdd6a828d9c89a737a52668be0cebaf; path=/; expires=Fri, 12-Mar-2010 18:30:19 GMT\r\nSet-Cookie: _session=BAh7CToPc2Vzc2lvbl9pZCIlZTQzMDQzMDg1YjI3MTQ4MzAzMTZmMWZmMWJjMTU1NmI6CWNhcnQiJThmZGQ2YTgyOGQ5Yzg5YTczN2E1MjY2OGJlMGNlYmFmOgZyIgA6BnMiDi9jYXJ0L2FkZA%3D%3D--6b0a699625caed9597580d8e9b6ca5f5e5954125; path=/; HttpOnly\r\nServer: nginx/0.6.37 + Phusion Passenger 2.2.4 (mod_rails/mod_rack)\r\nP3P: CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"\r\n\r\n")
47
123
  response.headers_hash["Set-Cookie"].should include("cart=8fdd6a828d9c89a737a52668be0cebaf; path=/; expires=Fri, 12-Mar-2010 18:30:19 GMT")
48
124
  response.headers_hash["Set-Cookie"].should include("_session=BAh7CToPc2Vzc2lvbl9pZCIlZTQzMDQzMDg1YjI3MTQ4MzAzMTZmMWZmMWJjMTU1NmI6CWNhcnQiJThmZGQ2YTgyOGQ5Yzg5YTczN2E1MjY2OGJlMGNlYmFmOgZyIgA6BnMiDi9jYXJ0L2FkZA%3D%3D--6b0a699625caed9597580d8e9b6ca5f5e5954125; path=/; HttpOnly")
49
125
  end
50
126
  end
51
-
127
+
52
128
  describe "status checking" do
53
129
  it "is successful if response code is 200-299" do
54
130
  Typhoeus::Response.new(:code => 220).success?.should be
55
131
  Typhoeus::Response.new(:code => 400).success?.should_not be
56
132
  end
57
-
133
+
58
134
  it "is not modified if the status code is 304" do
59
135
  Typhoeus::Response.new(:code => 304).modified?.should_not be
60
136
  Typhoeus::Response.new(:code => 200).modified?.should be