diff --git a/lib/webmock/request_signature.rb b/lib/webmock/request_signature.rb index 70686f40..949b874a 100644 --- a/lib/webmock/request_signature.rb +++ b/lib/webmock/request_signature.rb @@ -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 diff --git a/spec/unit/request_signature_spec.rb b/spec/unit/request_signature_spec.rb index 02a5ec17..a330b57f 100644 --- a/spec/unit/request_signature_spec.rb +++ b/spec/unit/request_signature_spec.rb @@ -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 @@ -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