webmock 2.0.3 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile +2 -7
- data/README.md +54 -53
- data/Rakefile +1 -1
- data/lib/webmock/callback_registry.rb +1 -1
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +68 -6
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +4 -4
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +9 -9
- data/lib/webmock/http_lib_adapters/http_rb/request.rb +2 -2
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +5 -5
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +3 -3
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +5 -5
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +7 -7
- data/lib/webmock/http_lib_adapters/net_http.rb +3 -3
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +4 -4
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +14 -14
- data/lib/webmock/matchers/hash_including_matcher.rb +1 -1
- data/lib/webmock/rack_response.rb +3 -3
- data/lib/webmock/request_pattern.rb +8 -8
- data/lib/webmock/request_stub.rb +5 -5
- data/lib/webmock/util/uri.rb +2 -2
- data/lib/webmock/version.rb +1 -1
- data/minitest/webmock_spec.rb +3 -3
- data/spec/acceptance/curb/curb_spec.rb +64 -47
- data/spec/acceptance/curb/curb_spec_helper.rb +4 -4
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +32 -32
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +9 -9
- data/spec/acceptance/excon/excon_spec.rb +14 -14
- data/spec/acceptance/excon/excon_spec_helper.rb +6 -6
- data/spec/acceptance/http_rb/http_rb_spec.rb +1 -1
- data/spec/acceptance/http_rb/http_rb_spec_helper.rb +7 -7
- data/spec/acceptance/httpclient/httpclient_spec.rb +20 -20
- data/spec/acceptance/httpclient/httpclient_spec_helper.rb +5 -5
- data/spec/acceptance/manticore/manticore_spec.rb +11 -11
- data/spec/acceptance/manticore/manticore_spec_helper.rb +3 -3
- data/spec/acceptance/net_http/net_http_shared.rb +4 -4
- data/spec/acceptance/net_http/net_http_spec.rb +25 -25
- data/spec/acceptance/net_http/net_http_spec_helper.rb +4 -4
- data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
- data/spec/acceptance/patron/patron_spec.rb +10 -10
- data/spec/acceptance/patron/patron_spec_helper.rb +5 -5
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +36 -36
- data/spec/acceptance/shared/callbacks.rb +11 -11
- data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +5 -5
- data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +4 -4
- data/spec/acceptance/shared/precedence_of_stubs.rb +4 -4
- data/spec/acceptance/shared/request_expectations.rb +90 -90
- data/spec/acceptance/shared/returning_declared_responses.rb +49 -49
- data/spec/acceptance/shared/stubbing_requests.rb +78 -78
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +11 -11
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +8 -8
- data/spec/spec_helper.rb +6 -3
- data/spec/support/webmock_server.rb +1 -1
- data/spec/unit/api_spec.rb +2 -2
- data/spec/unit/errors_spec.rb +3 -3
- data/spec/unit/matchers/hash_including_matcher_spec.rb +16 -16
- data/spec/unit/rack_response_spec.rb +2 -2
- data/spec/unit/request_body_diff_spec.rb +1 -1
- data/spec/unit/request_execution_verifier_spec.rb +1 -1
- data/spec/unit/request_pattern_spec.rb +115 -115
- data/spec/unit/request_signature_snippet_spec.rb +2 -2
- data/spec/unit/request_signature_spec.rb +16 -16
- data/spec/unit/request_stub_spec.rb +20 -20
- data/spec/unit/response_spec.rb +28 -28
- data/spec/unit/stub_registry_spec.rb +10 -10
- data/spec/unit/stub_request_snippet_spec.rb +6 -6
- data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
- data/spec/unit/util/query_mapper_spec.rb +2 -2
- data/spec/unit/util/uri_spec.rb +1 -1
- data/test/http_request.rb +3 -3
- data/test/shared_test.rb +6 -6
- data/webmock.gemspec +4 -2
- metadata +32 -4
data/lib/webmock/util/uri.rb
CHANGED
@@ -14,8 +14,8 @@ module WebMock
|
|
14
14
|
NORMALIZED_URIS = Hash.new do |hash, uri|
|
15
15
|
normalized_uri = WebMock::Util::URI.heuristic_parse(uri)
|
16
16
|
if normalized_uri.query_values
|
17
|
-
sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, :
|
18
|
-
normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, :
|
17
|
+
sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, notation: Config.instance.query_values_notation) || {})
|
18
|
+
normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, notation: WebMock::Config.instance.query_values_notation)
|
19
19
|
end
|
20
20
|
normalized_uri = normalized_uri.normalize #normalize! is slower
|
21
21
|
normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
|
data/lib/webmock/version.rb
CHANGED
data/minitest/webmock_spec.rb
CHANGED
@@ -25,20 +25,20 @@ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
|
25
25
|
|
26
26
|
it "should verify that expected request occured" do
|
27
27
|
http_request(:get, "http://www.example.com/")
|
28
|
-
assert_requested(:get, "http://www.example.com", :
|
28
|
+
assert_requested(:get, "http://www.example.com", times: 1)
|
29
29
|
assert_requested(:get, "http://www.example.com")
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should verify that expected http stub occured" do
|
33
33
|
http_request(:get, "http://www.example.com/")
|
34
|
-
assert_requested(@stub_http, :
|
34
|
+
assert_requested(@stub_http, times: 1)
|
35
35
|
assert_requested(@stub_http)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should verify that expected https stub occured" do
|
39
39
|
http_request(:get, "https://www.example.com/")
|
40
40
|
http_request(:get, "https://www.example.com/")
|
41
|
-
assert_requested(@stub_https, :
|
41
|
+
assert_requested(@stub_https, times: 2)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should verify that expect request didn't occur" do
|
@@ -11,8 +11,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
11
11
|
|
12
12
|
describe "when doing PUTs" do
|
13
13
|
it "should stub them" do
|
14
|
-
stub_request(:put, "www.example.com").with(:
|
15
|
-
expect(http_request(:put, "http://www.example.com", :
|
14
|
+
stub_request(:put, "www.example.com").with(body: "01234")
|
15
|
+
expect(http_request(:put, "http://www.example.com", body: "01234").
|
16
16
|
status).to eq("200")
|
17
17
|
end
|
18
18
|
end
|
@@ -30,9 +30,26 @@ unless RUBY_PLATFORM =~ /java/
|
|
30
30
|
@curl.url = "http://example.com"
|
31
31
|
end
|
32
32
|
|
33
|
+
describe 'on_debug' do
|
34
|
+
it "should call on_debug" do
|
35
|
+
stub_request(:any, "example.com").
|
36
|
+
to_return(status: 200, headers: { 'Server' => 'nginx' }, body: { hello: :world }.to_json)
|
37
|
+
|
38
|
+
test = []
|
39
|
+
|
40
|
+
@curl.on_debug do |message, operation|
|
41
|
+
test << "#{operation} -> #{message}"
|
42
|
+
end
|
43
|
+
@curl.headers['Content-Type'] = 'application/json'
|
44
|
+
@curl.http_post({ hello: :world }.to_json)
|
45
|
+
|
46
|
+
expect(test).to_not be_empty
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
33
50
|
it "should call on_success with 2xx response" do
|
34
51
|
body = "on_success fired"
|
35
|
-
stub_request(:any, "example.com").to_return(:
|
52
|
+
stub_request(:any, "example.com").to_return(body: body)
|
36
53
|
|
37
54
|
test = nil
|
38
55
|
@curl.on_success do |c|
|
@@ -45,7 +62,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
45
62
|
it "should call on_missing with 4xx response" do
|
46
63
|
response_code = 403
|
47
64
|
stub_request(:any, "example.com").
|
48
|
-
to_return(:
|
65
|
+
to_return(status: [response_code, "None shall pass"])
|
49
66
|
|
50
67
|
test = nil
|
51
68
|
@curl.on_missing do |c, code|
|
@@ -58,7 +75,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
58
75
|
it "should call on_failure with 5xx response" do
|
59
76
|
response_code = 599
|
60
77
|
stub_request(:any, "example.com").
|
61
|
-
to_return(:
|
78
|
+
to_return(status: [response_code, "Server On Fire"])
|
62
79
|
|
63
80
|
test = nil
|
64
81
|
@curl.on_failure do |c, code|
|
@@ -71,7 +88,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
71
88
|
it "should call on_body when response body is read" do
|
72
89
|
body = "on_body fired"
|
73
90
|
stub_request(:any, "example.com").
|
74
|
-
to_return(:
|
91
|
+
to_return(body: body)
|
75
92
|
|
76
93
|
test = nil
|
77
94
|
@curl.on_body do |data|
|
@@ -83,8 +100,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
83
100
|
|
84
101
|
it "should call on_body for each chunk with chunked response" do
|
85
102
|
stub_request(:any, "example.com").
|
86
|
-
to_return(:
|
87
|
-
:
|
103
|
+
to_return(body: ["first_chunk", "second_chunk"],
|
104
|
+
headers: {"Transfer-Encoding" => "chunked"})
|
88
105
|
|
89
106
|
test = []
|
90
107
|
@curl.on_body do |data|
|
@@ -96,7 +113,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
96
113
|
|
97
114
|
it "should call on_header when response headers are read" do
|
98
115
|
stub_request(:any, "example.com").
|
99
|
-
to_return(:
|
116
|
+
to_return(headers: {one: 1})
|
100
117
|
|
101
118
|
test = []
|
102
119
|
@curl.on_header do |data|
|
@@ -111,7 +128,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
111
128
|
|
112
129
|
it "should call on_complete when request is complete" do
|
113
130
|
body = "on_complete fired"
|
114
|
-
stub_request(:any, "example.com").to_return(:
|
131
|
+
stub_request(:any, "example.com").to_return(body: body)
|
115
132
|
|
116
133
|
test = nil
|
117
134
|
@curl.on_complete do |curl|
|
@@ -122,7 +139,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
122
139
|
end
|
123
140
|
|
124
141
|
it "should call on_progress when portion of response body is read" do
|
125
|
-
stub_request(:any, "example.com").to_return(:
|
142
|
+
stub_request(:any, "example.com").to_return(body: "01234")
|
126
143
|
|
127
144
|
test = nil
|
128
145
|
@curl.on_progress do |*args|
|
@@ -150,7 +167,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
150
167
|
end
|
151
168
|
|
152
169
|
it "should call callbacks in correct order on failed request" do
|
153
|
-
stub_request(:any, "example.com").to_return(:
|
170
|
+
stub_request(:any, "example.com").to_return(status: [500, ""])
|
154
171
|
order = []
|
155
172
|
@curl.on_success {|*args| order << :on_success }
|
156
173
|
@curl.on_missing {|*args| order << :on_missing }
|
@@ -165,7 +182,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
165
182
|
end
|
166
183
|
|
167
184
|
it "should call callbacks in correct order on missing request" do
|
168
|
-
stub_request(:any, "example.com").to_return(:
|
185
|
+
stub_request(:any, "example.com").to_return(status: [403, ""])
|
169
186
|
order = []
|
170
187
|
@curl.on_success {|*args| order << :on_success }
|
171
188
|
@curl.on_missing {|*args| order << :on_missing }
|
@@ -191,9 +208,9 @@ unless RUBY_PLATFORM =~ /java/
|
|
191
208
|
|
192
209
|
it 'should be the same as #url even with a location header' do
|
193
210
|
stub_request(:any, 'example.com').
|
194
|
-
to_return(:
|
195
|
-
:
|
196
|
-
:
|
211
|
+
to_return(body: "abc",
|
212
|
+
status: 302,
|
213
|
+
headers: { 'Location' => 'http://www.example.com' })
|
197
214
|
|
198
215
|
@curl.http_get
|
199
216
|
expect(@curl.last_effective_url).to eq('http://example.com')
|
@@ -211,7 +228,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
211
228
|
|
212
229
|
it 'should be the value of the location header when present' do
|
213
230
|
stub_request(:any, 'example.com').
|
214
|
-
to_return(:
|
231
|
+
to_return(headers: { 'Location' => 'http://www.example.com' })
|
215
232
|
stub_request(:any, 'www.example.com')
|
216
233
|
|
217
234
|
@curl.http_get
|
@@ -220,9 +237,9 @@ unless RUBY_PLATFORM =~ /java/
|
|
220
237
|
|
221
238
|
it 'should work with more than one redirect' do
|
222
239
|
stub_request(:any, 'example.com').
|
223
|
-
to_return(:
|
240
|
+
to_return(headers: { 'Location' => 'http://www.example.com' })
|
224
241
|
stub_request(:any, 'www.example.com').
|
225
|
-
to_return(:
|
242
|
+
to_return(headers: { 'Location' => 'http://blog.example.com' })
|
226
243
|
stub_request(:any, 'blog.example.com')
|
227
244
|
|
228
245
|
@curl.http_get
|
@@ -231,7 +248,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
231
248
|
|
232
249
|
it 'should maintain the original url' do
|
233
250
|
stub_request(:any, 'example.com').
|
234
|
-
to_return(:
|
251
|
+
to_return(headers: { 'Location' => 'http://www.example.com' })
|
235
252
|
stub_request(:any, 'www.example.com')
|
236
253
|
|
237
254
|
@curl.http_get
|
@@ -240,10 +257,10 @@ unless RUBY_PLATFORM =~ /java/
|
|
240
257
|
|
241
258
|
it 'should have the redirected-to attrs (body, response code)' do
|
242
259
|
stub_request(:any, 'example.com').
|
243
|
-
to_return(:
|
244
|
-
:
|
245
|
-
:
|
246
|
-
stub_request(:any, 'www.example.com').to_return(:
|
260
|
+
to_return(body: 'request A',
|
261
|
+
status: 302,
|
262
|
+
headers: { 'Location' => 'http://www.example.com' })
|
263
|
+
stub_request(:any, 'www.example.com').to_return(body: 'request B')
|
247
264
|
|
248
265
|
@curl.http_get
|
249
266
|
expect(@curl.body_str).to eq('request B')
|
@@ -252,10 +269,10 @@ unless RUBY_PLATFORM =~ /java/
|
|
252
269
|
|
253
270
|
it 'should follow more than one redirect' do
|
254
271
|
stub_request(:any, 'example.com').
|
255
|
-
to_return(:
|
272
|
+
to_return(headers: { 'Location' => 'http://www.example.com' })
|
256
273
|
stub_request(:any, 'www.example.com').
|
257
|
-
to_return(:
|
258
|
-
stub_request(:any, 'blog.example.com').to_return(:
|
274
|
+
to_return(headers: { 'Location' => 'http://blog.example.com' })
|
275
|
+
stub_request(:any, 'blog.example.com').to_return(body: 'blog post')
|
259
276
|
|
260
277
|
@curl.http_get
|
261
278
|
expect(@curl.url).to eq('http://example.com')
|
@@ -275,9 +292,9 @@ unless RUBY_PLATFORM =~ /java/
|
|
275
292
|
content_type = "application/json"
|
276
293
|
|
277
294
|
stub_request(:any, 'example.com').
|
278
|
-
to_return(:
|
279
|
-
:
|
280
|
-
:
|
295
|
+
to_return(body: "abc",
|
296
|
+
status: 200,
|
297
|
+
headers: { 'Content-Type' => content_type })
|
281
298
|
|
282
299
|
@curl.http_get
|
283
300
|
expect(@curl.content_type).to eq(content_type)
|
@@ -288,8 +305,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
288
305
|
it "returns nil for content_type" do
|
289
306
|
|
290
307
|
stub_request(:any, 'example.com').
|
291
|
-
to_return(:
|
292
|
-
:
|
308
|
+
to_return(body: "abc",
|
309
|
+
status: 200 )
|
293
310
|
|
294
311
|
@curl.http_get
|
295
312
|
expect(@curl.content_type).to be_nil
|
@@ -305,9 +322,9 @@ unless RUBY_PLATFORM =~ /java/
|
|
305
322
|
|
306
323
|
it "is true when Transfer-Encoding is 'chunked' and body responds to each" do
|
307
324
|
stub_request(:any, 'example.com').
|
308
|
-
to_return(:
|
309
|
-
:
|
310
|
-
:
|
325
|
+
to_return(body: ["abc", "def"],
|
326
|
+
status: 200,
|
327
|
+
headers: { 'Transfer-Encoding' => 'chunked' })
|
311
328
|
|
312
329
|
@curl.http_get
|
313
330
|
expect(@curl).to be_chunked_response
|
@@ -315,8 +332,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
315
332
|
|
316
333
|
it "is false when Transfer-Encoding is not 'chunked'" do
|
317
334
|
stub_request(:any, 'example.com').
|
318
|
-
to_return(:
|
319
|
-
:
|
335
|
+
to_return(body: ["abc", "def"],
|
336
|
+
status: 200)
|
320
337
|
|
321
338
|
@curl.http_get
|
322
339
|
expect(@curl).not_to be_chunked_response
|
@@ -324,8 +341,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
324
341
|
|
325
342
|
it "is false when Transfer-Encoding is 'chunked' but body does not respond to each" do
|
326
343
|
stub_request(:any, 'example.com').
|
327
|
-
to_return(:
|
328
|
-
:
|
344
|
+
to_return(body: "abc",
|
345
|
+
status: 200)
|
329
346
|
|
330
347
|
@curl.http_get
|
331
348
|
expect(@curl).not_to be_chunked_response
|
@@ -339,7 +356,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
339
356
|
include CurbSpecHelper::DynamicHttp
|
340
357
|
|
341
358
|
it "should work with uppercase arguments" do
|
342
|
-
stub_request(:get, "www.example.com").to_return(:
|
359
|
+
stub_request(:get, "www.example.com").to_return(body: "abc")
|
343
360
|
|
344
361
|
c = Curl::Easy.new
|
345
362
|
c.url = "http://www.example.com"
|
@@ -348,7 +365,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
348
365
|
end
|
349
366
|
|
350
367
|
it "should alias body to body_str" do
|
351
|
-
stub_request(:get, "www.example.com").to_return(:
|
368
|
+
stub_request(:get, "www.example.com").to_return(body: "abc")
|
352
369
|
|
353
370
|
c = Curl::Easy.new
|
354
371
|
c.url = "http://www.example.com"
|
@@ -357,7 +374,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
357
374
|
end
|
358
375
|
|
359
376
|
it "supports array headers passed to Curl::Easy" do
|
360
|
-
stub_request(:get, "www.example.com").with(headers: {'X-One' => '1'}).to_return(:
|
377
|
+
stub_request(:get, "www.example.com").with(headers: {'X-One' => '1'}).to_return(body: "abc")
|
361
378
|
|
362
379
|
c = Curl::Easy.new
|
363
380
|
c.url = "http://www.example.com"
|
@@ -372,7 +389,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
372
389
|
include CurbSpecHelper::NamedHttp
|
373
390
|
|
374
391
|
it "should work with blank arguments for post" do
|
375
|
-
stub_request(:post, "www.example.com").with(:
|
392
|
+
stub_request(:post, "www.example.com").with(body: "01234")
|
376
393
|
c = Curl::Easy.new
|
377
394
|
c.url = "http://www.example.com"
|
378
395
|
c.post_body = "01234"
|
@@ -381,13 +398,13 @@ unless RUBY_PLATFORM =~ /java/
|
|
381
398
|
end
|
382
399
|
|
383
400
|
it "should work with several body arguments for post using the class method" do
|
384
|
-
stub_request(:post, "www.example.com").with(:
|
401
|
+
stub_request(:post, "www.example.com").with(body: {user: {first_name: 'Bartosz', last_name: 'Blimke'}})
|
385
402
|
c = Curl::Easy.http_post "http://www.example.com", 'user[first_name]=Bartosz', 'user[last_name]=Blimke'
|
386
403
|
expect(c.response_code).to eq(200)
|
387
404
|
end
|
388
405
|
|
389
406
|
it "should work with blank arguments for put" do
|
390
|
-
stub_request(:put, "www.example.com").with(:
|
407
|
+
stub_request(:put, "www.example.com").with(body: "01234")
|
391
408
|
c = Curl::Easy.new
|
392
409
|
c.url = "http://www.example.com"
|
393
410
|
c.put_data = "01234"
|
@@ -396,9 +413,9 @@ unless RUBY_PLATFORM =~ /java/
|
|
396
413
|
end
|
397
414
|
|
398
415
|
it "should work with multiple arguments for post" do
|
399
|
-
data = { :
|
416
|
+
data = { name: "john", address: "111 example ave" }
|
400
417
|
|
401
|
-
stub_request(:post, "www.example.com").with(:
|
418
|
+
stub_request(:post, "www.example.com").with(body: data)
|
402
419
|
c = Curl::Easy.new
|
403
420
|
c.url = "http://www.example.com"
|
404
421
|
c.http_post Curl::PostField.content('name', data[:name]), Curl::PostField.content('address', data[:address])
|
@@ -21,10 +21,10 @@ module CurbSpecHelper
|
|
21
21
|
end
|
22
22
|
|
23
23
|
OpenStruct.new(
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
24
|
+
body: curl.body_str,
|
25
|
+
headers: WebMock::Util::Headers.normalize_headers(response_headers),
|
26
|
+
status: curl.response_code.to_s,
|
27
|
+
message: status
|
28
28
|
)
|
29
29
|
end
|
30
30
|
|
@@ -13,7 +13,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
13
13
|
|
14
14
|
#functionality only supported for em-http-request 1.x
|
15
15
|
if defined?(EventMachine::HttpConnection)
|
16
|
-
context 'when a real request is made and redirects are followed', :
|
16
|
+
context 'when a real request is made and redirects are followed', net_connect: true do
|
17
17
|
before { WebMock.allow_net_connect! }
|
18
18
|
|
19
19
|
# This url redirects to the https URL.
|
@@ -22,7 +22,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
22
22
|
|
23
23
|
def make_request
|
24
24
|
EM.run do
|
25
|
-
request = EM::HttpRequest.new(http_url).get(:
|
25
|
+
request = EM::HttpRequest.new(http_url).get(redirects: 1)
|
26
26
|
request.callback { EM.stop }
|
27
27
|
end
|
28
28
|
end
|
@@ -49,7 +49,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
49
49
|
describe "with middleware" do
|
50
50
|
|
51
51
|
it "should work with request middleware" do
|
52
|
-
stub_request(:get, "www.example.com").with(:
|
52
|
+
stub_request(:get, "www.example.com").with(body: 'bar')
|
53
53
|
|
54
54
|
middleware = Class.new do
|
55
55
|
def request(client, head, body)
|
@@ -62,10 +62,10 @@ unless RUBY_PLATFORM =~ /java/
|
|
62
62
|
|
63
63
|
conn.use middleware
|
64
64
|
|
65
|
-
http = conn.get(:
|
65
|
+
http = conn.get(body: 'foo')
|
66
66
|
|
67
67
|
http.callback do
|
68
|
-
expect(WebMock).to have_requested(:get, "www.example.com").with(:
|
68
|
+
expect(WebMock).to have_requested(:get, "www.example.com").with(body: 'bar')
|
69
69
|
EM.stop
|
70
70
|
end
|
71
71
|
end
|
@@ -80,7 +80,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should work with response middleware" do
|
83
|
-
stub_request(:get, "www.example.com").to_return(:
|
83
|
+
stub_request(:get, "www.example.com").to_return(body: 'foo')
|
84
84
|
|
85
85
|
EM.run do
|
86
86
|
conn = EventMachine::HttpRequest.new('http://www.example.com/')
|
@@ -116,13 +116,13 @@ unless RUBY_PLATFORM =~ /java/
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
context 'making a real request', :
|
119
|
+
context 'making a real request', net_connect: true do
|
120
120
|
before { WebMock.allow_net_connect! }
|
121
121
|
include_examples "em-http-request middleware/after_request hook integration"
|
122
122
|
end
|
123
123
|
|
124
124
|
context 'when the request is stubbed' do
|
125
|
-
before { stub_request(:get, webmock_server_url).to_return(:
|
125
|
+
before { stub_request(:get, webmock_server_url).to_return(body: 'hello world') }
|
126
126
|
include_examples "em-http-request middleware/after_request hook integration"
|
127
127
|
end
|
128
128
|
end
|
@@ -160,11 +160,11 @@ unless RUBY_PLATFORM =~ /java/
|
|
160
160
|
end
|
161
161
|
|
162
162
|
it "should work" do
|
163
|
-
stub_request(:post, /.*.testserver.com*/).to_return(:
|
163
|
+
stub_request(:post, /.*.testserver.com*/).to_return(status: 200, body: 'ok')
|
164
164
|
expect {
|
165
165
|
EM.run do
|
166
166
|
fiber = Fiber.new do
|
167
|
-
EM::HttpRequest.new("http://www.testserver.com").post :
|
167
|
+
EM::HttpRequest.new("http://www.testserver.com").post body: "foo=bar&baz=bang", timeout: 60
|
168
168
|
EM.stop
|
169
169
|
end
|
170
170
|
fiber.resume
|
@@ -187,7 +187,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
187
187
|
end
|
188
188
|
|
189
189
|
it "should work with streaming" do
|
190
|
-
stub_request(:get, "www.example.com").to_return(:
|
190
|
+
stub_request(:get, "www.example.com").to_return(body: "abc")
|
191
191
|
response = ""
|
192
192
|
EM.run {
|
193
193
|
http = EventMachine::HttpRequest.new('http://www.example.com/').get
|
@@ -197,41 +197,41 @@ unless RUBY_PLATFORM =~ /java/
|
|
197
197
|
end
|
198
198
|
|
199
199
|
it "should work with responses that use chunked transfer encoding" do
|
200
|
-
stub_request(:get, "www.example.com").to_return(:
|
200
|
+
stub_request(:get, "www.example.com").to_return(body: "abc", headers: { 'Transfer-Encoding' => 'chunked' })
|
201
201
|
expect(http_request(:get, "http://www.example.com").body).to eq("abc")
|
202
202
|
end
|
203
203
|
|
204
204
|
it "should work with optional query params" do
|
205
|
-
stub_request(:get, "www.example.com/?x=3&a[]=b&a[]=c").to_return(:
|
206
|
-
expect(http_request(:get, "http://www.example.com/?x=3", :
|
205
|
+
stub_request(:get, "www.example.com/?x=3&a[]=b&a[]=c").to_return(body: "abc")
|
206
|
+
expect(http_request(:get, "http://www.example.com/?x=3", query: {"a" => ["b", "c"]}).body).to eq("abc")
|
207
207
|
end
|
208
208
|
|
209
209
|
it "should work with optional query params declared as string" do
|
210
|
-
stub_request(:get, "www.example.com/?x=3&a[]=b&a[]=c").to_return(:
|
211
|
-
expect(http_request(:get, "http://www.example.com/?x=3", :
|
210
|
+
stub_request(:get, "www.example.com/?x=3&a[]=b&a[]=c").to_return(body: "abc")
|
211
|
+
expect(http_request(:get, "http://www.example.com/?x=3", query: "a[]=b&a[]=c").body).to eq("abc")
|
212
212
|
end
|
213
213
|
|
214
214
|
it "should work when the body is passed as a Hash" do
|
215
|
-
stub_request(:post, "www.example.com").with(:
|
216
|
-
expect(http_request(:post, "http://www.example.com", :
|
215
|
+
stub_request(:post, "www.example.com").with(body: {a: "1", b: "2"}).to_return(body: "ok")
|
216
|
+
expect(http_request(:post, "http://www.example.com", body: {a: "1", b: "2"}).body).to eq("ok")
|
217
217
|
end
|
218
218
|
|
219
219
|
if defined?(EventMachine::HttpConnection)
|
220
220
|
it "should work when a file is passed as body" do
|
221
|
-
stub_request(:post, "www.example.com").with(:
|
222
|
-
expect(http_request(:post, "http://www.example.com", :
|
221
|
+
stub_request(:post, "www.example.com").with(body: File.read(__FILE__)).to_return(body: "ok")
|
222
|
+
expect(http_request(:post, "http://www.example.com", file: __FILE__).body).to eq("ok")
|
223
223
|
end
|
224
224
|
end
|
225
225
|
|
226
226
|
it "should work with UTF-8 strings" do
|
227
227
|
body = "Привет, Мир!"
|
228
|
-
stub_request(:post, "www.example.com").to_return(:
|
228
|
+
stub_request(:post, "www.example.com").to_return(body: body)
|
229
229
|
expect(http_request(:post, "http://www.example.com").body.bytesize).to eq(body.bytesize)
|
230
230
|
end
|
231
231
|
|
232
232
|
it "should work with multiple requests to the same connection" do
|
233
|
-
stub_request(:get, "www.example.com/foo").to_return(:
|
234
|
-
stub_request(:get, "www.example.com/baz").to_return(:
|
233
|
+
stub_request(:get, "www.example.com/foo").to_return(body: "bar")
|
234
|
+
stub_request(:get, "www.example.com/baz").to_return(body: "wombat")
|
235
235
|
err1 = nil
|
236
236
|
err2 = nil
|
237
237
|
body1 = nil
|
@@ -240,7 +240,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
240
240
|
|
241
241
|
EM.run do
|
242
242
|
conn = EM::HttpRequest.new("http://www.example.com")
|
243
|
-
conn.get(:
|
243
|
+
conn.get(path: "/foo").callback do |resp|
|
244
244
|
body1 = resp.response
|
245
245
|
i += 1; EM.stop if i == 2
|
246
246
|
end.errback do |resp|
|
@@ -248,7 +248,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
248
248
|
i += 1; EM.stop if i == 2
|
249
249
|
end
|
250
250
|
|
251
|
-
conn.get(:
|
251
|
+
conn.get(path: "/baz").callback do |resp|
|
252
252
|
body2 = resp.response
|
253
253
|
i += 1; EM.stop if i == 2
|
254
254
|
end.errback do |resp|
|
@@ -264,17 +264,17 @@ unless RUBY_PLATFORM =~ /java/
|
|
264
264
|
end
|
265
265
|
|
266
266
|
it "should work with multiple requests to the same connection when the first request times out" do
|
267
|
-
stub_request(:get, "www.example.com/foo").to_timeout.then.to_return(:
|
267
|
+
stub_request(:get, "www.example.com/foo").to_timeout.then.to_return(status: 200, body: "wombat")
|
268
268
|
err = nil
|
269
269
|
body = nil
|
270
270
|
|
271
271
|
EM.run do
|
272
272
|
conn = EM::HttpRequest.new("http://www.example.com")
|
273
|
-
conn.get(:
|
273
|
+
conn.get(path: "/foo").callback do |resp|
|
274
274
|
err = :success_from_timeout
|
275
275
|
EM.stop
|
276
276
|
end.errback do |resp|
|
277
|
-
conn.get(:
|
277
|
+
conn.get(path: "/foo").callback do |retry_resp|
|
278
278
|
expect(retry_resp.response_header.status).to eq(200)
|
279
279
|
body = retry_resp.response
|
280
280
|
EM.stop
|
@@ -319,7 +319,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
319
319
|
|
320
320
|
context "with a query" do
|
321
321
|
let(:uri) { "http://www.example.com/?a=1&b=2" }
|
322
|
-
subject { client("http://www.example.com/?a=1", :
|
322
|
+
subject { client("http://www.example.com/?a=1", query: { 'b' => 2 }) }
|
323
323
|
|
324
324
|
it "#request_signature doesn't mutate the original uri" do
|
325
325
|
expect(subject.uri).to eq(Addressable::URI.parse("http://www.example.com/?a=1"))
|
@@ -333,9 +333,9 @@ unless RUBY_PLATFORM =~ /java/
|
|
333
333
|
before(:each) do
|
334
334
|
stub_request(:get, "http://example.org/").
|
335
335
|
to_return(
|
336
|
-
:
|
337
|
-
:
|
338
|
-
:
|
336
|
+
status: 200,
|
337
|
+
body: "",
|
338
|
+
headers: { 'Set-Cookie' => cookie_string }
|
339
339
|
)
|
340
340
|
end
|
341
341
|
|