Skip to content

Commit 27edf97

Browse files
Expand cookie header coverage.
1 parent ad2070f commit 27edf97

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎test/protocol/http/header/cookie.rb‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright, 2022, by Herrick Fang.
66

77
require "protocol/http/header/cookie"
8+
require "protocol/http/headers"
89

910
describe Protocol::HTTP::Header::Cookie do
1011
let(:header) {subject.parse(description)}
@@ -36,6 +37,29 @@
3637
end
3738
end
3839

40+
with "empty=; token=abc==" do
41+
it "preserves empty values and equals signs" do
42+
expect(cookies["empty"].value).to be == ""
43+
expect(cookies["token"].value).to be == "abc=="
44+
end
45+
end
46+
47+
with "first=1 ; second=2;\tthird=3" do
48+
it "ignores whitespace around separators" do
49+
expect(cookies.transform_values(&:value)).to be == {
50+
"first" => "1",
51+
"second" => "2",
52+
"third" => "3",
53+
}
54+
end
55+
end
56+
57+
with "session=first; session=second" do
58+
it "uses the last value for duplicate names" do
59+
expect(cookies["session"].value).to be == "second"
60+
end
61+
end
62+
3963
with "multiple cookies" do
4064
let(:header) do
4165
cookie = subject.new
@@ -53,4 +77,20 @@
5377
expect(cookies).to have_keys("session", "user_id", "token")
5478
end
5579
end
80+
81+
it "parses cookies through protocol headers" do
82+
headers = Protocol::HTTP::Headers[[
83+
["cookie", "session=abc123; user_id=42"],
84+
["cookie", "token=xyz789"],
85+
]]
86+
87+
header = headers["cookie"]
88+
89+
expect(header).to be_a(subject)
90+
expect(header.to_h.transform_values(&:value)).to be == {
91+
"session" => "abc123",
92+
"user_id" => "42",
93+
"token" => "xyz789",
94+
}
95+
end
5696
end

‎test/protocol/http/header/set_cookie.rb‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2026, by Samuel Williams.
55

66
require "protocol/http/header/set_cookie"
7+
require "protocol/http/headers"
78

89
describe Protocol::HTTP::Header::SetCookie do
910
let(:header) do
@@ -19,6 +20,47 @@
1920
end
2021

2122
it "can extract parsed cookies" do
22-
expect(header.to_h).to have_keys("session", "theme")
23+
cookies = header.to_h
24+
25+
expect(cookies).to have_keys("session", "theme")
26+
expect(cookies["session"]).to have_attributes(
27+
value: be == "abc123",
28+
directives: be == {"Path" => "/"},
29+
)
30+
expect(cookies["theme"]).to have_attributes(
31+
value: be == "dark",
32+
directives: be == {"HttpOnly" => true},
33+
)
34+
end
35+
36+
it "preserves cookie attributes" do
37+
header = subject.parse("session=abc123; Path=/; HttpOnly; SameSite=Lax; Max-Age=3600; Expires=Wed, 21 Oct 2015 07:28:00 GMT")
38+
cookie = header.to_h["session"]
39+
40+
expect(cookie.directives).to be == {
41+
"Path" => "/",
42+
"HttpOnly" => true,
43+
"SameSite" => "Lax",
44+
"Max-Age" => "3600",
45+
"Expires" => "Wed, 21 Oct 2015 07:28:00 GMT",
46+
}
47+
end
48+
49+
it "preserves separate fields through protocol headers" do
50+
headers = Protocol::HTTP::Headers[[
51+
["set-cookie", "session=abc123; Path=/; HttpOnly"],
52+
["set-cookie", "theme=dark; SameSite=Lax"],
53+
]]
54+
55+
header = headers["set-cookie"]
56+
cookies = header.to_h
57+
58+
expect(header).to be_a(subject)
59+
expect(header).to be == [
60+
"session=abc123; Path=/; HttpOnly",
61+
"theme=dark; SameSite=Lax",
62+
]
63+
expect(cookies["session"].directives).to be == {"Path" => "/", "HttpOnly" => true}
64+
expect(cookies["theme"].directives).to be == {"SameSite" => "Lax"}
2365
end
2466
end

0 commit comments

Comments
 (0)