Skip to content

Commit a65b2d3

Browse files
Add Headers#to_a method. (#88)
- Adds Headers#to_a method that returns the fields array. - Provides compatibility with standard Ruby array conversion pattern.
1 parent be65261 commit a65b2d3

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/protocol/http/headers.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ def flatten
108108
# @attribute [Array] An array of `[key, value]` pairs.
109109
attr :fields
110110

111+
# @returns [Array] The fields of the headers.
112+
def to_a
113+
@fields
114+
end
115+
111116
# @returns [Boolean] Whether there are any trailers.
112117
def trailer?
113118
@tail != nil

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Add `Protocol::HTTP::Headers#to_a` method that returns the fields array, providing compatibility with standard Ruby array conversion pattern.
6+
37
## v0.51.0
48

59
- `Protocol::HTTP::Headers` now raise a `DuplicateHeaderError` when a duplicate singleton header (e.g. `content-length`) is added.

test/protocol/http/headers.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@
148148
end
149149
end
150150

151+
with "#to_a" do
152+
it "should return the fields array" do
153+
expect(headers.to_a).to be == fields
154+
end
155+
156+
it "should return the same object as fields" do
157+
expect(headers.to_a).to be_equal(headers.fields)
158+
end
159+
160+
it "should return an array" do
161+
expect(headers.to_a).to be_a(Array)
162+
end
163+
end
164+
151165
with "#to_h" do
152166
it "should generate array values for duplicate keys" do
153167
expect(headers.to_h["set-cookie"]).to be == ["hello=world", "foo=bar"]

0 commit comments

Comments
 (0)