-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathhttp_cache_spec.rb
More file actions
351 lines (284 loc) · 12.4 KB
/
http_cache_spec.rb
File metadata and controls
351 lines (284 loc) · 12.4 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# frozen_string_literal: true
require 'spec_helper'
describe Faraday::HttpCache do
let(:logger) { double('a Logger object', debug: nil, warn: nil) }
let(:options) { { logger: logger } }
let(:client) do
Faraday.new(url: ENV['FARADAY_SERVER']) do |stack|
stack.use Faraday::HttpCache, options
adapter = ENV['FARADAY_ADAPTER']
stack.headers['X-Faraday-Adapter'] = adapter
stack.headers['Content-Type'] = 'application/x-www-form-urlencoded'
stack.adapter adapter.to_sym
end
end
before do
client.get('clear')
end
it 'does not cache POST requests' do
client.post('post').body
expect(client.post('post').body).to eq('2')
end
it 'logs that a POST request is unacceptable' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [POST /post] unacceptable, delete') }
client.post('post').body
end
it 'does not cache responses with , status code' do
client.get('broken')
expect(client.get('broken').body).to eq('2')
end
it 'adds a trace of the actions performed to the env' do
response = client.post('post')
expect(response.env[:http_cache_trace]).to eq(%i[unacceptable delete])
end
describe 'cache invalidation' do
it 'expires POST requests' do
client.get('counter')
client.post('counter')
expect(client.get('counter').body).to eq('2')
end
it 'logs that a POST request was deleted from the cache' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [POST /counter] unacceptable, delete') }
client.post('counter')
end
it 'does not expires POST requests that failed' do
client.get('get')
client.post('get')
expect(client.get('get').body).to eq('1')
end
it 'expires PUT requests' do
client.get('counter')
client.put('counter')
expect(client.get('counter').body).to eq('2')
end
it 'logs that a PUT request was deleted from the cache' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [PUT /counter] unacceptable, delete') }
client.put('counter')
end
it 'expires DELETE requests' do
client.get('counter')
client.delete('counter')
expect(client.get('counter').body).to eq('2')
end
it 'logs that a DELETE request was deleted from the cache' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [DELETE /counter] unacceptable, delete') }
client.delete('counter')
end
it 'expires PATCH requests' do
client.get('counter')
client.patch('counter')
expect(client.get('counter').body).to eq('2')
end
it 'logs that a PATCH request was deleted from the cache' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [PATCH /counter] unacceptable, delete') }
client.patch('counter')
end
it 'logs that a response with a bad status code is uncacheable' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /broken] miss, uncacheable') }
client.get('broken')
end
it 'expires entries for the "Location" header' do
client.get('get')
client.post('delete-with-location')
expect(client.get('get').body).to eq('2')
end
it 'expires entries for the "Content-Location" header' do
client.get('get')
client.post('delete-with-content-location')
expect(client.get('get').body).to eq('2')
end
end
describe 'when acting as a shared cache' do
let(:options) { { logger: logger, shared_cache: true } }
it 'does not cache requests with a private cache control' do
client.get('private')
expect(client.get('private').body).to eq('2')
end
it 'logs that a private response is uncacheable' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /private] miss, uncacheable') }
client.get('private')
end
end
describe 'when acting as a private cache' do
let(:options) { { logger: logger, shared_cache: false } }
it 'does cache requests with a private cache control' do
client.get('private')
expect(client.get('private').body).to eq('1')
end
it 'logs that a private response is stored' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /private] miss, store') }
client.get('private')
end
end
it 'does not cache responses with a explicit no-store directive' do
client.get('dontstore')
expect(client.get('dontstore').body).to eq('2')
end
it 'logs that a response with a no-store directive is uncacheable' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /dontstore] miss, uncacheable') }
client.get('dontstore')
end
it 'does not caches multiple responses when the headers differ' do
client.get('get', nil, 'HTTP_ACCEPT' => 'text/html')
expect(client.get('get', nil, 'HTTP_ACCEPT' => 'text/html').body).to eq('1')
expect(client.get('get', nil, 'HTTP_ACCEPT' => 'application/json').body).to eq('1')
end
it 'caches multiples responses based on the "Vary" header' do
client.get('vary', nil, 'User-Agent' => 'Agent/1.0')
expect(client.get('vary', nil, 'User-Agent' => 'Agent/1.0').body).to eq('1')
expect(client.get('vary', nil, 'User-Agent' => 'Agent/2.0').body).to eq('2')
expect(client.get('vary', nil, 'User-Agent' => 'Agent/3.0').body).to eq('3')
end
it 'never caches responses with the wildcard "Vary" header' do
client.get('vary-wildcard')
expect(client.get('vary-wildcard').body).to eq('2')
end
it 'caches requests with the "Expires" header' do
client.get('expires')
expect(client.get('expires').body).to eq('1')
end
it 'logs that a request with the "Expires" is fresh and stored' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /expires] miss, store') }
client.get('expires')
end
it 'caches GET responses' do
client.get('get')
expect(client.get('get').body).to eq('1')
end
context 'when the request has a "no-cache" directive' do
it 'revalidates the cache' do
expect(client.get('etag').body).to eq('1')
expect(client.get('etag', nil, 'Cache-Control' => 'no-cache').body).to eq('1')
expect(client.get('get', nil).body).to eq('2')
expect(client.get('etag', nil, 'Cache-Control' => 'no-cache').body).to eq('3')
end
it 'caches the response' do
client.get('get', nil, 'Cache-Control' => 'no-cache')
expect(client.get('get', nil).body).to eq('1')
end
end
context 'when the response has a "no-cache" directive' do
it 'always revalidate the cached response' do
client.get('no_cache')
expect(client.get('no_cache').body).to eq('2')
expect(client.get('no_cache').body).to eq('3')
end
end
it 'logs that a GET response is stored' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /get] miss, store') }
client.get('get')
end
it 'differs requests with different query strings in the log' do
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /get] miss, store') }
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /get?q=what] miss, store') }
client.get('get')
client.get('get', q: 'what')
end
it 'logs that a stored GET response is fresh' do
client.get('get')
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /get] fresh') }
client.get('get')
end
describe 'stale-while-revalidate' do
let(:on_stale) { double('stale callback', call: nil) }
let(:options) { { logger: logger, on_stale: on_stale } }
it 'serves stale cached responses within stale-while-revalidate window' do
expect(client.get('stale-while-revalidate').body).to eq('1')
response = client.get('stale-while-revalidate')
expect(response.body).to eq('1')
expect(response.env[:http_cache_trace]).to eq([:stale])
end
it 'invokes the on_stale callback with request, env and cached response' do
client.get('stale-while-revalidate')
expect(on_stale).to receive(:call).with(
request: an_instance_of(Faraday::HttpCache::Request),
env: an_instance_of(Faraday::Env),
cached_response: an_instance_of(Faraday::HttpCache::Response)
)
client.get('stale-while-revalidate')
end
it 'ignores on_stale callback errors and still serves stale response' do
failing_callback = lambda do |request:, env:, cached_response:|
request && env && cached_response
raise 'boom'
end
local_client = Faraday.new(url: ENV['FARADAY_SERVER']) do |stack|
stack.use Faraday::HttpCache, logger: logger, on_stale: failing_callback
adapter = ENV['FARADAY_ADAPTER']
stack.headers['X-Faraday-Adapter'] = adapter
stack.headers['Content-Type'] = 'application/x-www-form-urlencoded'
stack.adapter adapter.to_sym
end
local_client.get('stale-while-revalidate')
expect(logger).to receive(:warn).with(/on_stale callback failed: RuntimeError: boom/)
response = local_client.get('stale-while-revalidate')
expect(response.body).to eq('1')
expect(response.env[:http_cache_trace]).to eq([:stale])
end
it 'revalidates when stale-while-revalidate window has expired' do
expect(client.get('stale-while-revalidate-expired').body).to eq('1')
response = client.get('stale-while-revalidate-expired')
expect(response.body).to eq('1')
expect(response.env[:http_cache_trace]).to eq(%i[must_revalidate valid store])
end
end
it 'sends the "Last-Modified" header on response validation' do
client.get('timestamped')
expect(client.get('timestamped').body).to eq('1')
end
it 'logs that the request with "Last-Modified" was revalidated' do
client.get('timestamped')
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /timestamped] must_revalidate, valid, store') }
expect(client.get('timestamped').body).to eq('1')
end
it 'sends the "If-None-Match" header on response validation' do
client.get('etag')
expect(client.get('etag').body).to eq('1')
end
it 'logs that the request with "ETag" was revalidated' do
client.get('etag')
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /etag] must_revalidate, valid, store') }
expect(client.get('etag').body).to eq('1')
end
it 'maintains the "Date" header for cached responses' do
first_date = client.get('get').headers['Date']
second_date = client.get('get').headers['Date']
expect(first_date).to eq(second_date)
end
it 'preserves an old "Date" header if present' do
date = client.get('yesterday').headers['Date']
expect(date).to match(/^\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/)
end
it 'updates the "Cache-Control" header when a response is validated' do
first_cache_control = client.get('etag').headers['Cache-Control']
second_cache_control = client.get('etag').headers['Cache-Control']
expect(first_cache_control).not_to eql(second_cache_control)
end
it 'updates the "Date" header when a response is validated' do
first_date = client.get('etag').headers['Date']
second_date = client.get('etag').headers['Date']
expect(first_date).not_to eql(second_date)
end
it 'updates the "Expires" header when a response is validated' do
first_expires = client.get('etag').headers['Expires']
second_expires = client.get('etag').headers['Expires']
expect(first_expires).not_to eql(second_expires)
end
it 'updates the "Vary" header when a response is validated' do
first_vary = client.get('etag').headers['Vary']
second_vary = client.get('etag').headers['Vary']
expect(first_vary).not_to eql(second_vary)
end
it 'caches non-stale response with "must-revalidate" directive' do
client.get('must-revalidate')
expect(client.get('must-revalidate').body).to eq('1')
end
describe 'Configuration options' do
let(:app) { double('it is an app!') }
it 'uses the options to create a Cache Store' do
store = double(read: nil, write: nil)
expect(Faraday::HttpCache::Strategies::ByUrl).to receive(:new).with(hash_including(store: store))
Faraday::HttpCache.new(app, store: store)
end
end
end