Skip to content

Commit

Permalink
Add JSON API support
Browse files Browse the repository at this point in the history
  • Loading branch information
ouranos committed Jan 3, 2018
1 parent d987916 commit 6948175
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
21 changes: 11 additions & 10 deletions lib/webmock/request_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,17 @@ class BodyPattern
include RSpecMatcherDetector

BODY_FORMATS = {
'text/xml' => :xml,
'application/xml' => :xml,
'application/json' => :json,
'text/json' => :json,
'application/javascript' => :json,
'text/javascript' => :json,
'text/html' => :html,
'application/x-yaml' => :yaml,
'text/yaml' => :yaml,
'text/plain' => :plain
'text/xml' => :xml,
'application/xml' => :xml,
'application/json' => :json,
'application/vnd.api+json' => :json,
'text/json' => :json,
'application/javascript' => :json,
'text/javascript' => :json,
'text/html' => :html,
'application/x-yaml' => :yaml,
'text/yaml' => :yaml,
'text/plain' => :plain
}

attr_reader :pattern
Expand Down
2 changes: 1 addition & 1 deletion lib/webmock/request_signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def url_encoded?
end

def json_headers?
!!(headers && headers['Content-Type'] == 'application/json')
!!(headers && ['application/vnd.api+json', 'application/json'].include?(headers['Content-Type']))
end

private
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/request_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ def match(request_signature)
body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
end

it "should match when content type is set to json api" do
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/vnd.api+json'},
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
end

it "should not match when body is not json" do
expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).
not_to match(WebMock::RequestSignature.new(:post, "www.example.com",
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/request_signature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@
expect(subject.json_headers?).to be true
end

it "returns true if the headers are JSON API" do
subject.headers = { "Content-Type" => "application/vnd.api+json" }
expect(subject.json_headers?).to be true
end

it "returns false if the headers are NOT json" do
subject.headers = { "Content-Type" => "application/made-up-format" }
expect(subject.json_headers?).to be false
Expand Down

0 comments on commit 6948175

Please sign in to comment.