Skip to content

Commit

Permalink
fix: preserve rfc7234 5.5.4 warnings
Browse files Browse the repository at this point in the history
If a cached object is more than 24 hours old, and it has a heuristically
chosen expiration date of more than 24 hours (thus resulting in it being
served from cache), then a 113 warning should be applied

http-cache-semantics provides this facility, but we were not taking
advantage of it.
  • Loading branch information
isaacs authored and claudiahdz committed Oct 1, 2019
1 parent 119e479 commit 001b91e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ function isStale (req, res) {
policy._responseTime = new Date(responseTime)

const bool = !policy.satisfiesWithoutRevalidation(_req)
const headers = policy.responseHeaders()
if (headers.warning && /^113\b/.test(headers.warning)) {
// Possible to pick up a rfc7234 warning at this point.
// This is kind of a weird place to stick this, should probably go
// in cachingFetch. But by putting it here, we save an extra
// CachePolicy object construction.
res.headers.append('warning', headers.warning)
}
return bool
}

Expand Down
25 changes: 25 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,31 @@ test('heuristic freshness lifetime', t => {
})
})

test('heuristic age warning', t => {
const srv = tnock(t, HOST)
// just a very old thing
srv.get('/heuristic').reply(200, CONTENT, {
age: 3600 * 72,
'last-modified': 'Tue, 15 Nov 1994 12:45:26 GMT',
date: 'Tue, 15 Nov 1994 12:45:26 GMT'
})
return fetch(`${HOST}/heuristic`, {
cacheManager: CACHE,
retry: {retries: 0}
}).then(res => res.buffer().then(body => {
t.equal(res.headers.get('warning'), null, 'no warnings')
t.deepEqual(body, CONTENT, 'got remote content')
return fetch(`${HOST}/heuristic`, {
cacheManager: CACHE,
retry: {retries: 0}
})
})).then(res => {
t.equal(res.status, 200, 'got 200 response')
t.same(res.headers.get('warning'), '113 - "rfc7234 5.5.4"')
return res.buffer()
})
})

test('refreshes cached request on HEAD request', t => {
const srv = tnock(t, HOST)
srv.get('/test').reply(200, CONTENT, {
Expand Down

0 comments on commit 001b91e

Please sign in to comment.