diff --git a/lib/request-handler.js b/lib/request-handler.js index 42e426e53b..4e6318524f 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -97,7 +97,7 @@ function handleRequest (handlerOptions) { // Use sindresorhus query-string because it sorts the keys, whereas the // builtin querystring module relies on the iteration order. const stringified = queryString.stringify(filteredQueryParams); - const cacheIndex = `match[0]?${stringified}`; + const cacheIndex = `${match[0]}?${stringified}`; // Should we return the data right away? const cached = requestCache.get(cacheIndex); @@ -199,5 +199,7 @@ function clearRequestCache() { module.exports = { handleRequest, - clearRequestCache + clearRequestCache, + // Expose for testing. + _requestCache: requestCache }; diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 5778640437..7fe072a0a4 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -8,7 +8,8 @@ const analytics = require('./analytics'); const { makeBadgeData: getBadgeData } = require('./badge-data'); const { handleRequest, - clearRequestCache + clearRequestCache, + _requestCache } = require('./request-handler'); const baseUri = `http://127.0.0.1:${config.port}`; @@ -113,6 +114,24 @@ describe('The request handler', function() { '/testing/123.svg?foo=2' ).then(() => { assert.equal(handlerCallCount, 1); }); }); + + describe('the cache key', function () { + const expectedCacheKey = '/testing/123.json?colorB=123&label=foo'; + it('should match expected and use canonical order - 1', function () { + return fetch(`${baseUri}/testing/123.json?colorB=123&label=foo`) + .then(res => { + assert.ok(res.ok); + assert.deepEqual([..._requestCache.cache.keys()], [expectedCacheKey]); + }); + }); + it('should match expected and use canonical order - 2', function () { + return fetch(`${baseUri}/testing/123.json?label=foo&colorB=123`) + .then(res => { + assert.ok(res.ok); + assert.deepEqual([..._requestCache.cache.keys()], [expectedCacheKey]); + }); + }); + }); }); describe('custom query parameters', function() {