typhoeus 0.4.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +26 -0
  5. data/CHANGELOG.md +341 -28
  6. data/CONTRIBUTING.md +20 -0
  7. data/Gemfile +31 -2
  8. data/Guardfile +9 -0
  9. data/LICENSE +1 -1
  10. data/README.md +486 -357
  11. data/Rakefile +21 -12
  12. data/UPGRADE.md +55 -0
  13. data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +76 -0
  14. data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
  15. data/lib/rack/typhoeus.rb +1 -0
  16. data/lib/typhoeus/adapters/faraday.rb +180 -0
  17. data/lib/typhoeus/cache/dalli.rb +28 -0
  18. data/lib/typhoeus/cache/rails.rb +28 -0
  19. data/lib/typhoeus/cache/redis.rb +35 -0
  20. data/lib/typhoeus/config.rb +69 -0
  21. data/lib/typhoeus/easy_factory.rb +180 -0
  22. data/lib/typhoeus/errors/no_stub.rb +12 -0
  23. data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
  24. data/lib/typhoeus/errors.rb +9 -0
  25. data/lib/typhoeus/expectation.rb +217 -0
  26. data/lib/typhoeus/hydra/addable.rb +23 -0
  27. data/lib/typhoeus/hydra/before.rb +31 -0
  28. data/lib/typhoeus/hydra/block_connection.rb +35 -0
  29. data/lib/typhoeus/hydra/cacheable.rb +15 -0
  30. data/lib/typhoeus/hydra/memoizable.rb +56 -0
  31. data/lib/typhoeus/hydra/queueable.rb +83 -0
  32. data/lib/typhoeus/hydra/runnable.rb +19 -0
  33. data/lib/typhoeus/hydra/stubbable.rb +28 -0
  34. data/lib/typhoeus/hydra.rb +84 -236
  35. data/lib/typhoeus/pool.rb +70 -0
  36. data/lib/typhoeus/railtie.rb +12 -0
  37. data/lib/typhoeus/request/actions.rb +125 -0
  38. data/lib/typhoeus/request/before.rb +30 -0
  39. data/lib/typhoeus/request/block_connection.rb +52 -0
  40. data/lib/typhoeus/request/cacheable.rb +38 -0
  41. data/lib/typhoeus/request/callbacks.rb +151 -0
  42. data/lib/typhoeus/request/marshal.rb +22 -0
  43. data/lib/typhoeus/request/memoizable.rb +38 -0
  44. data/lib/typhoeus/request/operations.rb +40 -0
  45. data/lib/typhoeus/request/responseable.rb +29 -0
  46. data/lib/typhoeus/request/streamable.rb +34 -0
  47. data/lib/typhoeus/request/stubbable.rb +30 -0
  48. data/lib/typhoeus/request.rb +186 -231
  49. data/lib/typhoeus/response/cacheable.rb +14 -0
  50. data/lib/typhoeus/response/header.rb +105 -0
  51. data/lib/typhoeus/response/informations.rb +248 -0
  52. data/lib/typhoeus/response/status.rb +106 -0
  53. data/lib/typhoeus/response.rb +60 -115
  54. data/lib/typhoeus/version.rb +3 -1
  55. data/lib/typhoeus.rb +126 -39
  56. data/perf/profile.rb +14 -0
  57. data/perf/vs_nethttp.rb +64 -0
  58. data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +156 -0
  59. data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
  60. data/spec/spec_helper.rb +29 -0
  61. data/spec/support/localhost_server.rb +94 -0
  62. data/spec/support/memory_cache.rb +15 -0
  63. data/spec/support/server.rb +116 -0
  64. data/spec/typhoeus/adapters/faraday_spec.rb +339 -0
  65. data/spec/typhoeus/cache/dalli_spec.rb +41 -0
  66. data/spec/typhoeus/cache/redis_spec.rb +41 -0
  67. data/spec/typhoeus/config_spec.rb +15 -0
  68. data/spec/typhoeus/easy_factory_spec.rb +143 -0
  69. data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
  70. data/spec/typhoeus/expectation_spec.rb +280 -0
  71. data/spec/typhoeus/hydra/addable_spec.rb +22 -0
  72. data/spec/typhoeus/hydra/before_spec.rb +98 -0
  73. data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
  74. data/spec/typhoeus/hydra/cacheable_spec.rb +88 -0
  75. data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
  76. data/spec/typhoeus/hydra/queueable_spec.rb +98 -0
  77. data/spec/typhoeus/hydra/runnable_spec.rb +137 -0
  78. data/spec/typhoeus/hydra/stubbable_spec.rb +48 -0
  79. data/spec/typhoeus/hydra_spec.rb +22 -0
  80. data/spec/typhoeus/pool_spec.rb +137 -0
  81. data/spec/typhoeus/request/actions_spec.rb +19 -0
  82. data/spec/typhoeus/request/before_spec.rb +93 -0
  83. data/spec/typhoeus/request/block_connection_spec.rb +75 -0
  84. data/spec/typhoeus/request/cacheable_spec.rb +94 -0
  85. data/spec/typhoeus/request/callbacks_spec.rb +91 -0
  86. data/spec/typhoeus/request/marshal_spec.rb +60 -0
  87. data/spec/typhoeus/request/memoizable_spec.rb +34 -0
  88. data/spec/typhoeus/request/operations_spec.rb +101 -0
  89. data/spec/typhoeus/request/responseable_spec.rb +13 -0
  90. data/spec/typhoeus/request/stubbable_spec.rb +45 -0
  91. data/spec/typhoeus/request_spec.rb +232 -0
  92. data/spec/typhoeus/response/header_spec.rb +147 -0
  93. data/spec/typhoeus/response/informations_spec.rb +283 -0
  94. data/spec/typhoeus/response/status_spec.rb +256 -0
  95. data/spec/typhoeus/response_spec.rb +100 -0
  96. data/spec/typhoeus_spec.rb +105 -0
  97. data/typhoeus.gemspec +25 -0
  98. metadata +146 -158
  99. data/lib/typhoeus/curl.rb +0 -453
  100. data/lib/typhoeus/easy/auth.rb +0 -14
  101. data/lib/typhoeus/easy/callbacks.rb +0 -33
  102. data/lib/typhoeus/easy/ffi_helper.rb +0 -61
  103. data/lib/typhoeus/easy/infos.rb +0 -90
  104. data/lib/typhoeus/easy/options.rb +0 -115
  105. data/lib/typhoeus/easy/proxy.rb +0 -20
  106. data/lib/typhoeus/easy/ssl.rb +0 -82
  107. data/lib/typhoeus/easy.rb +0 -115
  108. data/lib/typhoeus/filter.rb +0 -28
  109. data/lib/typhoeus/form.rb +0 -61
  110. data/lib/typhoeus/header.rb +0 -54
  111. data/lib/typhoeus/hydra/callbacks.rb +0 -24
  112. data/lib/typhoeus/hydra/connect_options.rb +0 -61
  113. data/lib/typhoeus/hydra/stubbing.rb +0 -68
  114. data/lib/typhoeus/hydra_mock.rb +0 -131
  115. data/lib/typhoeus/multi.rb +0 -146
  116. data/lib/typhoeus/param_processor.rb +0 -43
  117. data/lib/typhoeus/remote.rb +0 -306
  118. data/lib/typhoeus/remote_method.rb +0 -108
  119. data/lib/typhoeus/remote_proxy_object.rb +0 -50
  120. data/lib/typhoeus/utils.rb +0 -50
