diff --git a/lib/protocol/http/headers.rb b/lib/protocol/http/headers.rb index 2080353..69e386b 100644 --- a/lib/protocol/http/headers.rb +++ b/lib/protocol/http/headers.rb @@ -108,6 +108,11 @@ def flatten # @attribute [Array] An array of `[key, value]` pairs. attr :fields + # @returns [Array] The fields of the headers. + def to_a + @fields + end + # @returns [Boolean] Whether there are any trailers. def trailer? @tail != nil diff --git a/releases.md b/releases.md index d9f4b48..20ddb4f 100644 --- a/releases.md +++ b/releases.md @@ -1,5 +1,9 @@ # Releases +## Unreleased + + - Add `Protocol::HTTP::Headers#to_a` method that returns the fields array, providing compatibility with standard Ruby array conversion pattern. + ## v0.51.0 - `Protocol::HTTP::Headers` now raise a `DuplicateHeaderError` when a duplicate singleton header (e.g. `content-length`) is added. diff --git a/test/protocol/http/headers.rb b/test/protocol/http/headers.rb index dbb0955..3bb150f 100644 --- a/test/protocol/http/headers.rb +++ b/test/protocol/http/headers.rb @@ -148,6 +148,20 @@ end end + with "#to_a" do + it "should return the fields array" do + expect(headers.to_a).to be == fields + end + + it "should return the same object as fields" do + expect(headers.to_a).to be_equal(headers.fields) + end + + it "should return an array" do + expect(headers.to_a).to be_a(Array) + end + end + with "#to_h" do it "should generate array values for duplicate keys" do expect(headers.to_h["set-cookie"]).to be == ["hello=world", "foo=bar"]