webmock 3.6.2 → 3.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/CI.yml +37 -0
- data/CHANGELOG.md +214 -0
- data/Gemfile +1 -1
- data/README.md +85 -32
- data/Rakefile +12 -2
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +216 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +4 -0
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +7 -4
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +3 -0
- data/lib/webmock/http_lib_adapters/http_rb/client.rb +4 -1
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +24 -3
- data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +2 -2
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +1 -1
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +23 -6
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +24 -9
- data/lib/webmock/http_lib_adapters/net_http.rb +43 -19
- data/lib/webmock/request_pattern.rb +82 -47
- data/lib/webmock/response.rb +11 -5
- data/lib/webmock/rspec.rb +2 -1
- data/lib/webmock/stub_registry.rb +26 -11
- data/lib/webmock/test_unit.rb +1 -3
- data/lib/webmock/util/uri.rb +5 -4
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +5 -3
- data/lib/webmock.rb +1 -0
- data/spec/acceptance/async_http_client/async_http_client_spec.rb +375 -0
- data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
- data/spec/acceptance/curb/curb_spec.rb +12 -5
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +56 -0
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +1 -1
- data/spec/acceptance/excon/excon_spec_helper.rb +2 -0
- data/spec/acceptance/http_rb/http_rb_spec.rb +11 -0
- data/spec/acceptance/manticore/manticore_spec.rb +51 -0
- data/spec/acceptance/net_http/net_http_spec.rb +38 -0
- data/spec/acceptance/patron/patron_spec_helper.rb +2 -2
- data/spec/acceptance/shared/callbacks.rb +2 -1
- data/spec/acceptance/shared/returning_declared_responses.rb +36 -15
- data/spec/acceptance/shared/stubbing_requests.rb +35 -0
- data/spec/unit/request_pattern_spec.rb +183 -48
- data/spec/unit/response_spec.rb +22 -18
- data/spec/unit/util/uri_spec.rb +10 -0
- data/spec/unit/webmock_spec.rb +52 -11
- data/test/test_webmock.rb +6 -0
- data/webmock.gemspec +11 -1
- metadata +48 -10
- data/.travis.yml +0 -19
@@ -111,7 +111,8 @@ shared_context "callbacks" do |*adapter_info|
|
|
111
111
|
end
|
112
112
|
|
113
113
|
it "should pass real response to callback with headers" do
|
114
|
-
expect(@response.headers["
|
114
|
+
expect(@response.headers["X-Powered-By"]).to eq( "ASP.NET")
|
115
|
+
expect(@response.headers["Content-Length"]).to eq("11") unless adapter_info.include?(:no_content_length_header)
|
115
116
|
end
|
116
117
|
|
117
118
|
it "should pass response to callback with body" do
|
@@ -64,7 +64,10 @@ shared_context "declared responses" do |*adapter_info|
|
|
64
64
|
it "should return response with declared headers" do
|
65
65
|
stub_request(:get, "www.example.com").to_return(headers: SAMPLE_HEADERS)
|
66
66
|
response = http_request(:get, "http://www.example.com/")
|
67
|
-
expect(response.headers["
|
67
|
+
expect(response.headers["Accept"]).to eq("application/json")
|
68
|
+
unless adapter_info.include?(:no_content_length_header)
|
69
|
+
expect(response.headers["Content-Length"]).to eq("8888")
|
70
|
+
end
|
68
71
|
end
|
69
72
|
|
70
73
|
it "should return response with declared headers even if there are multiple headers with the same key" do
|
@@ -171,13 +174,22 @@ shared_context "declared responses" do |*adapter_info|
|
|
171
174
|
end
|
172
175
|
|
173
176
|
it "should return recorded headers" do
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
177
|
+
if adapter_info.include?(:no_content_length_header)
|
178
|
+
expect(@response.headers).to eq({
|
179
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
180
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
181
|
+
"Connection"=>"Keep-Alive",
|
182
|
+
"Accept"=>"image/jpeg, image/png"
|
183
|
+
})
|
184
|
+
else
|
185
|
+
expect(@response.headers).to eq({
|
186
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
187
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
188
|
+
"Content-Length"=>"419",
|
189
|
+
"Connection"=>"Keep-Alive",
|
190
|
+
"Accept"=>"image/jpeg, image/png"
|
191
|
+
})
|
192
|
+
end
|
181
193
|
end
|
182
194
|
|
183
195
|
it "should return recorded body" do
|
@@ -205,13 +217,22 @@ shared_context "declared responses" do |*adapter_info|
|
|
205
217
|
end
|
206
218
|
|
207
219
|
it "should return recorded headers" do
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
220
|
+
if adapter_info.include?(:no_content_length_header)
|
221
|
+
expect(@response.headers).to eq({
|
222
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
223
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
224
|
+
"Connection"=>"Keep-Alive",
|
225
|
+
"Accept"=>"image/jpeg, image/png"
|
226
|
+
})
|
227
|
+
else
|
228
|
+
expect(@response.headers).to eq({
|
229
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
230
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
231
|
+
"Content-Length"=>"419",
|
232
|
+
"Connection"=>"Keep-Alive",
|
233
|
+
"Accept"=>"image/jpeg, image/png"
|
234
|
+
})
|
235
|
+
end
|
215
236
|
end
|
216
237
|
|
217
238
|
it "should return recorded body" do
|
@@ -593,6 +593,23 @@ shared_examples_for "stubbing requests" do |*adapter_info|
|
|
593
593
|
end
|
594
594
|
end
|
595
595
|
end
|
596
|
+
|
597
|
+
context "when global stub should be invoked last" do
|
598
|
+
before do
|
599
|
+
WebMock.globally_stub_request(:after_local_stubs) do
|
600
|
+
{ body: "global stub body" }
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
it "uses global stub when non-global stub is not defined" do
|
605
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
|
606
|
+
end
|
607
|
+
|
608
|
+
it "uses non-global stub first" do
|
609
|
+
stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
|
610
|
+
expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
|
611
|
+
end
|
612
|
+
end
|
596
613
|
end
|
597
614
|
|
598
615
|
describe "when stubbing request with a block evaluated on request" do
|
@@ -640,4 +657,22 @@ shared_examples_for "stubbing requests" do |*adapter_info|
|
|
640
657
|
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
|
641
658
|
end
|
642
659
|
end
|
660
|
+
|
661
|
+
describe "in Rspec around(:each) hook" do
|
662
|
+
# order goes
|
663
|
+
# around(:each)
|
664
|
+
# before(:each)
|
665
|
+
# after(:each)
|
666
|
+
# anything after example.run in around(:each)
|
667
|
+
around(:each) do |example|
|
668
|
+
example.run
|
669
|
+
expect {
|
670
|
+
http_request(:get, "http://www.example.com/")
|
671
|
+
}.to_not raise_error # WebMock::NetConnectNotAllowedError
|
672
|
+
end
|
673
|
+
|
674
|
+
it "should still allow me to make a mocked request" do
|
675
|
+
stub_request(:get, "www.example.com")
|
676
|
+
end
|
677
|
+
end
|
643
678
|
end
|
@@ -111,6 +111,21 @@ describe WebMock::RequestPattern do
|
|
111
111
|
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
112
112
|
end
|
113
113
|
|
114
|
+
it "should match if uri matches requesst uri as URI object" do
|
115
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"))).
|
116
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should match if uri proc pattern returning true" do
|
120
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true })).
|
121
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should not match if uri proc pattern returns false" do
|
125
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { false })).
|
126
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))
|
127
|
+
end
|
128
|
+
|
114
129
|
it "should match if uri Addressable::Template pattern matches unescaped form of request uri" do
|
115
130
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{any_path}"))).
|
116
131
|
to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
|
@@ -132,6 +147,30 @@ describe WebMock::RequestPattern do
|
|
132
147
|
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
133
148
|
end
|
134
149
|
|
150
|
+
it "should match if Addressable::Template pattern that has ip address host without port matches request uri" do
|
151
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1/1234")
|
152
|
+
uri = Addressable::Template.new("127.0.0.1/{id}")
|
153
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should match if Addressable::Template pattern host matches request uri" do
|
157
|
+
signature = WebMock::RequestSignature.new(:get, "www.example.com")
|
158
|
+
uri = Addressable::Template.new("{subdomain}.example.com")
|
159
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should not match if Addressable::Template pattern host does not match request uri" do
|
163
|
+
signature = WebMock::RequestSignature.new(:get, "www.bad-example.com")
|
164
|
+
uri = Addressable::Template.new("{subdomain}.example.com")
|
165
|
+
expect(WebMock::RequestPattern.new(:get, uri)).not_to match(signature)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should match if uri Addressable::Template pattern matches request uri without a schema and a path " do
|
169
|
+
signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000")
|
170
|
+
uri = Addressable::Template.new("127.0.0.1:3000")
|
171
|
+
expect(WebMock::RequestPattern.new(:get, uri)).to match(signature)
|
172
|
+
end
|
173
|
+
|
135
174
|
it "should match for uris with same parameters as pattern" do
|
136
175
|
expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).
|
137
176
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
|
@@ -199,7 +238,7 @@ describe WebMock::RequestPattern do
|
|
199
238
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
200
239
|
end
|
201
240
|
|
202
|
-
it "should match request query params if params don't match" do
|
241
|
+
it "should not match request query params if params don't match" do
|
203
242
|
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).
|
204
243
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
205
244
|
end
|
@@ -229,13 +268,85 @@ describe WebMock::RequestPattern do
|
|
229
268
|
end
|
230
269
|
end
|
231
270
|
|
271
|
+
describe "when uri is described as URI" do
|
272
|
+
it "should match request query params" do
|
273
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"), query: {"a" => ["b", "c"]})).
|
274
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should not match request query params if params don't match" do
|
278
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"), query: {"x" => ["b", "c"]})).
|
279
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
283
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
284
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
285
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
289
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
290
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
291
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
295
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
296
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
297
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
301
|
+
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"),
|
302
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
303
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe "when uri is described as a proc" do
|
308
|
+
it "should match request query params" do
|
309
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"a" => ["b", "c"]})).
|
310
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should not match request query params if params don't match" do
|
314
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"x" => ["b", "c"]})).
|
315
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should match when query params are declared as HashIncluding matcher matching params" do
|
319
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
320
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
321
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should not match when query params are declared as HashIncluding matcher not matching params" do
|
325
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
326
|
+
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
|
327
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
|
331
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
332
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
|
333
|
+
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
|
337
|
+
expect(WebMock::RequestPattern.new(:get, ->(uri) { true },
|
338
|
+
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
|
339
|
+
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
232
343
|
describe "when uri is described as Addressable::Template" do
|
233
344
|
it "should raise error if query params are specified" do
|
234
345
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"a" => ["b", "c"]})).
|
235
346
|
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
236
347
|
end
|
237
348
|
|
238
|
-
it "should match request query params if params don't match" do
|
349
|
+
it "should not match request query params if params don't match" do
|
239
350
|
expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"x" => ["b", "c"]})).
|
240
351
|
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
|
241
352
|
end
|
@@ -423,69 +534,93 @@ describe WebMock::RequestPattern do
|
|
423
534
|
end
|
424
535
|
|
425
536
|
describe "for request with json body and content type is set to json" do
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
537
|
+
shared_examples "a json body" do
|
538
|
+
it "should match when hash matches body" do
|
539
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
540
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
541
|
+
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
|
542
|
+
end
|
431
543
|
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
544
|
+
it "should match if hash matches body in different form" do
|
545
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
546
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
547
|
+
body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
|
548
|
+
end
|
437
549
|
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
550
|
+
it "should not match when body is not json" do
|
551
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
552
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
553
|
+
headers: {content_type: content_type}, body: "foo bar"))
|
554
|
+
end
|
443
555
|
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
556
|
+
it "should not match if request body is different" do
|
557
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
|
558
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
559
|
+
headers: {content_type: content_type}, body: "{\"a\":1,\"c\":null}"))
|
560
|
+
end
|
561
|
+
|
562
|
+
it "should not match if request body is has less params than pattern" do
|
563
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
|
564
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
565
|
+
headers: {content_type: content_type}, body: "{\"a\":1}"))
|
566
|
+
end
|
567
|
+
|
568
|
+
it "should not match if request body is has more params than pattern" do
|
569
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1})).
|
570
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
571
|
+
headers: {content_type: content_type}, body: "{\"a\":1,\"c\":1}"))
|
572
|
+
end
|
448
573
|
end
|
449
574
|
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
headers: {content_type: 'application/json'}, body: "{\"a\":1}"))
|
575
|
+
context "standard application/json" do
|
576
|
+
let(:content_type) { 'application/json' }
|
577
|
+
it_behaves_like "a json body"
|
454
578
|
end
|
455
579
|
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":1}"))
|
580
|
+
context "custom json content type" do
|
581
|
+
let(:content_type) { 'application/vnd.api+json' }
|
582
|
+
it_behaves_like "a json body"
|
460
583
|
end
|
461
584
|
end
|
462
585
|
|
463
586
|
describe "for request with xml body and content type is set to xml" do
|
464
587
|
let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
|
465
588
|
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
589
|
+
shared_examples "a xml body" do
|
590
|
+
it "should match when hash matches body" do
|
591
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
592
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
593
|
+
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
594
|
+
end
|
471
595
|
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
596
|
+
it "should match if hash matches body in different form" do
|
597
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
598
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: content_type},
|
599
|
+
body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
600
|
+
end
|
477
601
|
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
602
|
+
it "should not match when body is not xml" do
|
603
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
604
|
+
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
|
605
|
+
headers: {content_type: content_type}, body: "foo bar"))
|
606
|
+
end
|
483
607
|
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
608
|
+
it "matches when the content type include a charset" do
|
609
|
+
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
|
610
|
+
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: "#{content_type};charset=UTF-8"},
|
611
|
+
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
|
612
|
+
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
context "standard application/xml" do
|
617
|
+
let(:content_type) { 'application/xml' }
|
618
|
+
it_behaves_like "a xml body"
|
619
|
+
end
|
488
620
|
|
621
|
+
context "custom xml content type" do
|
622
|
+
let(:content_type) { 'application/atom+xml' }
|
623
|
+
it_behaves_like "a xml body"
|
489
624
|
end
|
490
625
|
end
|
491
626
|
end
|
data/spec/unit/response_spec.rb
CHANGED
@@ -130,11 +130,15 @@ describe WebMock::Response do
|
|
130
130
|
# Users of webmock commonly make the mistake of stubbing the response
|
131
131
|
# body to return a hash, to prevent this:
|
132
132
|
#
|
133
|
-
it "should error if
|
133
|
+
it "should error if given a non-allowed type: a hash" do
|
134
134
|
expect { WebMock::Response.new(body: Hash.new) }.to \
|
135
135
|
raise_error(WebMock::Response::InvalidBody)
|
136
136
|
end
|
137
137
|
|
138
|
+
it "should error if given a non-allowed type: something that is not a hash" do
|
139
|
+
expect { WebMock::Response.new(body: 123) }.to \
|
140
|
+
raise_error(WebMock::Response::InvalidBody)
|
141
|
+
end
|
138
142
|
end
|
139
143
|
|
140
144
|
describe "from raw response" do
|
@@ -152,12 +156,12 @@ describe WebMock::Response do
|
|
152
156
|
|
153
157
|
it "should read headers" do
|
154
158
|
expect(@response.headers).to eq({
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
159
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
160
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
161
|
+
"Content-Length"=>"419",
|
162
|
+
"Connection"=>"Keep-Alive",
|
163
|
+
"Accept"=>"image/jpeg, image/png"
|
164
|
+
})
|
161
165
|
end
|
162
166
|
|
163
167
|
it "should read body" do
|
@@ -182,12 +186,12 @@ describe WebMock::Response do
|
|
182
186
|
|
183
187
|
it "should read headers" do
|
184
188
|
expect(@response.headers).to eq({
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
189
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
190
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
191
|
+
"Content-Length"=>"419",
|
192
|
+
"Connection"=>"Keep-Alive",
|
193
|
+
"Accept"=>"image/jpeg, image/png"
|
194
|
+
})
|
191
195
|
end
|
192
196
|
|
193
197
|
it "should read body" do
|
@@ -234,11 +238,11 @@ describe WebMock::Response do
|
|
234
238
|
it "should evaluate new response with evaluated options" do
|
235
239
|
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc", headers: {'A' => 'a'})
|
236
240
|
response = WebMock::DynamicResponse.new(lambda {|request|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
241
|
+
{
|
242
|
+
body: request.body,
|
243
|
+
headers: request.headers,
|
244
|
+
status: 302
|
245
|
+
}
|
242
246
|
})
|
243
247
|
evaluated_response = response.evaluate(request_signature)
|
244
248
|
expect(evaluated_response.body).to eq("abc")
|
data/spec/unit/util/uri_spec.rb
CHANGED
@@ -177,6 +177,16 @@ describe WebMock::Util::URI do
|
|
177
177
|
end
|
178
178
|
end
|
179
179
|
|
180
|
+
it "should find all variations of uris with https, basic auth, a non-standard port and a path" do
|
181
|
+
uri = "https://~%8A:pass@www.example.com:9000/foo"
|
182
|
+
variations = [
|
183
|
+
"https://~%8A:pass@www.example.com:9000/foo",
|
184
|
+
"https://~\x8A:pass@www.example.com:9000/foo".force_encoding(Encoding::ASCII_8BIT)
|
185
|
+
]
|
186
|
+
|
187
|
+
expect(WebMock::Util::URI.variations_of_uri_as_strings(uri)).to eq(variations)
|
188
|
+
end
|
189
|
+
|
180
190
|
end
|
181
191
|
|
182
192
|
describe "normalized uri equality" do
|
data/spec/unit/webmock_spec.rb
CHANGED
@@ -1,19 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "WebMock
|
4
|
-
it "should report version" do
|
5
|
-
expect(WebMock.version).to eq(WebMock::VERSION)
|
6
|
-
end
|
3
|
+
describe "WebMock" do
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
describe ".version" do
|
6
|
+
it "should report version" do
|
7
|
+
expect(WebMock.version).to eq(WebMock::VERSION)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should not require safe_yaml" do
|
11
|
+
expect(defined?SafeYAML).to eq(nil)
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
it "should alias enable_net_connect! to allow_net_connect!" do
|
15
|
+
expect(WebMock.method(:enable_net_connect!)).to eq(WebMock.method(:allow_net_connect!))
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should alias disallow_net_connect! to disable_net_connect!" do
|
19
|
+
expect(WebMock.method(:disallow_net_connect!)).to eq(WebMock.method(:disable_net_connect!))
|
20
|
+
end
|
14
21
|
end
|
15
22
|
|
16
|
-
|
17
|
-
|
23
|
+
describe ".net_connect_allowed?" do
|
24
|
+
context 'enabled globally' do
|
25
|
+
before do
|
26
|
+
WebMock.enable_net_connect!
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'without arguments' do
|
30
|
+
it 'returns WebMock::Config.instance.allow_net_connect' do
|
31
|
+
expect(WebMock.net_connect_allowed?).to eql(true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'disabled with allowed remote string' do
|
37
|
+
before do
|
38
|
+
WebMock.disable_net_connect!(allow: "http://192.168.64.2:20031")
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'without arguments' do
|
42
|
+
it 'returns WebMock::Config.instance.allow_net_connect' do
|
43
|
+
expect(WebMock.net_connect_allowed?).to eql(false)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'disabled globally' do
|
49
|
+
before do
|
50
|
+
WebMock.disable_net_connect!
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'without arguments' do
|
54
|
+
it 'returns WebMock::Config.instance.allow_net_connect' do
|
55
|
+
expect(WebMock.net_connect_allowed?).to eql(false)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
18
59
|
end
|
19
60
|
end
|
data/test/test_webmock.rb
CHANGED
@@ -3,4 +3,10 @@ require File.expand_path(File.dirname(__FILE__) + '/shared_test')
|
|
3
3
|
|
4
4
|
class TestWebMock < Test::Unit::TestCase
|
5
5
|
include SharedTest
|
6
|
+
|
7
|
+
def teardown
|
8
|
+
# Ensure global Test::Unit teardown was called
|
9
|
+
assert_empty WebMock::RequestRegistry.instance.requested_signatures.hash
|
10
|
+
assert_empty WebMock::StubRegistry.instance.request_stubs
|
11
|
+
end
|
6
12
|
end
|
data/webmock.gemspec
CHANGED
@@ -13,9 +13,17 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = %q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
|
14
14
|
s.license = "MIT"
|
15
15
|
|
16
|
+
s.metadata = {
|
17
|
+
'bug_tracker_uri' => 'https://github.com/bblimke/webmock/issues',
|
18
|
+
'changelog_uri' => "https://github.com/bblimke/webmock/blob/v#{s.version}/CHANGELOG.md",
|
19
|
+
'documentation_uri' => "https://www.rubydoc.info/gems/webmock/#{s.version}",
|
20
|
+
'source_code_uri' => "https://github.com/bblimke/webmock/tree/v#{s.version}",
|
21
|
+
'wiki_uri' => 'https://github.com/bblimke/webmock/wiki'
|
22
|
+
}
|
23
|
+
|
16
24
|
s.required_ruby_version = '>= 2.0'
|
17
25
|
|
18
|
-
s.add_dependency 'addressable', '>= 2.
|
26
|
+
s.add_dependency 'addressable', '>= 2.8.0'
|
19
27
|
s.add_dependency 'crack', '>= 0.3.2'
|
20
28
|
s.add_dependency 'hashdiff', ['>= 0.4.0', '< 2.0.0']
|
21
29
|
|
@@ -33,9 +41,11 @@ Gem::Specification.new do |s|
|
|
33
41
|
s.add_development_dependency 'em-http-request', '>= 1.0.2'
|
34
42
|
s.add_development_dependency 'em-synchrony', '>= 1.0.0'
|
35
43
|
s.add_development_dependency 'excon', '>= 0.27.5'
|
44
|
+
s.add_development_dependency 'async-http', '>= 0.48.0'
|
36
45
|
s.add_development_dependency 'minitest', '>= 5.0.0'
|
37
46
|
s.add_development_dependency 'test-unit', '>= 3.0.0'
|
38
47
|
s.add_development_dependency 'rdoc', '> 3.5.0'
|
48
|
+
s.add_development_dependency 'webrick'
|
39
49
|
|
40
50
|
s.files = `git ls-files`.split("\n")
|
41
51
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|