@@ -0,0 +1,280 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Expectation do
4
+ let(:options) { {} }
5
+ let(:base_url) { "www.example.com" }
6
+ let(:expectation) { described_class.new(base_url, options) }
7
+
8
+ describe ".new" do
9
+ it "sets base_url" do
10
+ expect(expectation.instance_variable_get(:@base_url)).to eq(base_url)
11
+ end
12
+
13
+ it "sets options" do
14
+ expect(expectation.instance_variable_get(:@options)).to eq(options)
15
+ end
16
+
17
+ it "initializes response_counter" do
18
+ expect(expectation.instance_variable_get(:@response_counter)).to eq(0)
19
+ end
20
+ end
21
+
22
+ describe ".all" do
23
+ context "when @expectations nil" do
24
+ it "returns empty array" do
25
+ expect(Typhoeus::Expectation.all).to eq([])
26
+ end
27
+ end
28
+
29
+ context "when @expectations not nil" do
30
+ let(:expectations) { [1] }
31
+
32
+ it "returns @expectations" do
33
+ Typhoeus::Expectation.instance_variable_set(:@expectations, expectations)
34
+ expect(Typhoeus::Expectation.all).to be(expectations)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe ".clear" do
40
+ let(:expectations) { double(:clear) }
41
+
42
+ it "clears all" do
43
+ expect(expectations).to receive(:clear)
44
+ Typhoeus::Expectation.instance_variable_set(:@expectations, expectations)
45
+ Typhoeus::Expectation.clear
46
+ Typhoeus::Expectation.instance_variable_set(:@expectations, nil)
47
+ end
48
+ end
49
+
50
+ describe ".response_for" do
51
+ let(:request) { Typhoeus::Request.new("") }
52
+ let(:stubbed_response) { Typhoeus::Response.new }
53
+
54
+ it "finds a matching expectation and returns its next response" do
55
+ Typhoeus::Expectation.all << expectation
56
+ expect(expectation).to receive(:matches?).with(request).and_return(true)
57
+ expect(expectation).to receive(:response).with(request).and_return(stubbed_response)
58
+
59
+ response = Typhoeus::Expectation.response_for(request)
60
+
61
+ expect(response).to be(stubbed_response)
62
+ end
63
+
64
+ it "returns nil if no matching expectation is found" do
65
+ response = Typhoeus::Expectation.response_for(request)
66
+ expect(response).to be(nil)
67
+ end
68
+ end
69
+
70
+ describe "#stubbed_from" do
71
+ it "sets value" do
72
+ expectation.stubbed_from(:webmock)
73
+ expect(expectation.from).to eq(:webmock)
74
+ end
75
+
76
+ it "returns self" do
77
+ expect(expectation.stubbed_from(:webmock)).to be(expectation)
78
+ end
79
+ end
80
+
81
+ describe "#and_return" do
82
+ context "when value" do
83
+ it "adds to responses" do
84
+ expectation.and_return(1)
85
+ expect(expectation.responses).to eq([1])
86
+ end
87
+ end
88
+
89
+ context "when array" do
90
+ it "adds to responses" do
91
+ expectation.and_return([1, 2])
92
+ expect(expectation.responses).to eq([1, 2])
93
+ end
94
+ end
95
+
96
+ context "when block" do
97
+ it "adds to responses" do
98
+ block = Proc.new {}
99
+ expectation.and_return(&block)
100
+ expect(expectation.responses).to eq([block])
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#responses" do
106
+ it "returns responses" do
107
+ expect(expectation.responses).to be_a(Array)
108
+ end
109
+ end
110
+
111
+ describe "#response" do
112
+ let(:request) { Typhoeus::Request.new("") }
113
+
114
+ before { expectation.instance_variable_set(:@responses, responses) }
115
+
116
+ context "when one response" do
117
+ context "is pre-constructed" do
118
+ let(:responses) { [Typhoeus::Response.new] }
119
+
120
+ it "returns response" do
121
+ expect(expectation.response(request)).to be(responses[0])
122
+ end
123
+ end
124
+
125
+ context "is lazily-constructed" do
126
+ def construct_response(request)
127
+ @request_from_response_construction = request
128
+ lazily_constructed_response
129
+ end
130
+
131
+ let(:lazily_constructed_response) { Typhoeus::Response.new }
132
+ let(:responses) { [ Proc.new { |request| construct_response(request) } ] }
133
+
134
+ it "returns response" do
135
+ expect(expectation.response(request)).to be(lazily_constructed_response)
136
+ expect(@request_from_response_construction).to be(request)
137
+ end
138
+ end
139
+ end
140
+
141
+ context "when multiple responses" do
142
+ let(:responses) { [Typhoeus::Response.new, Typhoeus::Response.new, Typhoeus::Response.new] }
143
+
144
+ it "returns one by one" do
145
+ 3.times do |i|
146
+ expect(expectation.response(request)).to be(responses[i])
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "#matches?" do
153
+ let(:request) { double(:base_url => nil) }
154
+
155
+ it "calls url_match?" do
156
+ expect(expectation).to receive(:url_match?)
157
+ expectation.matches?(request)
158
+ end
159
+
160
+ it "calls options_match?" do
161
+ expect(expectation).to receive(:url_match?).and_return(true)
162
+ expect(expectation).to receive(:options_match?)
163
+ expectation.matches?(request)
164
+ end
165
+ end
166
+
167
+ describe "#url_match?" do
168
+ let(:request_url) { "www.example.com" }
169
+ let(:request) { Typhoeus::Request.new(request_url) }
170
+ let(:url_match) { expectation.method(:url_match?).call(request.base_url) }
171
+
172
+ context "when string" do
173
+ context "when match" do
174
+ it "returns true" do
175
+ expect(url_match).to be_truthy
176
+ end
177
+ end
178
+
179
+ context "when no match" do
180
+ let(:base_url) { "no_match" }
181
+
182
+ it "returns false" do
183
+ expect(url_match).to be_falsey
184
+ end
185
+ end
186
+ end
187
+
188
+ context "when regexp" do
189
+ context "when match" do
190
+ let(:base_url) { /example/ }
191
+
192
+ it "returns true" do
193
+ expect(url_match).to be_truthy
194
+ end
195
+ end
196
+
197
+ context "when no match" do
198
+ let(:base_url) { /nomatch/ }
199
+
200
+ it "returns false" do
201
+ expect(url_match).to be_falsey
202
+ end
203
+
204
+ context "with nil request_url" do
205
+ let(:request_url) { nil }
206
+
207
+ it "returns false" do
208
+ expect(url_match).to be_falsey
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ context "when nil" do
215
+ let(:base_url) { nil }
216
+
217
+ it "returns true" do
218
+ expect(url_match).to be_truthy
219
+ end
220
+ end
221
+
222
+ context "when not string, regexp, nil" do
223
+ let(:base_url) { 1 }
224
+
225
+ it "returns false" do
226
+ expect(url_match).to be_falsey
227
+ end
228
+ end
229
+ end
230
+
231
+ describe "options_match?" do
232
+ let(:request_options) { {} }
233
+ let(:request) { Typhoeus::Request.new(nil, request_options) }
234
+ let(:options_match) { expectation.method(:options_match?).call(request) }
235
+
236
+ context "when match" do
237
+ let(:options) { { :a => 1 } }
238
+ let(:request_options) { options }
239
+
240
+ it "returns true" do
241
+ expect(options_match).to be_truthy
242
+ end
243
+ end
244
+
245
+ context "when options are a subset from request_options" do
246
+ let(:options) { { :a => 1 } }
247
+ let(:request_options) { { :a => 1, :b => 2 } }
248
+
249
+ it "returns true" do
250
+ expect(options_match).to be_truthy
251
+ end
252
+ end
253
+
254
+ context "when options are nested" do
255
+ let(:options) { { :a => { :b => 1 } } }
256
+ let(:request_options) { options }
257
+
258
+ it "returns true" do
259
+ expect(options_match).to be_truthy
260
+ end
261
+ end
262
+
263
+ context "when options contains an array" do
264
+ let(:options) { { :a => [1, 2] } }
265
+ let(:request_options) { options }
266
+
267
+ it "returns true" do
268
+ expect(options_match).to be_truthy
269
+ end
270
+ end
271
+
272
+ context "when no match" do
273
+ let(:options) { { :a => 1 } }
274
+
275
+ it "returns false" do
276
+ expect(options_match).to be_falsey
277
+ end
278
+ end
279
+ end
280
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Addable do
4
+ let(:hydra) { Typhoeus::Hydra.new() }
5
+ let(:request) { Typhoeus::Request.new("localhost:3001", {:method => :get}) }
6
+
7
+ it "asks easy factory for an easy" do
8
+ multi = double
9
+ expect(Typhoeus::EasyFactory).to receive(:new).with(request, hydra).and_return(double(:get => 1))
10
+ expect(hydra).to receive(:multi).and_return(multi)
11
+ expect(multi).to receive(:add).with(1)
12
+ hydra.add(request)
13
+ end
14
+
15
+ it "adds easy to multi" do
16
+ multi = double
17
+ expect(Typhoeus::EasyFactory).to receive(:new).with(request, hydra).and_return(double(:get => 1))
18
+ expect(hydra).to receive(:multi).and_return(multi)
19
+ expect(multi).to receive(:add).with(1)
20
+ hydra.add(request)
21
+ end
22
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Before do
4
+ let(:request) { Typhoeus::Request.new("") }
5
+ let(:hydra) { Typhoeus::Hydra.new }
6
+ let(:receive_counter) { double :mark => :twain }
7
+
8
+ describe "#add" do
9
+ context "when before" do
10
+ context "when one" do
11
+ it "executes" do
12
+ Typhoeus.before { |r| receive_counter.mark }
13
+ expect(receive_counter).to receive(:mark)
14
+ hydra.add(request)
15
+ end
16
+
17
+ context "when true" do
18
+ it "calls super" do
19
+ Typhoeus.before { true }
20
+ expect(Typhoeus::Expectation).to receive(:response_for)
21
+ hydra.add(request)
22
+ end
23
+ end
24
+
25
+ context "when falsy" do
26
+ context "when queue requests" do
27
+ let(:queued_request) { Typhoeus::Request.new("") }
28
+
29
+ before { hydra.queue(queued_request) }
30
+
31
+ it "dequeues" do
32
+ Typhoeus.before { false }
33
+ hydra.add(request)
34
+ expect(hydra.queued_requests).to be_empty
35
+ end
36
+ end
37
+
38
+ context "when false" do
39
+ it "doesn't call super" do
40
+ Typhoeus.before { false }
41
+ expect(Typhoeus::Expectation).to receive(:response_for).never
42
+ hydra.add(request)
43
+ end
44
+ end
45
+
46
+ context "when response" do
47
+ it "doesn't call super" do
48
+ Typhoeus.before { Typhoeus::Response.new }
49
+ expect(Typhoeus::Expectation).to receive(:response_for).never
50
+ hydra.add(request)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ context "when multi" do
57
+ context "when all true" do
58
+ before { 3.times { Typhoeus.before { |r| receive_counter.mark } } }
59
+
60
+ it "calls super" do
61
+ expect(Typhoeus::Expectation).to receive(:response_for)
62
+ hydra.add(request)
63
+ end
64
+
65
+ it "executes all" do
66
+ expect(receive_counter).to receive(:mark).exactly(3).times
67
+ hydra.add(request)
68
+ end
69
+ end
70
+
71
+ context "when middle false" do
72
+ before do
73
+ Typhoeus.before { |r| receive_counter.mark }
74
+ Typhoeus.before { |r| receive_counter.mark; nil }
75
+ Typhoeus.before { |r| receive_counter.mark }
76
+ end
77
+
78
+ it "doesn't call super" do
79
+ expect(Typhoeus::Expectation).to receive(:response_for).never
80
+ hydra.add(request)
81
+ end
82
+
83
+ it "executes only two" do
84
+ expect(receive_counter).to receive(:mark).exactly(2).times
85
+ hydra.add(request)
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ context "when no before" do
92
+ it "calls super" do
93
+ expect(Typhoeus::Expectation).to receive(:response_for)
94
+ hydra.add(request)
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::BlockConnection do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:hydra) { Typhoeus::Hydra.new() }
6
+ let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
7
+
8
+ describe "add" do
9
+ context "when block_connection activated" do
10
+ before { Typhoeus::Config.block_connection = true }
11
+ after { Typhoeus::Config.block_connection = false }
12
+
13
+ it "raises" do
14
+ expect{ hydra.add(request) }.to raise_error(Typhoeus::Errors::NoStub)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Cacheable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:hydra) { Typhoeus::Hydra.new() }
6
+ let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
7
+ let(:response) { Typhoeus::Response.new }
8
+ let(:cache) { MemoryCache.new }
9
+
10
+ describe "add" do
11
+ context "when cache activated" do
12
+ before { Typhoeus::Config.cache = cache }
13
+ after { Typhoeus::Config.cache = false }
14
+
15
+ context "when request new" do
16
+ it "sets no response" do
17
+ hydra.add(request)
18
+ expect(request.response).to be_nil
19
+ end
20
+
21
+ it "doesn't call complete" do
22
+ expect(request).to receive(:complete).never
23
+ hydra.add(request)
24
+ end
25
+ end
26
+
27
+ context "when request in memory" do
28
+ before { cache.memory[request] = response }
29
+
30
+ it "returns response with cached status" do
31
+ hydra.add(request)
32
+ expect(response.cached?).to be_truthy
33
+ end
34
+
35
+ context "when no queued requests" do
36
+ it "finishes request" do
37
+ expect(request).to receive(:finish).with(response)
38
+ hydra.add(request)
39
+ expect(response.cached?).to be_truthy
40
+ end
41
+ end
42
+
43
+ context "when queued requests" do
44
+ let(:queued_request) { Typhoeus::Request.new(base_url, {:method => :get}) }
45
+
46
+ before { cache.memory[queued_request] = response }
47
+
48
+ it "finishes both requests" do
49
+ hydra.queue(queued_request)
50
+ expect(request).to receive(:finish).with(response)
51
+ expect(queued_request).to receive(:finish).with(response)
52
+ hydra.add(request)
53
+ end
54
+ end
55
+ end
56
+
57
+ context "when cache is specified on a request" do
58
+ before { Typhoeus::Config.cache = false }
59
+
60
+ context "when cache is false" do
61
+ let(:non_cached_request) { Typhoeus::Request.new(base_url, {:method => :get, :cache => false}) }
62
+
63
+ it "initiates an HTTP call" do
64
+ expect(Typhoeus::EasyFactory).to receive(:new).with(non_cached_request, hydra).and_call_original
65
+
66
+ hydra.add(non_cached_request)
67
+ end
68
+ end
69
+
70
+ context "when cache is defined" do
71
+ let(:cached_request) { Typhoeus::Request.new(base_url, {:method => :get, :cache => cache}) }
72
+
73
+ before { cache.memory[cached_request] = response }
74
+
75
+ it "uses the cache instead of making a new request" do
76
+ expect(Typhoeus::EasyFactory).not_to receive(:new)
77
+
78
+ hydra.add(cached_request)
79
+
80
+ expect(cached_request.response).to be_cached
81
+ expect(cached_request.response).to eq(response)
82
+ end
83
+ end
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Memoizable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:hydra) { Typhoeus::Hydra.new() }
6
+ let(:request) { Typhoeus::Request.new(base_url) }
7
+
8
+ describe "add" do
9
+ context "when memoization activated" do
10
+ before { Typhoeus::Config.memoize = true }
11
+
12
+ context "when request new" do
13
+ it "sets no response" do
14
+ hydra.add(request)
15
+ expect(request.response).to be_nil
16
+ end
17
+
18
+ it "doesn't call complete" do
19
+ expect(request).to receive(:complete).never
20
+ hydra.add(request)
21
+ end
22
+ end
23
+
24
+ context "when request in memory" do
25
+ let(:response) { Typhoeus::Response.new }
26
+ before { hydra.memory[request] = response }
27
+
28
+ it "finishes request" do
29
+ expect(request).to receive(:finish).with(response, true)
30
+ hydra.add(request)
31
+ end
32
+
33
+ context "when queued request" do
34
+ let(:queued_request) { Typhoeus::Request.new(base_url) }
35
+
36
+ it "dequeues" do
37
+ hydra.queue(queued_request)
38
+ expect(request).to receive(:finish).with(response, true)
39
+ expect(queued_request).to receive(:finish).with(response, true)
40
+ hydra.add(request)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#run" do
48
+ it "clears memory before starting" do
49
+ expect(hydra.memory).to receive(:clear)
50
+ hydra.run
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Queueable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:options) { {} }
6
+ let(:hydra) { Typhoeus::Hydra.new(options) }
7
+
8
+ describe "#queue" do
9
+ let(:request) { Typhoeus::Request.new("") }
10
+
11
+ it "accepts requests" do
12
+ hydra.queue(request)
13
+ end
14
+
15
+ it "sets hydra on request" do
16
+ hydra.queue(request)
17
+ expect(request.hydra).to eq(hydra)
18
+ end
19
+
20
+ it "adds to queued requests" do
21
+ hydra.queue(request)
22
+ expect(hydra.queued_requests).to include(request)
23
+ end
24
+
25
+ it "adds to front of queued requests" do
26
+ hydra.queue_front(request)
27
+ expect(hydra.queued_requests.first).to be(request)
28
+ end
29
+ end
30
+
31
+ describe "#abort" do
32
+ before { hydra.queued_requests << 1 }
33
+
34
+ it "clears queue" do
35
+ hydra.abort
36
+ expect(hydra.queued_requests).to be_empty
37
+ end
38
+ end
39
+
40
+ describe "#dequeue_many" do
41
+ before do
42
+ requests.each { |r| hydra.queue r }
43
+ end
44
+
45
+ context "when no request queued" do
46
+ let(:requests) { [] }
47
+
48
+ it "does nothing" do
49
+ expect(hydra).to_not receive(:add)
50
+ hydra.dequeue_many
51
+ end
52
+ end
53
+
54
+ context "when request queued" do
55
+ let(:first) { Typhoeus::Request.new("localhost:3001/first") }
56
+ let(:requests) { [first] }
57
+
58
+ it "adds request from queue to multi" do
59
+ expect(hydra).to receive(:add).with(first)
60
+ hydra.dequeue_many
61
+ end
62
+ end
63
+
64
+ context "when three request queued" do
65
+ let(:first) { Typhoeus::Request.new("localhost:3001/first") }
66
+ let(:second) { Typhoeus::Request.new("localhost:3001/second") }
67
+ let(:third) { Typhoeus::Request.new("localhost:3001/third") }
68
+ let(:requests) { [first, second, third] }
69
+
70
+ it "adds requests from queue to multi" do
71
+ expect(hydra).to receive(:add).with(first)
72
+ expect(hydra).to receive(:add).with(second)
73
+ expect(hydra).to receive(:add).with(third)
74
+ hydra.dequeue_many
75
+ end
76
+
77
+ context "when max_concurrency is two" do
78
+ let(:options) { {:max_concurrency => 2} }
79
+ it "adds requests from queue to multi" do
80
+ expect(hydra).to receive(:add).with(first)
81
+ expect(hydra).to receive(:add).with(second)
82
+ expect(hydra).to_not receive(:add).with(third)
83
+ hydra.dequeue_many
84
+ end
85
+ end
86
+
87
+ context "when max_concurrency is a string" do
88
+ let(:options) { {:max_concurrency => "2"} }
89
+ it "adds requests from queue to multi" do
90
+ expect(hydra).to receive(:add).with(first)
91
+ expect(hydra).to receive(:add).with(second)
92
+ expect(hydra).to_not receive(:add).with(third)
93
+ hydra.dequeue_many
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end