Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions lib/protocol/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,18 @@ def extract(keys)
# @parameter key [String] the header key.
# @parameter value [String] the header value to assign.
def add(key, value)
self[key] = value
# The value MUST be a string, so we convert it to a string to prevent errors later on.
value = value.to_s

if @indexed
merge_into(@indexed, key.downcase, value)
end

@fields << [key, value]
end

alias []= add

# Set the specified header key to the specified value, replacing any existing header keys with the same name.
#
# @parameter key [String] the header key to replace.
Expand All @@ -214,7 +223,7 @@ def set(key, value)
# Merge the headers into this instance.
def merge!(headers)
headers.each do |key, value|
self[key] = value
self.add(key, value)
end

return self
Expand All @@ -225,18 +234,6 @@ def merge(headers)
self.dup.merge!(headers)
end

# Append the value to the given key. Some values can be appended multiple times, others can only be set once.
#
# @parameter key [String] The header key.
# @parameter value [String] The header value.
def []= key, value
if @indexed
merge_into(@indexed, key.downcase, value)
end

@fields << [key, value]
end

# The policy for various headers, including how they are merged and normalized.
POLICY = {
# Headers which may only be specified once:
Expand Down
1 change: 1 addition & 0 deletions releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- `Protocol::HTTP::Headers` now raise a `DuplicateHeaderError` when a duplicate singleton header (e.g. `content-length`) is added.
- `Protocol::HTTP::Headers#add` now coerces the value to a string when adding a header, ensuring consistent behaviour.

## v0.50.0

Expand Down
23 changes: 15 additions & 8 deletions test/protocol/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,34 @@
end

with "#[]=" do
it "can add field" do
it "can add field with a String value" do
headers["Content-Length"] = "1"

expect(headers.fields.last).to be == ["Content-Length", "1"]
expect(headers["content-length"]).to be == "1"
end

it "can add field with an Integer value" do
headers["Content-Length"] = 1

expect(headers.fields.last).to be == ["Content-Length", 1]
expect(headers["content-length"]).to be == 1
expect(headers.fields.last).to be == ["Content-Length", "1"]
expect(headers["content-length"]).to be == "1"
end

it "can add field with indexed hash" do
expect(headers.to_h).not.to be(:empty?)

headers["Content-Length"] = 1
expect(headers["content-length"]).to be == 1
headers["Content-Length"] = "1"
expect(headers["content-length"]).to be == "1"
end
end

with "#add" do
it "can add field" do
headers.add("Content-Length", 1)

expect(headers.fields.last).to be == ["Content-Length", 1]
expect(headers["content-length"]).to be == 1
expect(headers.fields.last).to be == ["Content-Length", "1"]
expect(headers["content-length"]).to be == "1"
end
end

Expand Down Expand Up @@ -241,7 +248,7 @@
it "can merge content-length" do
headers.merge!("content-length" => 2)

expect(headers["content-length"]).to be == 2
expect(headers["content-length"]).to be == "2"
end
end

Expand Down
Loading