From d88191a77ac13c7d44c7545aea9892e2dd310520 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Gibson Date: Sun, 5 Feb 2017 00:04:36 +0000 Subject: [PATCH] Extract function --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ac597ac..122b219 100644 --- a/index.js +++ b/index.js @@ -120,10 +120,14 @@ module.exports = class CachePolicy { this._resHeaders.expires; } - satisfiesWithoutRevalidation(req) { + _assertRequestHasHeaders(req) { if (!req || !req.headers) { throw Error("Request headers missing"); } + } + + satisfiesWithoutRevalidation(req) { + this._assertRequestHasHeaders(req); // When presented with a request, a cache MUST NOT reuse a stored response, unless: // the presented request does not contain the no-cache pragma (Section 5.4), nor the no-cache cache directive, @@ -150,11 +154,15 @@ module.exports = class CachePolicy { } } + return this._requestMatches(req, false); + } + + _requestMatches(req, allowHeadMethod) { // The presented effective request URI and that of the stored response match, and return (!this._url || this._url === req.url) && (this._host === req.headers.host) && // the request method associated with the stored response allows it to be used for the presented request, and - (!req.method || this._method === req.method) && + (!req.method || this._method === req.method || (allowHeadMethod && 'HEAD' === req.method)) && // selecting header fields nominated by the stored response (if any) match those presented, and this._varyMatches(req); }