File: cache_control.rb

package info (click to toggle)
ruby-protocol-http 0.59.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ruby: 7,612; makefile: 4
file content (124 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023-2025, by Samuel Williams.
# Copyright, 2023, by Thomas Morgan.

require "protocol/http/header/cache_control"

describe Protocol::HTTP::Header::CacheControl do
	let(:header) {subject.parse(description)}
	
	with "max-age=60, s-maxage=30, public" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				public?: be == true,
				private?: be == false,
				max_age: be == 60,
				s_maxage: be == 30,
			)
		end
	end
	
	with "max-age=-10, s-maxage=0x22" do
		it "gracefully handles invalid values" do
			expect(header).to have_attributes(
				max_age: be == nil,
				s_maxage: be == nil,
			)
		end
	end
	
	with "no-cache, no-store" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				no_cache?: be == true,
				no_store?: be == true,
			)
		end
	end
	
	with "static" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				static?: be == true,
			)
		end
	end
	
	with "dynamic" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				dynamic?: be == true,
			)
		end
	end
	
	with "streaming" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				streaming?: be == true,
			)
		end
	end
	
	with "must-revalidate" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				must_revalidate?: be == true,
			)
		end
	end
	
	with "proxy-revalidate" do
		it "correctly parses cache header" do
			expect(header).to have_attributes(
				proxy_revalidate?: be == true,
			)
		end
	end
	
	with "#<<" do
		let(:header) {subject.new}
		
		it "can append values" do
			header << "max-age=60"
			expect(header).to have_attributes(
				max_age: be == 60,
			)
		end
	end
	
	with ".coerce" do
		it "normalizes array values to lowercase" do
			header = subject.coerce(["PUBLIC", "NO-CACHE"])
			expect(header).to be(:include?, "public")
			expect(header).to be(:include?, "no-cache")
			expect(header).not.to be(:include?, "PUBLIC")
		end
		
		it "normalizes string values to lowercase" do
			header = subject.coerce("PUBLIC, MAX-AGE=60")
			expect(header).to be(:include?, "public")
			expect(header).to be(:include?, "max-age=60")
		end
	end
	
	with ".new" do
		it "preserves case when given array" do
			header = subject.new(["PUBLIC", "NO-CACHE"])
			expect(header).to be(:include?, "PUBLIC")
			expect(header).to be(:include?, "NO-CACHE")
		end
		
		it "normalizes when given string (backward compatibility)" do
			header = subject.new("PUBLIC, MAX-AGE=60")
			expect(header).to be(:include?, "public")
			expect(header).to be(:include?, "max-age=60")
		end
		
		it "raises ArgumentError for invalid value types" do
			expect{subject.new(123)}.to raise_exception(ArgumentError)
		end
	end
end