webmock 3.11.3 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57ec99bb8b1ce01a51c062c634c17abe5cda03777fbb43d4aec3dd12175c44bf
4
- data.tar.gz: 55c9f75174da8a3c63ac600d8199cef85350a6b525e39c44e8c514383a5d3432
3
+ metadata.gz: 6a63f193f9a6fb608fd8e50b8484cd06bf2ee29765cbe0347b74a6a8e82677be
4
+ data.tar.gz: bf7b5341dc79029a57c793feb7a7fee2a411a5187f05b1240a3e890842b6300d
5
5
  SHA512:
6
- metadata.gz: 9d7ed44a6d69e2a6fef4cf5636e6ee1a525485bccc3951735f8d3470901c9f05b9d61da09dc6b2ac39a558aa8f42d2d40c8b29a9048d21853af080f8f8a6912b
7
- data.tar.gz: 2e0a3617e7eac5a0cbe7a63def37747f5e3948a284b40aa996cb519463d73c3cfdec206963f5eff351259f9af7c2a11fd6eacab49e6fff930327c1fb42d6f30f
6
+ metadata.gz: fbde90987073665a627807880917906602ad10f661f2df95ae038e0e465839bcf98ef76bbc889d0368aa037518ffc0a85239e8cb66a6538600702b0ab72e9cfd
7
+ data.tar.gz: b47709a8f994adee29b5d6808580af3980dd45b2dd5a539b678897da4779ee306da8d1137bdca324ad8d314e2d0e8f14cb04ce8ea27de4932002c6b2763f3ec1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ # 3.12.0
4
+
5
+ * Added support for handling custom JSON and XML content types e.g. 'application/vnd.api+json'
6
+
3
7
  # 3.11.3
4
8
 
5
9
  * Fixed async-http adapter to only considered requests as real if they are real.
data/README.md CHANGED
@@ -1156,6 +1156,7 @@ People who submitted patches and new features or suggested improvements. Many th
1156
1156
  * Oleksiy Kovyrin
1157
1157
  * Matt Larraz
1158
1158
  * Tony Schneider
1159
+ * Niklas Hösl
1159
1160
 
1160
1161
  For a full list of contributors you can visit the
1161
1162
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -295,8 +295,9 @@ module WebMock
295
295
  end
296
296
 
297
297
  private
298
+
298
299
  def body_as_hash(body, content_type)
299
- case BODY_FORMATS[content_type]
300
+ case body_format(content_type)
300
301
  when :json then
301
302
  WebMock::Util::JSON.parse(body)
302
303
  when :xml then
@@ -306,6 +307,11 @@ module WebMock
306
307
  end
307
308
  end
308
309
 
310
+ def body_format(content_type)
311
+ normalized_content_type = content_type.sub(/\A(application\/)[a-zA-Z0-9.-]+\+(json|xml)\Z/,'\1\2')
312
+ BODY_FORMATS[normalized_content_type]
313
+ end
314
+
309
315
  def assert_non_multipart_body(content_type)
310
316
  if content_type =~ %r{^multipart/form-data}
311
317
  raise ArgumentError.new("WebMock does not support matching body for multipart/form-data requests yet :(")
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '3.11.3' unless defined?(::WebMock::VERSION)
2
+ VERSION = '3.12.0' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -534,69 +534,93 @@ describe WebMock::RequestPattern do
534
534
  end
535
535
 
536
536
  describe "for request with json body and content type is set to json" do
537
- it "should match when hash matches body" do
538
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
539
- to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/json'},
540
- body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
541
- end
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
542
543
 
543
- it "should match if hash matches body in different form" do
544
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
545
- to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/json'},
546
- body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
547
- end
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
548
549
 
549
- it "should not match when body is not json" do
550
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
551
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
552
- headers: {content_type: 'application/json'}, body: "foo bar"))
553
- end
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
555
+
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
554
561
 
555
- it "should not match if request body is different" do
556
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
557
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
558
- headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":null}"))
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
559
573
  end
560
574
 
561
- it "should not match if request body is has less params than pattern" do
562
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).
563
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
564
- 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"
565
578
  end
566
579
 
567
- it "should not match if request body is has more params than pattern" do
568
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1})).
569
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
570
- 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"
571
583
  end
572
584
  end
573
585
 
574
586
  describe "for request with xml body and content type is set to xml" do
575
587
  let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
576
588
 
577
- it "should match when hash matches body" do
578
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
579
- to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml'},
580
- body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
581
- end
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
582
595
 
583
- it "should match if hash matches body in different form" do
584
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
585
- to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml'},
586
- body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
587
- end
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
588
601
 
589
- it "should not match when body is not xml" do
590
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
591
- not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
592
- headers: {content_type: 'application/xml'}, body: "foo bar"))
593
- end
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
594
607
 
595
- it "matches when the content type include a charset" do
596
- expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
597
- to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml;charset=UTF-8'},
598
- body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
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/json" do
617
+ let(:content_type) { 'application/xml' }
618
+ it_behaves_like "a xml body"
619
+ end
599
620
 
621
+ context "custom json content type" do
622
+ let(:content_type) { 'application/atom+xml' }
623
+ it_behaves_like "a xml body"
600
624
  end
601
625
  end
602
626
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.11.3
4
+ version: 3.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-25 00:00:00.000000000 Z
11
+ date: 2021-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -422,9 +422,9 @@ licenses:
422
422
  - MIT
423
423
  metadata:
424
424
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
425
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.11.3/CHANGELOG.md
426
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.11.3
427
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.11.3
425
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.12.0/CHANGELOG.md
426
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.12.0
427
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.12.0
428
428
  wiki_uri: https://github.com/bblimke/webmock/wiki
429
429
  post_install_message:
430
430
  rdoc_options: []