-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 82ec1f4
Showing
7 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
|
|||
test: | |||
@./node_modules/.bin/mocha \ | |||
--reporter spec \ | |||
--require should | |||
|
|||
.PHONY: test |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
|
|||
/** | |||
* Expose `fresh()`. | |||
*/ | |||
|
|||
module.exports = fresh; | |||
|
|||
/** | |||
* Check freshness of `req` and `res` headers. | |||
* | |||
* When the cache is "fresh" __true__ is returned, | |||
* otherwise __false__ is returned to indicate that | |||
* the cache is now stale. | |||
* | |||
* @param {Object} req | |||
* @param {Object} res | |||
* @return {Boolean} | |||
* @api public | |||
*/ | |||
|
|||
function fresh(req, res) { | |||
// defaults | |||
var etagMatches = true; | |||
var notModified = true; | |||
|
|||
// fields | |||
var modifiedSince = req['if-modified-since']; | |||
var noneMatch = req['if-none-match']; | |||
var lastModified = res['last-modified']; | |||
var etag = res['etag']; | |||
|
|||
// unconditional request | |||
if (!modifiedSince && !noneMatch) return false; | |||
|
|||
// parse if-none-match | |||
if (noneMatch) noneMatch = noneMatch.split(/ *, */); | |||
|
|||
// if-none-match | |||
if (noneMatch) etagMatches = ~noneMatch.indexOf(etag); | |||
|
|||
// if-modified-since | |||
if (modifiedSince) { | |||
modifiedSince = new Date(modifiedSince); | |||
lastModified = new Date(lastModified); | |||
notModified = lastModified <= modifiedSince; | |||
} | |||
|
|||
return !! (etagMatches && notModified); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
{ | |||
"name": "fresh", | |||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", | |||
"description": "HTTP response freshness testing", | |||
"version": "0.0.1", | |||
"main": "index.js", | |||
"dependencies": {}, | |||
"devDependencies": { | |||
"mocha": "*", | |||
"should": "*" | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
|
|||
var fresh = require('..'); | |||
|
|||
describe('fresh(reqHeader, resHeader)', function(){ | |||
describe('when a non-conditional GET is performed', function(){ | |||
it('should be stale', function(){ | |||
var req = {}; | |||
var res = {}; | |||
fresh(req, res).should.be.false; | |||
}) | |||
}) | |||
|
|||
describe('when requested with If-None-Match', function(){ | |||
describe('when ETags match', function(){ | |||
it('should be fresh', function(){ | |||
var req = { 'if-none-match': 'tobi' }; | |||
var res = { 'etag': 'tobi' }; | |||
fresh(req, res).should.be.true; | |||
}) | |||
}) | |||
|
|||
describe('when ETags mismatch', function(){ | |||
it('should be stale', function(){ | |||
var req = { 'if-none-match': 'tobi' }; | |||
var res = { 'etag': 'luna' }; | |||
fresh(req, res).should.be.false; | |||
}) | |||
}) | |||
|
|||
xdescribe('when etag is missing'); | |||
}) | |||
|
|||
describe('when requested with If-Modified-Since', function(){ | |||
describe('when modified since the date', function(){ | |||
it('should be stale', function(){ | |||
var now = new Date; | |||
var req = { 'if-modified-since': new Date(now - 4000).toUTCString() }; | |||
var res = { 'last-modified': new Date(now - 2000).toUTCString() }; | |||
fresh(req, res).should.be.false; | |||
}) | |||
}) | |||
|
|||
describe('when unmodified since the date', function(){ | |||
it('should be fresh', function(){ | |||
var now = new Date; | |||
var req = { 'if-modified-since': new Date(now - 2000).toUTCString() }; | |||
var res = { 'last-modified': new Date(now - 4000).toUTCString() }; | |||
fresh(req, res).should.be.true; | |||
}) | |||
}) | |||
|
|||
xdescribe('with invalid if-modified-since date'); | |||
xdescribe('with invalid modified-since date'); | |||
xdescribe('when last-modified is missing'); | |||
}) | |||
}) |