/
cache_control_test.rb
226 lines (190 loc) · 6.26 KB
/
cache_control_test.rb
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require 'test_helper'
require 'lotus/router'
CacheControlRoutes = Lotus::Router.new do
get '/symbol', to: 'cache_control#symbol'
get '/symbols', to: 'cache_control#symbols'
get '/hash', to: 'cache_control#hash'
get '/private-and-public', to: 'cache_control#private_public'
end
ExpiresRoutes = Lotus::Router.new do
get '/symbol', to: 'expires#symbol'
get '/symbols', to: 'expires#symbols'
get '/hash', to: 'expires#hash'
end
ConditionalGetRoutes = Lotus::Router.new do
get '/etag', to: 'conditional_get#etag'
get '/last-modified', to: 'conditional_get#last_modified'
get '/etag-last-modified', to: 'conditional_get#etag_last_modified'
end
class CacheControlController
include Lotus::Controller
action 'Symbol' do
def call(params)
cache_control :private
end
end
action 'Symbols' do
def call(params)
cache_control :private, :no_cache, :no_store
end
end
action 'Hash' do
def call(params)
cache_control :public, :no_store, max_age: 900, s_maxage: 86400, min_fresh: 500, max_stale: 700
end
end
action 'PrivatePublic' do
def call(params)
cache_control :private, :public
end
end
end
class ExpiresController
include Lotus::Controller
action 'Symbol' do
def call(params)
expires 900, :private
end
end
action 'Symbols' do
def call(params)
expires 900, :private, :no_cache, :no_store
end
end
action 'Hash' do
def call(params)
expires 900, :public, :no_store, s_maxage: 86400, min_fresh: 500, max_stale: 700
end
end
end
class ConditionalGetController
include Lotus::Controller
action 'Etag' do
def call(params)
fresh etag: 'updated'
end
end
action 'LastModified' do
def call(params)
fresh last_modified: Time.now
end
end
action 'EtagLastModified' do
def call(params)
fresh etag: 'updated', last_modified: Time.now
end
end
end
describe 'Cache control' do
before do
@app = Rack::MockRequest.new(CacheControlRoutes)
end
it 'accepts a Symbol' do
response = @app.get('/symbol')
response.headers.fetch('Cache-Control').must_equal('private')
end
it 'accepts multiple Symbols' do
response = @app.get('/symbols')
response.headers.fetch('Cache-Control').split(', ').must_equal %w(private no-cache no-store)
end
it 'accepts a Hash' do
Time.stub(:now, Time.now) do
response = @app.get('/hash')
response.headers.fetch('Cache-Control').split(', ').must_equal %w(public no-store max-age=900 s-maxage=86400 min-fresh=500 max-stale=700)
end
end
describe "private and public directives" do
it "ignores public directive" do
response = @app.get('/private-and-public')
response.headers.fetch('Cache-Control').must_equal('private')
end
end
end
describe 'Expires' do
before do
@app = Rack::MockRequest.new(ExpiresRoutes)
end
it 'accepts a Symbol' do
Time.stub(:now, Time.now) do
response = @app.get('/symbol')
response.headers.fetch('Expires').must_equal (Time.now + 900).httpdate
response.headers.fetch('Cache-Control').split(', ').must_equal %w(private max-age=900)
end
end
it 'accepts multiple Symbols' do
Time.stub(:now, Time.now) do
response = @app.get('/symbols')
response.headers.fetch('Expires').must_equal (Time.now + 900).httpdate
response.headers.fetch('Cache-Control').split(', ').must_equal %w(private no-cache no-store max-age=900)
end
end
it 'accepts a Hash' do
Time.stub(:now, Time.now) do
response = @app.get('/hash')
response.headers.fetch('Expires').must_equal (Time.now + 900).httpdate
response.headers.fetch('Cache-Control').split(', ').must_equal %w(public no-store s-maxage=86400 min-fresh=500 max-stale=700 max-age=900)
end
end
end
describe 'Fresh' do
before do
@app = Rack::MockRequest.new(ConditionalGetRoutes)
end
describe 'etag' do
describe 'when etag matches HTTP_IF_NONE_MATCH header' do
it 'halts 304 not modified' do
response = @app.get('/etag', {'HTTP_IF_NONE_MATCH' => 'updated'})
response.status.must_equal 304
end
it 'keeps the same etag header' do
response = @app.get('/etag', {'HTTP_IF_NONE_MATCH' => 'outdated'})
response.headers.fetch('ETag').must_equal 'updated'
end
end
describe 'when etag does not match HTTP_IF_NONE_MATCH header' do
it 'completes request' do
response = @app.get('/etag', {'HTTP_IF_NONE_MATCH' => 'outdated'})
response.status.must_equal 200
end
it 'returns etag header' do
response = @app.get('/etag', {'HTTP_IF_NONE_MATCH' => 'outdated'})
response.headers.fetch('ETag').must_equal 'updated'
end
end
end
describe 'last_modified' do
describe 'when last modified is less than or equal to HTTP_IF_MODIFIED_SINCE header' do
before { @modified_since = Time.new(2014, 1, 8, 0, 0, 0) }
it 'halts 304 not modified' do
Time.stub(:now, @modified_since) do
response = @app.get('/last-modified', {'HTTP_IF_MODIFIED_SINCE' => @modified_since.httpdate})
response.status.must_equal 304
end
end
it 'keeps the same IfModifiedSince header' do
Time.stub(:now, @modified_since) do
response = @app.get('/last-modified', {'HTTP_IF_MODIFIED_SINCE' => @modified_since.httpdate})
response.headers.fetch('Last-Modified').must_equal @modified_since.httpdate
end
end
end
describe 'when last modified is bigger than HTTP_IF_MODIFIED_SINCE header' do
before do
@modified_since = Time.new(2014, 1, 8, 0, 0, 0)
@last_modified = Time.new(2014, 2, 8, 0, 0, 0)
end
it 'completes request' do
Time.stub(:now, @last_modified) do
response = @app.get('/last-modified', {'HTTP_IF_MODIFIED_SINCE' => @modified_since.httpdate})
response.status.must_equal 200
end
end
it 'returns etag header' do
Time.stub(:now, @last_modified) do
response = @app.get('/last-modified', {'HTTP_IF_MODIFIED_SINCE' => @modified_since.httpdate})
response.headers.fetch('Last-Modified').must_equal @last_modified.httpdate
end
end
end
end
end