Skip to content

Commit f781332

Browse files
committed
Allow non-string value in Headers#[]=.
1 parent 2c346ea commit f781332

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/protocol/http/headers.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ def merge(headers)
230230
# @parameter key [String] The header key.
231231
# @parameter value [String] The header value.
232232
def []= key, value
233+
# The value MUST be a string, so we convert it to a string to prevent errors later on.
234+
value = value.to_s
235+
233236
if @indexed
234237
merge_into(@indexed, key.downcase, value)
235238
end

test/protocol/http/headers.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,25 @@
167167
end
168168

169169
with "#[]=" do
170-
it "can add field" do
170+
it "can add field with a String value" do
171+
headers["Content-Length"] = "1"
172+
173+
expect(headers.fields.last).to be == ["Content-Length", "1"]
174+
expect(headers["content-length"]).to be == "1"
175+
end
176+
177+
it "can add field with an Integer value" do
171178
headers["Content-Length"] = 1
172179

173-
expect(headers.fields.last).to be == ["Content-Length", 1]
174-
expect(headers["content-length"]).to be == 1
180+
expect(headers.fields.last).to be == ["Content-Length", "1"]
181+
expect(headers["content-length"]).to be == "1"
175182
end
176183

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

180-
headers["Content-Length"] = 1
181-
expect(headers["content-length"]).to be == 1
187+
headers["Content-Length"] = "1"
188+
expect(headers["content-length"]).to be == "1"
182189
end
183190
end
184191

0 commit comments

Comments
 (0)