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

Keep underscores when normalizing headers #964

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
3 changes: 2 additions & 1 deletion lib/webmock/util/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Headers
def self.normalize_headers(headers)
return nil unless headers
array = headers.map { |name, value|
[name.to_s.split(/_|-/).map { |segment| segment.capitalize }.join("-"),
name = name.to_s.tr("_", "-") if name.is_a? Symbol
[name.to_s.split(/(_|-)/).map { |segment| segment.capitalize }.join,
case value
when Regexp then value
when Array then (value.size == 1) ? value.first.to_s : value.map {|v| v.to_s}.sort
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/request_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,9 @@ def match(request_signature)
not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))
end

it "should match even is header keys are declared in different form" do
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'ContentLength' => '8888', 'Content-type' => 'image/png'})).
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {:ContentLength => 8888, 'content_type' => 'image/png'}))
it "should match even if header keys are declared in different form" do
expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'ContentLength' => '8888', 'Content-Type' => 'image/png'})).
to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {:ContentLength => 8888, 'content-type' => 'image/png'}))
end

it "should match is pattern doesn't have specified headers" do
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/util/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@

end

describe ".normalize_headers" do
it "normalizes capitalization for string header names" do
expect(described_class.normalize_headers({ "foo-BAR-bAz" => "qUx" }))
.to eq({ "Foo-Bar-Baz" => "qUx" })
end

it "stringifies and dasherizes symbol header names" do
expect(described_class.normalize_headers({ foo_bar: "qUx" }))
.to eq({ "Foo-Bar" => "qUx" })
end

it "respects choice of underscores for string header names" do
expect(described_class.normalize_headers({ "foo-BAR_bAz" => "qUx" }))
.to eq({ "Foo-Bar_Baz" => "qUx" })
end
end
end