Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 10, 2012
0 parents commit 82ec1f4
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
test
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@

test:
@./node_modules/.bin/mocha \
--reporter spec \
--require should

.PHONY: test
Empty file added Readme.md
Empty file.
49 changes: 49 additions & 0 deletions index.js
@@ -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);
}
12 changes: 12 additions & 0 deletions package.json
@@ -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": "*"
}
}
56 changes: 56 additions & 0 deletions test/fresh.js
@@ -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');
})
})

0 comments on commit 82ec1f4

Please sign in to comment.