Skip to content

Commit 6e18a55

Browse files
authored
Fix date/expires header parsing. (#37)
1 parent 46f895b commit 6e18a55

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

lib/protocol/http/header/date.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2020-2023, by Samuel Williams.
5+
6+
require 'time'
7+
8+
module Protocol
9+
module HTTP
10+
module Header
11+
class Date < String
12+
def << value
13+
replace(value)
14+
end
15+
16+
def to_time
17+
::Time.parse(self)
18+
end
19+
end
20+
end
21+
end
22+
end

lib/protocol/http/headers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require_relative 'header/etags'
1313
require_relative 'header/vary'
1414
require_relative 'header/authorization'
15+
require_relative 'header/date'
1516

1617
module Protocol
1718
module HTTP
@@ -239,6 +240,9 @@ def []= key, value
239240
# Custom headers:
240241
'set-cookie' => Header::SetCookie,
241242
'cookie' => Header::Cookie,
243+
244+
'date' => Header::Date,
245+
'expires' => Header::Date,
242246
}.tap{|hash| hash.default = Split}
243247

244248
# Delete all headers with the given key, and return the merged value.

test/protocol/http/header/date.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2023, by Samuel Williams.
5+
6+
require 'protocol/http/header/date'
7+
8+
describe Protocol::HTTP::Header::Date do
9+
let(:header) {subject.new(description)}
10+
11+
with 'Wed, 21 Oct 2015 07:28:00 GMT' do
12+
it "can parse time" do
13+
time = header.to_time
14+
expect(time).to be_a(::Time)
15+
16+
expect(time).to have_attributes(
17+
year: be == 2015,
18+
month: be == 10,
19+
mday: be == 21,
20+
hour: be == 7,
21+
min: be == 28,
22+
sec: be == 0
23+
)
24+
end
25+
end
26+
27+
with "#<<" do
28+
let(:header) {subject.new}
29+
30+
it "can replace values" do
31+
header << 'Wed, 21 Oct 2015 07:28:00 GMT'
32+
expect(header.to_time).to have_attributes(
33+
year: be == 2015,
34+
month: be == 10,
35+
mday: be == 21
36+
)
37+
38+
header << 'Wed, 22 Oct 2015 07:28:00 GMT'
39+
expect(header.to_time).to have_attributes(
40+
year: be == 2015,
41+
month: be == 10,
42+
mday: be == 22
43+
)
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)