Skip to content

Commit 81d71eb

Browse files
committed
Add support for constructing headers with tail marker.
1 parent a65b2d3 commit 81d71eb

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

lib/protocol/http/headers.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ def self.[] headers
6464
# Initialize the headers with the specified fields.
6565
#
6666
# @parameter fields [Array] An array of `[key, value]` pairs.
67-
# @parameter indexed [Hash] A hash table of normalized headers, if available.
68-
def initialize(fields = [], indexed = nil)
67+
# @parameter tail [Integer | Nil] The index of the trailer start in the @fields array.
68+
def initialize(fields = [], tail = nil, indexed: nil)
6969
@fields = fields
70-
@indexed = indexed
7170

72-
# Marks where trailer start in the @fields array.
73-
@tail = nil
71+
# Marks where trailer start in the @fields array:
72+
@tail = tail
73+
74+
# The cached index of headers:
75+
@indexed = nil
7476
end
7577

7678
# Initialize a copy of the headers.
@@ -86,8 +88,8 @@ def initialize_dup(other)
8688
# Clear all headers.
8789
def clear
8890
@fields.clear
89-
@indexed = nil
9091
@tail = nil
92+
@indexed = nil
9193
end
9294

9395
# Flatten trailer into the headers, in-place.

releases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Add `Protocol::HTTP::Headers#to_a` method that returns the fields array, providing compatibility with standard Ruby array conversion pattern.
6+
- Expose `tail` in `Headers.new` so that trailers can be accurately reproduced.
67

78
## v0.51.0
89

test/protocol/http/headers.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,25 @@
1010
let(:fields) do
1111
[
1212
["Content-Type", "text/html"],
13+
["connection", "Keep-Alive"],
1314
["Set-Cookie", "hello=world"],
1415
["Accept", "*/*"],
1516
["set-cookie", "foo=bar"],
16-
["connection", "Keep-Alive"]
1717
]
1818
end
1919

2020
let(:headers) {subject[fields]}
2121

22+
with ".new" do
23+
it "can construct headers with trailers" do
24+
headers = subject.new(fields, 4)
25+
expect(headers).to be(:trailer?)
26+
expect(headers.trailer.to_a).to be == [
27+
["set-cookie", "foo=bar"],
28+
]
29+
end
30+
end
31+
2232
with ".[]" do
2333
it "can be constructed from frozen array" do
2434
self.fields.freeze
@@ -29,7 +39,7 @@
2939

3040
with "#keys" do
3141
it "should return keys" do
32-
expect(headers.keys).to be == ["content-type", "set-cookie", "accept", "connection"]
42+
expect(headers.keys).to be == ["content-type", "connection", "set-cookie", "accept"]
3343
end
3444
end
3545

@@ -345,8 +355,8 @@
345355
describe Protocol::HTTP::Headers::Merged do
346356
let(:merged) do
347357
Protocol::HTTP::Headers::Merged.new(
348-
Protocol::HTTP::Headers.new("content-type" => "text/html"),
349-
Protocol::HTTP::Headers.new("content-encoding" => "gzip")
358+
Protocol::HTTP::Headers["content-type" => "text/html"],
359+
Protocol::HTTP::Headers["content-encoding" => "gzip"]
350360
)
351361
end
352362

@@ -382,8 +392,8 @@
382392
with "non-normalized case" do
383393
let(:merged) do
384394
Protocol::HTTP::Headers::Merged.new(
385-
Protocol::HTTP::Headers.new("Content-Type" => "text/html"),
386-
Protocol::HTTP::Headers.new("Content-Encoding" => "gzip")
395+
Protocol::HTTP::Headers["Content-Type" => "text/html"],
396+
Protocol::HTTP::Headers["Content-Encoding" => "gzip"]
387397
)
388398
end
389399

0 commit comments

Comments
 (0)