Skip to content

Commit e997103

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

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/protocol/http/headers.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ def extract(keys)
198198
# @parameter key [String] the header key.
199199
# @parameter value [String] the header value to assign.
200200
def add(key, value)
201-
self[key] = value
201+
if @indexed
202+
merge_into(@indexed, key.downcase, value)
203+
end
204+
205+
@fields << [key, value]
202206
end
203207

204208
# Set the specified header key to the specified value, replacing any existing header keys with the same name.
@@ -230,11 +234,8 @@ def merge(headers)
230234
# @parameter key [String] The header key.
231235
# @parameter value [String] The header value.
232236
def []= key, value
233-
if @indexed
234-
merge_into(@indexed, key.downcase, value)
235-
end
236-
237-
@fields << [key, value]
237+
# The value MUST be a string, so we convert it to a string to prevent errors later on.
238+
self.add(key, value.to_s)
238239
end
239240

240241
# The policy for various headers, including how they are merged and normalized.

test/protocol/http/headers.rb

Lines changed: 13 additions & 6 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

@@ -241,7 +248,7 @@
241248
it "can merge content-length" do
242249
headers.merge!("content-length" => 2)
243250

244-
expect(headers["content-length"]).to be == 2
251+
expect(headers["content-length"]).to be == "2"
245252
end
246253
end
247254

0 commit comments

Comments
 (0)