pax_global_header00006660000000000000000000000064122015321000014474gustar00rootroot0000000000000052 comment=4357419340d69a7ca27fd2f1228c1daead4c05c9 node-fresh-0.2.0/000077500000000000000000000000001220153210000135255ustar00rootroot00000000000000node-fresh-0.2.0/.gitignore000066400000000000000000000000151220153210000155110ustar00rootroot00000000000000node_modules node-fresh-0.2.0/.npmignore000066400000000000000000000000051220153210000155170ustar00rootroot00000000000000test node-fresh-0.2.0/Makefile000066400000000000000000000001311220153210000151600ustar00rootroot00000000000000 test: @./node_modules/.bin/mocha \ --reporter spec \ --require should .PHONY: testnode-fresh-0.2.0/Readme.md000066400000000000000000000031271220153210000152470ustar00rootroot00000000000000 # node-fresh HTTP response freshness testing ## fresh(req, res) 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. ## Example: ```js var req = { 'if-none-match': 'tobi' }; var res = { 'etag': 'luna' }; fresh(req, res); // => false var req = { 'if-none-match': 'tobi' }; var res = { 'etag': 'tobi' }; fresh(req, res); // => true ``` ## Installation ``` $ npm install fresh ``` ## License (The MIT License) Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.node-fresh-0.2.0/index.js000066400000000000000000000023111220153210000151670ustar00rootroot00000000000000 /** * 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']; var cc = req['cache-control']; // unconditional request if (!modifiedSince && !noneMatch) return false; // check for no-cache cache request directive if (cc && cc.indexOf('no-cache') !== -1) return false; // parse if-none-match if (noneMatch) noneMatch = noneMatch.split(/ *, */); // if-none-match if (noneMatch) etagMatches = ~noneMatch.indexOf(etag) || '*' == noneMatch[0]; // if-modified-since if (modifiedSince) { modifiedSince = new Date(modifiedSince); lastModified = new Date(lastModified); notModified = lastModified <= modifiedSince; } return !! (etagMatches && notModified); }node-fresh-0.2.0/package.json000066400000000000000000000007171220153210000160200ustar00rootroot00000000000000{ "name": "fresh", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "description": "HTTP response freshness testing", "version": "0.2.0", "main": "index.js", "repository": { "type": "git", "url": "https://github.com/visionmedia/node-fresh.git"}, "dependencies": {}, "repository": { "type": "git", "url": "https://github.com/visionmedia/node-fresh.git" }, "devDependencies": { "mocha": "*", "should": "*" } } node-fresh-0.2.0/test/000077500000000000000000000000001220153210000145045ustar00rootroot00000000000000node-fresh-0.2.0/test/fresh.js000066400000000000000000000104761220153210000161610ustar00rootroot00000000000000 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; }) }) describe('when etag is missing', function(){ it('should be stale', function(){ var req = { 'if-none-match': 'tobi' }; var res = {}; fresh(req, res).should.be.false; }) }) describe('when * is given', function(){ it('should be fresh', function(){ var req = { 'if-none-match': '*' }; var res = { 'etag': 'hey' }; fresh(req, res).should.be.true; }) }) }) 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; }) }) describe('when last-modified is missing', function(){ it('should be stale', function(){ var req = { 'if-none-match': new Date().toUTCString() }; var res = {}; fresh(req, res).should.be.false; }) }) describe('with invalid if-modified-since date', function(){ it('should be stale', function(){ var req = { 'if-none-match': 'foo' }; var res = {}; fresh(req, res).should.be.false; }) }) describe('with invalid modified-since date', function(){ it('should be stale', function(){ var req = { 'if-none-match': new Date().toUTCString() }; var res = { 'modified-since': 'foo' }; fresh(req, res).should.be.false; }) }) }) describe('when requested with If-Modified-Since and If-None-Match', function(){ describe('when both match', function(){ it('should be fresh', function(){ var now = new Date; var req = { 'if-none-match': 'tobi', 'if-modified-since': new Date(now - 2000).toUTCString() }; var res = { 'etag': 'tobi', 'last-modified': new Date(now - 4000).toUTCString() }; fresh(req, res).should.be.true; }) }) describe('when only one matches', function(){ it('should be stale', function(){ var now = new Date; var req = { 'if-none-match': 'tobi', 'if-modified-since': new Date(now - 4000).toUTCString() }; var res = { 'etag': 'tobi', 'last-modified': new Date(now - 2000).toUTCString() }; fresh(req, res).should.be.false; var now = new Date; var req = { 'if-none-match': 'tobi', 'if-modified-since': new Date(now - 2000).toUTCString() }; var res = { 'etag': 'luna', 'last-modified': new Date(now - 4000).toUTCString() }; fresh(req, res).should.be.false; }) }) describe('when none match', function(){ it('should be stale', function(){ var now = new Date; var req = { 'if-none-match': 'tobi', 'if-modified-since': new Date(now - 4000).toUTCString() }; var res = { 'etag': 'luna', 'last-modified': new Date(now - 2000).toUTCString() }; fresh(req, res).should.be.false; }) }) }) describe('when requested with Cache-Control: no-cache', function(){ it('should be stale', function(){ var req = { 'cache-control' : ' no-cache' }; var res = {}; fresh(req, res).should.be.false; }) }) })