Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased default BADGE_MAX_AGE_SECONDS #1846

Merged
merged 4 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/components/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
9 changes: 6 additions & 3 deletions lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions lib/request-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down