Skip to content

Commit

Permalink
Blocking generating an ETag for responses that can't be cached & addi…
Browse files Browse the repository at this point in the history
…ng tests, updating dependencies
  • Loading branch information
avoidwork committed Feb 6, 2017
1 parent 8af434c commit 09b0996
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
15 changes: 9 additions & 6 deletions lib/etag.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const lru = require("tiny-lru"),
explicitGet: /^GET$/i,
etag: /ETag:\s/i,
invalid: /^(a|cache|connection|content-(d|e|l|m|r)|d|ex|l|p|r|s|t|u|v|w|x)/,
valid: /^(200|304)$/
valid: /^(200|304)$/,
nonCachable: /no-cache|no-store|private|must-revalidate/
};

function clone (arg) {
Expand Down Expand Up @@ -60,11 +61,13 @@ class ETag {
}
});

obj.register(parsed.href, {
etag: lheaders.etag,
headers: lheaders,
timestamp: cached ? cached.timestamp : parseInt(new Date().getTime() / 1000, 10)
});
if (!lheaders["cache-control"] || !regex.nonCachable.test(lheaders["cache-control"])) {
obj.register(parsed.href, {
etag: lheaders.etag,
headers: lheaders,
timestamp: cached ? cached.timestamp : parseInt(new Date().getTime() / 1000, 10)
});
}
}
});

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-etag",
"version": "1.2.7",
"version": "1.2.8",
"description": "ETag middleware",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -31,15 +31,15 @@
"murmurhash3js": "~3.0.1",
"retsu": "~2.0.3",
"tiny-lru": "~1.4.2",
"tiny-parse": "~1.0.3"
"tiny-parse": "~2.0.0"
},
"devDependencies": {
"grunt": "~1.0.1",
"grunt-eslint": "~18.1.0",
"grunt-eslint": "~19.0.0",
"grunt-mocha-test": "~0.12.7",
"grunt-nsp": "~2.2.0",
"hippie": "~0.5.0",
"mocha": "~2.5.3",
"woodland": "~1.1.17"
"woodland": "~1.1.18"
}
}
41 changes: 38 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const http = require("http"),
mmh3 = require("murmurhash3js").x86.hash32,
etagValue = "\"" + mmh3("Hello World!", random) + "\"",
cacheSize = 1000,
router = require("woodland")({defaultHeaders: {"Cache-Control": "no-cache"}, cacheSize: cacheSize, defaultHost: "localhost", hosts: ["localhost", "noresponse"]}),
router = require("woodland")({defaultHeaders: {"Cache-Control": "public"}, cacheSize: cacheSize, defaultHost: "localhost", hosts: ["localhost", "noresponse"]}),
hippie = require("hippie"),
etag = require(path.join(__dirname, "..", "index.js"))({cacheSize: cacheSize, seed: random});

Expand All @@ -21,6 +21,11 @@ router.use("/", function hello (req, res) {
res.end("Hello World!");
});

router.use("/no-cache", function hello (req, res) {
res.writeHead(200, {"Content-Type": "text/plain", "Cache-Control":"no-cache"});
res.end("Hello World!");
});

http.createServer(router.route).listen(8001);

describe("Valid ETag", function () {
Expand All @@ -29,7 +34,7 @@ describe("Valid ETag", function () {
.get("/")
.expectStatus(200)
.expectHeader("Allow", "GET, HEAD, OPTIONS")
.expectHeader("Cache-Control", "no-cache")
.expectHeader("Cache-Control", "public")
.expectHeader("Content-Type", "text/plain")
.expectHeader("ETag", etagValue)
.expectBody(/^Hello World!$/)
Expand All @@ -44,7 +49,7 @@ describe("Valid ETag", function () {
.head("/")
.expectStatus(200)
.expectHeader("Allow", "GET, HEAD, OPTIONS")
.expectHeader("Cache-Control", "no-cache")
.expectHeader("Cache-Control", "public")
.expectHeader("Content-Type", "text/plain")
.expectHeader("ETag", etagValue)
.expectBody(/^$/)
Expand Down Expand Up @@ -83,4 +88,34 @@ describe("Valid ETag", function () {
done();
});
});

it("GET /no-cache (200 / 'Success' / No Etag)", function (done) {
request()
.get("/no-cache")
.expectStatus(200)
.expectHeader("Allow", "GET, HEAD, OPTIONS")
.expectHeader("Cache-Control", "no-cache")
.expectHeader("Content-Type", "text/plain")
.expectHeader("ETag", undefined)
.expectBody(/^Hello World!$/)
.end(function (err) {
if (err) throw err;
done();
});
});

it("HEAD /no-cache (200 / 'Success' / No Etag)", function (done) {
request()
.head("/no-cache")
.expectStatus(200)
.expectHeader("Allow", "GET, HEAD, OPTIONS")
.expectHeader("Cache-Control", "no-cache")
.expectHeader("Content-Type", "text/plain")
.expectHeader("ETag", undefined)
.expectBody(/^$/)
.end(function (err) {
if (err) throw err;
done();
});
});
});

0 comments on commit 09b0996

Please sign in to comment.