Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON API support #734

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/webmock/request_pattern.rb
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
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
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
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