Skip to content

Commit

Permalink
Merge pull request #969 from Timmitry/allow-content-type-headers-to-s…
Browse files Browse the repository at this point in the history
…pecify-charset

Allow content type headers to specify a charset
  • Loading branch information
bblimke committed Aug 2, 2022
2 parents 524016e + 58e3c92 commit 7b9e1c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/webmock/request_signature.rb
Expand Up @@ -35,11 +35,11 @@ def eql?(other)
alias == eql?

def url_encoded?
!!(headers && headers['Content-Type'] == 'application/x-www-form-urlencoded')
!!(headers&.fetch('Content-Type', nil)&.start_with?('application/x-www-form-urlencoded'))
end

def json_headers?
!!(headers && headers['Content-Type'] == 'application/json')
!!(headers&.fetch('Content-Type', nil)&.start_with?('application/json'))
end

private
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/request_signature_spec.rb
Expand Up @@ -125,11 +125,21 @@
expect(subject.url_encoded?).to be true
end

it "returns true if the headers are urlencoded with a specified charset" do
subject.headers = { "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8" }
expect(subject.url_encoded?).to be true
end

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

it "returns false when no content type header is present" do
subject.headers = { "Some-Header" => "some-value" }
expect(subject.url_encoded?).to be false
end

it "returns false when no headers are set" do
subject.headers = nil
expect(subject.url_encoded?).to be false
Expand All @@ -142,11 +152,21 @@
expect(subject.json_headers?).to be true
end

it "returns true if the headers are json with a specified charset" do
subject.headers = { "Content-Type" => "application/json; charset=UTF-8" }
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
end

it "returns false when no content type header is present" do
subject.headers = { "Some-Header" => "some-value" }
expect(subject.json_headers?).to be false
end

it "returns false when no headers are set" do
subject.headers = nil
expect(subject.json_headers?).to be false
Expand Down

0 comments on commit 7b9e1c0

Please sign in to comment.