Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 82ec1f49af7df273fb021fd63d3abbec55845462 0 parents
TJ Holowaychuk tj authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
1  .npmignore
@@ -0,0 +1 @@
+test
7 Makefile
@@ -0,0 +1,7 @@
+
+test:
+ @./node_modules/.bin/mocha \
+ --reporter spec \
+ --require should
+
+.PHONY: test
0  Readme.md
No changes.
49 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 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 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');
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.