Skip to content

Commit ff321eb

Browse files
committed
cache-control: add must-revalidate, proxy-revalidate, s-maxage
1 parent 8b5b25f commit ff321eb

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/protocol/http/header/cache_control.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ class CacheControl < Split
1414
NO_CACHE = 'no-cache'
1515
NO_STORE = 'no-store'
1616
MAX_AGE = 'max-age'
17+
S_MAXAGE = 's-maxage'
1718

1819
STATIC = 'static'
1920
DYNAMIC = 'dynamic'
2021
STREAMING = 'streaming'
2122

23+
MUST_REVALIDATE = 'must-revalidate'
24+
PROXY_REVALIDATE = 'proxy-revalidate'
25+
2226
def initialize(value = nil)
2327
super(value&.downcase)
2428
end
@@ -55,11 +59,27 @@ def no_store?
5559
self.include?(NO_STORE)
5660
end
5761

62+
def must_revalidate?
63+
self.include?(MUST_REVALIDATE)
64+
end
65+
66+
def proxy_revalidate?
67+
self.include?(PROXY_REVALIDATE)
68+
end
69+
5870
def max_age
5971
if value = self.find{|value| value.start_with?(MAX_AGE)}
6072
_, age = value.split('=', 2)
6173

62-
return Integer(age)
74+
return Integer(age, exception: false)
75+
end
76+
end
77+
78+
def s_maxage
79+
if value = self.find{|value| value.start_with?(S_MAXAGE)}
80+
_, age = value.split('=', 2)
81+
82+
return Integer(age, exception: false)
6383
end
6484
end
6585
end

test/protocol/http/header/cache_control.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@
88
describe Protocol::HTTP::Header::CacheControl do
99
let(:header) {subject.new(description)}
1010

11-
with "max-age=60, public" do
11+
with "max-age=60, s-maxage=30, public" do
1212
it "correctly parses cache header" do
1313
expect(header).to have_attributes(
1414
public?: be == true,
1515
private?: be == false,
1616
max_age: be == 60,
17+
s_maxage: be == 30,
18+
)
19+
end
20+
end
21+
22+
with "max-age=invalid, s-maxage=invalid" do
23+
it "gracefully handles invalid values" do
24+
expect(header).to have_attributes(
25+
max_age: be == nil,
26+
s_maxage: be == nil,
1727
)
1828
end
1929
end
@@ -51,6 +61,22 @@
5161
end
5262
end
5363

64+
with "must-revalidate" do
65+
it "correctly parses cache header" do
66+
expect(header).to have_attributes(
67+
must_revalidate?: be == true,
68+
)
69+
end
70+
end
71+
72+
with "proxy-revalidate" do
73+
it "correctly parses cache header" do
74+
expect(header).to have_attributes(
75+
proxy_revalidate?: be == true,
76+
)
77+
end
78+
end
79+
5480
with "#<<" do
5581
let(:header) {subject.new}
5682

0 commit comments

Comments
 (0)