Skip to content

Commit cbb10d5

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

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
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.

test/protocol/http/headers.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@
99
describe Protocol::HTTP::Headers do
1010
let(:fields) do
1111
[
12+
["connection", "Keep-Alive"],
1213
["Content-Type", "text/html"],
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

0 commit comments

Comments
 (0)