Skip to content

Commit 4fa0573

Browse files
authored
Add support for constructing headers with tail marker. (#89)
1 parent a65b2d3 commit 4fa0573

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

lib/protocol/http/headers.rb

Lines changed: 11 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.
@@ -108,6 +110,9 @@ def flatten
108110
# @attribute [Array] An array of `[key, value]` pairs.
109111
attr :fields
110112

113+
# @attribute [Integer | Nil] The index where trailers begin.
114+
attr :tail
115+
111116
# @returns [Array] The fields of the headers.
112117
def to_a
113118
@fields

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: 19 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,13 +39,14 @@
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

3646
with "#trailer?" do
3747
it "should not be a trailer" do
3848
expect(headers).not.to be(:trailer?)
49+
expect(headers.tail).to be_nil
3950
end
4051
end
4152

@@ -269,8 +280,10 @@
269280
with "#trailer!" do
270281
it "can add trailer" do
271282
headers.add("trailer", "etag")
283+
count = headers.fields.size
272284

273285
trailer = headers.trailer!
286+
expect(headers.tail).to be == count
274287

275288
headers.add("etag", "abcd")
276289

@@ -345,8 +358,8 @@
345358
describe Protocol::HTTP::Headers::Merged do
346359
let(:merged) do
347360
Protocol::HTTP::Headers::Merged.new(
348-
Protocol::HTTP::Headers.new("content-type" => "text/html"),
349-
Protocol::HTTP::Headers.new("content-encoding" => "gzip")
361+
Protocol::HTTP::Headers["content-type" => "text/html"],
362+
Protocol::HTTP::Headers["content-encoding" => "gzip"]
350363
)
351364
end
352365

@@ -382,8 +395,8 @@
382395
with "non-normalized case" do
383396
let(:merged) do
384397
Protocol::HTTP::Headers::Merged.new(
385-
Protocol::HTTP::Headers.new("Content-Type" => "text/html"),
386-
Protocol::HTTP::Headers.new("Content-Encoding" => "gzip")
398+
Protocol::HTTP::Headers["Content-Type" => "text/html"],
399+
Protocol::HTTP::Headers["Content-Encoding" => "gzip"]
387400
)
388401
end
389402

0 commit comments

Comments
 (0)