Skip to content

Commit

Permalink
Check max-age=0 for responses
Browse files Browse the repository at this point in the history
  • Loading branch information
d11wtq committed Aug 30, 2012
1 parent e6c6906 commit 35fe6c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/http-cache/validators.js
Expand Up @@ -87,6 +87,16 @@ Validators.response.noStore = function(req, res, evaluator) {
evaluator.flagStorable(false);
};

/**
* Checks if response cache-control states max-age=0, rendering it uncacheable.
*
* RFC 2616 Section 14.9.
*/
Validators.response.maxAgeZero = function(req, res, evaluator) {
if ((evaluator.headers['cache-control'] || '').match(/max-age=(0|-[0-9]+)/))
evaluator.flagStorable(false);
};

/**
* Checks if response cache-control states max-age, allowing it to be cached.
*
Expand Down Expand Up @@ -145,6 +155,7 @@ Validators.requestValidators = [
Validators.responseValidators = [
Validators.response.onlyPrivate,
Validators.response.noStore,
Validators.response.maxAgeZero,
Validators.response.maxAgeFuture,
Validators.response.lastModified,
Validators.response.eTag,
Expand Down
18 changes: 18 additions & 0 deletions test/support/shared-examples.js
Expand Up @@ -231,6 +231,24 @@ exports.behavesLikeACacheStorage = function(storage) {
});
});

/* RFC 2616 Section 14.9 */
context('with cache-control: max-age=0', function(){
headers.is(function(){ return {'cache-control':'max-age=0'} });

it('evaluates as not storable', function(){
assert(!evaluator().storable);
});
});

/* RFC 2616 Section 14.9 */
context('with cache-control: max-age=-60', function(){
headers.is(function(){ return {'cache-control':'max-age=-60'} });

it('evaluates as not storable', function(){
assert(!evaluator().storable);
});
});

/* RFC 2616 Section 14.9 */
context('with cache-control: no-cache="x-something", max-age=60', function(){
headers.is(function(){ return {'cache-control':'no-cache="x-something", max-age=60'} });
Expand Down

0 comments on commit 35fe6c1

Please sign in to comment.