forked from typhoeus/typhoeus
/
hydra_spec.rb
456 lines (392 loc) · 13.5 KB
/
hydra_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
# some of these tests assume that you have some local services running.
# ruby spec/servers/app.rb -p 3000
# ruby spec/servers/app.rb -p 3001
# ruby spec/servers/app.rb -p 3002
describe Typhoeus::Hydra do
before(:all) do
cache_class = Class.new do
def initialize
@cache = {}
end
def get(key)
@cache[key]
end
def set(key, object, timeout = 0)
@cache[key] = object
end
end
@cache = cache_class.new
end
it "has a singleton" do
Typhoeus::Hydra.hydra.should be_a Typhoeus::Hydra
end
it "has a setter for the singleton" do
Typhoeus::Hydra.hydra = :foo
Typhoeus::Hydra.hydra.should == :foo
Typhoeus::Hydra.hydra = Typhoeus::Hydra.new
end
it "queues a request" do
hydra = Typhoeus::Hydra.new
hydra.queue Typhoeus::Request.new("http://localhost:3000")
end
it "runs a batch of requests" do
hydra = Typhoeus::Hydra.new
first = Typhoeus::Request.new("http://localhost:3000/first")
second = Typhoeus::Request.new("http://localhost:3001/second")
hydra.queue first
hydra.queue second
hydra.run
first.response.body.should include("first")
second.response.body.should include("second")
end
it "has a cache_setter proc" do
hydra = Typhoeus::Hydra.new
hydra.cache_setter do |request|
# @cache.set(request.cache_key, request.response, request.cache_timeout)
end
end
it "has a cache_getter" do
hydra = Typhoeus::Hydra.new
hydra.cache_getter do |request|
# @cache.get(request.cache_key) rescue nil
end
end
it "memoizes GET reqeusts" do
hydra = Typhoeus::Hydra.new
first = Typhoeus::Request.new("http://localhost:3000/foo", :params => {:delay => 1})
second = Typhoeus::Request.new("http://localhost:3000/foo", :params => {:delay => 1})
hydra.queue first
hydra.queue second
start_time = Time.now
hydra.run
first.response.body.should include("foo")
first.handled_response.body.should include("foo")
first.response.should == second.response
first.handled_response.should == second.handled_response
(Time.now - start_time).should < 1.2 # if it had run twice it would be ~ 2 seconds
end
it "can turn off memoization for GET requests" do
hydra = Typhoeus::Hydra.new
hydra.disable_memoization
first = Typhoeus::Request.new("http://localhost:3000/foo")
second = Typhoeus::Request.new("http://localhost:3000/foo")
hydra.queue first
hydra.queue second
hydra.run
first.response.body.should include("foo")
first.response.object_id.should_not == second.response.object_id
end
it "pulls GETs from cache" do
hydra = Typhoeus::Hydra.new
start_time = Time.now
hydra.cache_getter do |request|
@cache.get(request.cache_key) rescue nil
end
hydra.cache_setter do |request|
@cache.set(request.cache_key, request.response, request.cache_timeout)
end
first = Typhoeus::Request.new("http://localhost:3000/foo", :params => {:delay => 1})
@cache.set(first.cache_key, :foo, 60)
hydra.queue first
hydra.run
(Time.now - start_time).should < 0.1
first.response.should == :foo
end
it "sets GET responses to cache when the request has a cache_timeout value" do
hydra = Typhoeus::Hydra.new
hydra.cache_getter do |request|
@cache.get(request.cache_key) rescue nil
end
hydra.cache_setter do |request|
@cache.set(request.cache_key, request.response, request.cache_timeout)
end
first = Typhoeus::Request.new("http://localhost:3000/first", :cache_timeout => 0)
second = Typhoeus::Request.new("http://localhost:3000/second")
hydra.queue first
hydra.queue second
hydra.run
first.response.body.should include("first")
@cache.get(first.cache_key).should == first.response
@cache.get(second.cache_key).should be_nil
end
it "has a global on_complete" do
foo = nil
hydra = Typhoeus::Hydra.new
hydra.on_complete do |response|
foo = :called
end
first = Typhoeus::Request.new("http://localhost:3000/first")
hydra.queue first
hydra.run
first.response.body.should include("first")
foo.should == :called
end
it "has a global on_complete setter" do
foo = nil
hydra = Typhoeus::Hydra.new
proc = Proc.new {|response| foo = :called}
hydra.on_complete = proc
first = Typhoeus::Request.new("http://localhost:3000/first")
hydra.queue first
hydra.run
first.response.body.should include("first")
foo.should == :called
end
it "should reuse connections from the pool for a host"
it "should queue up requests while others are running" do
hydra = Typhoeus::Hydra.new
start_time = Time.now
@responses = []
request = Typhoeus::Request.new("http://localhost:3000/first", :params => {:delay => 1})
request.on_complete do |response|
@responses << response
response.body.should include("first")
end
request.after_complete do |object|
second_request = Typhoeus::Request.new("http://localhost:3001/second", :params => {:delay => 2})
second_request.on_complete do |response|
@responses << response
response.body.should include("second")
end
hydra.queue second_request
end
hydra.queue request
third_request = Typhoeus::Request.new("http://localhost:3002/third", :params => {:delay => 3})
third_request.on_complete do |response|
@responses << response
response.body.should include("third")
end
hydra.queue third_request
hydra.run
@responses.size.should == 3
(Time.now - start_time).should < 3.3
end
it "should fire and forget" do
# this test is totally hacky. I have no clue how to make it verify. I just look at the test servers
# to verify that stuff is running
hydra = Typhoeus::Hydra.new
first = Typhoeus::Request.new("http://localhost:3000/first?delay=1")
second = Typhoeus::Request.new("http://localhost:3001/second?delay=2")
hydra.queue first
hydra.queue second
hydra.fire_and_forget
third = Typhoeus::Request.new("http://localhost:3002/third?delay=3")
hydra.queue third
hydra.fire_and_forget
sleep 3 # have to do this or future tests may break.
end
it "should take the maximum number of concurrent requests as an argument" do
hydra = Typhoeus::Hydra.new(:max_concurrency => 2)
first = Typhoeus::Request.new("http://localhost:3000/first?delay=1")
second = Typhoeus::Request.new("http://localhost:3001/second?delay=1")
third = Typhoeus::Request.new("http://localhost:3002/third?delay=1")
hydra.queue first
hydra.queue second
hydra.queue third
start_time = Time.now
hydra.run
finish_time = Time.now
first.response.code.should == 200
second.response.code.should == 200
third.response.code.should == 200
(finish_time - start_time).should > 2.0
end
it "should respect the follow_location option when set on a request" do
hydra = Typhoeus::Hydra.new
request = Typhoeus::Request.new("http://localhost:3000/redirect", :follow_location => true)
hydra.queue request
hydra.run
request.response.code.should == 200
end
it "should pass through the max_redirects option when set on a request" do
hydra = Typhoeus::Hydra.new
request = Typhoeus::Request.new("http://localhost:3000/bad_redirect", :max_redirects => 5)
hydra.queue request
hydra.run
request.response.code.should == 302
end
end
describe Typhoeus::Hydra::Stubbing do
shared_examples_for "any stubbable target" do
before(:each) do
@on_complete_handler_called = nil
@request = Typhoeus::Request.new("http://localhost:3000/foo",
:user_agent => 'test')
@request.on_complete do |response|
@on_complete_handler_called = true
response.code.should == 404
response.headers.should == "whatever"
end
@response = Typhoeus::Response.new(:code => 404,
:headers => "whatever",
:body => "not found",
:time => 0.1)
end
after(:each) do
@stub_target.clear_stubs
end
it "should provide a stubs accessor" do
begin
@stub_target.stubs.should == []
@stub_target.stubs = [:foo]
ensure
@stub_target.clear_stubs
end
end
it "stubs requests to a specific URI" do
@stub_target.stub(:get, "http://localhost:3000/foo",
:headers => { 'user-agent' => 'test'}).
and_return(@response)
@hydra.queue(@request)
@hydra.run
@on_complete_handler_called.should be_true
@response.request.should == @request
end
it "stubs requests to URIs matching a pattern" do
@stub_target.stub(:get, /foo/,
:headers => { 'user-agent' => 'test' }).
and_return(@response)
@hydra.queue(@request)
@hydra.run
@on_complete_handler_called.should be_true
@response.request.should == @request
end
it "can clear stubs" do
@stub_target.clear_stubs
end
it "clears out previously queued requests once they are called" do
@stub_target.stub(:get, "http://localhost:3000/asdf",
:headers => { 'user-agent' => 'test' }).
and_return(@response)
call_count = 0
request = Typhoeus::Request.new("http://localhost:3000/asdf", :user_agent => 'test')
request.on_complete do |response|
call_count += 1
end
@hydra.queue(request)
@hydra.run
call_count.should == 1
@hydra.run
call_count.should == 1
end
it "calls stubs for requests that are queued up in the on_complete of a first stub" do
@stub_target.stub(:get, "http://localhost:3000/asdf").and_return(@response)
@stub_target.stub(:get, "http://localhost:3000/bar").and_return(@response)
second_handler_called = false
request = Typhoeus::Request.new("http://localhost:3000/asdf")
request.on_complete do |response|
r = Typhoeus::Request.new("http://localhost:3000/bar")
r.on_complete do |res|
second_handler_called = true
end
@hydra.queue(r)
end
@hydra.queue(request)
@hydra.run
second_handler_called.should be_true
end
end
describe "global (class-level) stubbing" do
before(:each) do
@hydra = Typhoeus::Hydra.new
@stub_target = Typhoeus::Hydra
end
it_should_behave_like "any stubbable target"
end
describe "instance stubbing" do
before(:each) do
@hydra = Typhoeus::Hydra.new
@stub_target = @hydra
end
it_should_behave_like "any stubbable target"
end
end
describe Typhoeus::Hydra::Callbacks do
before(:all) do
@klass = Typhoeus::Hydra
end
describe "#after_request_before_on_complete" do
it "should provide a global hook after a request" do
begin
http_method = nil
@klass.after_request_before_on_complete do |request|
http_method = request.method
end
hydra = @klass.new
request = Typhoeus::Request.new('http://localhost:3000',
:method => :get)
response = Typhoeus::Response.new(:code => 404,
:headers => "whatever",
:body => "not found",
:time => 0.1)
hydra.stub(:get, 'http://localhost:3000').
and_return(response)
hydra.queue(request)
hydra.run
http_method.should == :get
ensure
@klass.clear_global_hooks
end
end
end
end
describe Typhoeus::Hydra::ConnectOptions do
before(:all) do
@klass = Typhoeus::Hydra
end
describe "#ignore_localhost" do
it "should allow localhost requests over the wire if true" do
begin
old_net_connect = @klass.allow_net_connect
old_ignore_localhost = @klass.ignore_localhost
@klass.allow_net_connect = false
@klass.ignore_localhost = true
req = Typhoeus::Request.new("http://localhost:3000")
hydra = @klass.new
lambda {
hydra.queue(req)
}.should_not raise_error
ensure
@klass.allow_net_connect = old_net_connect
@klass.ignore_localhost = old_ignore_localhost
end
end
end
describe "#allow_net_connect" do
it "should be settable" do
begin
# make sure to not mess up other specs.
old = @klass.allow_net_connect
@klass.allow_net_connect = true
@klass.allow_net_connect.should be_true
ensure
@klass.allow_net_connect = old
end
end
it "should default to true" do
@klass.allow_net_connect.should be_true
end
it "should raise an error if we queue a request while its false" do
begin
request = Typhoeus::Request.new("http://localhost:3000")
old_net_connect = @klass.allow_net_connect
old_ignore_localhost = @klass.ignore_localhost
@klass.allow_net_connect = false
@klass.ignore_localhost = false
hydra = Typhoeus::Hydra.new
lambda {
hydra.queue(request)
}.should raise_error(Typhoeus::Hydra::NetConnectNotAllowedError)
ensure
@klass.allow_net_connect = old_net_connect
@klass.ignore_localhost = old_ignore_localhost
end
end
end
describe "#allow_net_connect?" do
it "should return true by default" do
@klass.allow_net_connect?.should be_true
end
end
end