diff --git a/frontend/components/usage.js b/frontend/components/usage.js index 2f615b16c4ddce3337005ec272152b024111c84d..f61a57f8ba5ea6f58a5bcdf6d01fe1381720c0ee 100644 --- a/frontend/components/usage.js +++ b/frontend/components/usage.js @@ -215,7 +215,7 @@ export default class Usage extends React.PureComponent { <td> <code>?maxAge=3600</code> </td> - <td>Set the HTTP cache lifetime in secs (values below the default will be ignored)</td> + <td>Set the HTTP cache lifetime in secs (values below the default (currently 120 seconds) will be ignored)</td> </tr> </tbody> </table> diff --git a/lib/request-handler.js b/lib/request-handler.js index 8918666f73258ecd74ae19c1102c88d6b8186d89..80f34396bc6abc9e5e546446e995e2d962bbc75b 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -81,10 +81,9 @@ function handleRequest (makeBadge, handlerOptions) { return (queryParams, match, end, ask) => { const reqTime = new Date(); - let maxAge = parseInt(process.env.BADGE_MAX_AGE_SECONDS) || 0; + let maxAge = isInt(process.env.BADGE_MAX_AGE_SECONDS) ? parseInt(process.env.BADGE_MAX_AGE_SECONDS) : 120; if ( - queryParams.maxAge !== undefined - && /^[0-9]+$/.test(queryParams.maxAge) + isInt(queryParams.maxAge) && parseInt(queryParams.maxAge) > maxAge ) { // only queryParams.maxAge to override the default @@ -236,6 +235,10 @@ function clearRequestCache() { requestCache.clear(); } +function isInt(number) { + return number !== undefined && /^[0-9]+$/.test(number); +} + module.exports = { handleRequest, makeHandleRequestFn: makeBadge => handlerOptions => handleRequest(makeBadge, handlerOptions), diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 68f0b3d331d28a19fa9f01825d1e10162757dd15..1582ed56391709d0473d494d4982491e42459867 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -139,11 +139,12 @@ describe('The request handler', function() { expect(res.headers.get('cache-control')).to.equal('no-cache, no-store, must-revalidate'); }); - it('should set Cache-Control: no-cache, no-store, must-revalidate if BADGE_MAX_AGE_SECONDS not set', async function () { + it('should set the expires header to current time + 120 if BADGE_MAX_AGE_SECONDS not set', async function () { delete process.env.BADGE_MAX_AGE_SECONDS; const res = await fetch(`${baseUri}/testing/123.json`); - expect(res.headers.get('expires')).to.equal(res.headers.get('date')); - expect(res.headers.get('cache-control')).to.equal('no-cache, no-store, must-revalidate'); + const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 120000).toGMTString(); + expect(res.headers.get('expires')).to.equal(expectedExpiry); + expect(res.headers.get('cache-control')).to.equal('max-age=120'); }); describe('the cache key', function () {