_isPathMatch(mockedResponse, pathToMatch) { const { path, isRegexPath } = mockedResponse; if (path instanceof RegExp || isRegexPath) { const pathRegex = path instanceof RegExp ? path : path.slice(1, -1); const pathRegExp = new RegExp(pathRegex); return pathRegExp.test(pathToMatch); } const parsedPathToMatch = queryString.parseUrl(pathToMatch); const parsedPath = queryString.parseUrl(path); return deepEqual(parsedPathToMatch, parsedPath); }
const cache = () => { return async(req, res, next) => { const parseUrl = queryString.parseUrl(req.url); const stringifyQuery = queryString.stringify(parseUrl.query); const cacheKey = `__req__${parseUrl.url}?${stringifyQuery}`; let cachedResponse = null; try { cachedResponse = await redisClient.get(cacheKey); } catch (err) { console.error('Error retrieving response from cache', err); } if (cachedResponse) { res.set('Cache-Control', `private, max-age=${config.get('cache.maxAge')}`); res.json(JSON.parse(cachedResponse)); return; } else { res.sendResponse = res.send; res.send = async(body) => { await redisClient.set(cacheKey, body); res.sendResponse(body); } next(); } }; }