diff --git a/index.js b/index.js index a373153..8a5bcab 100644 --- a/index.js +++ b/index.js @@ -221,7 +221,14 @@ module.exports = class CachePolicy { responseHeaders() { const headers = this._copyWithoutHopByHopHeaders(this._resHeaders); - headers.age = `${Math.round(this.age())}`; + const age = this.age(); + + // A cache SHOULD generate 113 warning if it heuristically chose a freshness + // lifetime greater than 24 hours and the response's age is greater than 24 hours. + if (age > 3600*24 && !this._hasExplicitExpiration() && this.maxAge() > 3600*24) { + headers.warning = (headers.warning ? `${headers.warning}, ` : '') + '113 - "rfc7234 5.5.4"'; + } + headers.age = `${Math.round(age)}`; return headers; } diff --git a/test/revalidatetest.js b/test/revalidatetest.js index 1f905ec..9dc7377 100644 --- a/test/revalidatetest.js +++ b/test/revalidatetest.js @@ -106,6 +106,7 @@ describe('Can be revalidated?', function() { const headers = cache.revalidationHeaders(simpleRequest); assertHeadersPassed(headers); assert.equal(headers['if-modified-since'], 'Tue, 15 Nov 1994 12:45:26 GMT'); + assert(!/113/.test(headers.warning)); }); it('not without validators', function() { @@ -113,6 +114,20 @@ describe('Can be revalidated?', function() { const headers = cache.revalidationHeaders(simpleRequest); assertHeadersPassed(headers); assertNoValidators(headers); + assert(!/113/.test(headers.warning)); + }) + + it('113 added', function() { + const veryOldResponse = { + headers: { + age: 3600*72, + 'last-modified': 'Tue, 15 Nov 1994 12:45:26 GMT', + }, + }; + + const cache = new CachePolicy(simpleRequest, veryOldResponse); + const headers = cache.responseHeaders(simpleRequest); + assert(/113/.test(headers.warning)); }) });