diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/package.json b/package.json index 5c4df36..591ff7a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "request handler", "middleware" ], + "scripts": { + "test": "node test/staticFileTests.js" + }, "maintainers": [ { "name": "Xavi", @@ -24,6 +27,9 @@ "engines": { "node": ">= 0.3.1" }, + "devDependencies" : { + "tape" : ">=1.0.1" + }, "dependencies": { "lru-cache": "~2.3.0" } diff --git a/test/staticFileTests.js b/test/staticFileTests.js new file mode 100644 index 0000000..8d7f3c4 --- /dev/null +++ b/test/staticFileTests.js @@ -0,0 +1,119 @@ +var test = require('tape'), + bee = require("../"); + + +test('StaticFile defaults max-age to 31536000', function (t) { + t.plan(1); + + var staticFile = bee.staticFile('./index.js', 'text/javascript'); + + staticFile({ headers: {}, url: "/load-existing-static-file" }, { + setHeader: function(type, value) { + if(type === 'Cache-Control') + { + t.equal(value, 'private, max-age=31536000'); + } + }, + writeHead: function() {}, + removeHeader: function() {}, + end: function() {} + }); + +}); + +test('StaticFile set max-age when provided', function (t) { + t.plan(1); + + var maxAge = 123456, + staticFile = bee.staticFile('./index.js', 'text/javascript', maxAge); + + staticFile({ headers: {}, url: "/load-existing-static-file" }, { + setHeader: function(type, value) { + if(type === 'Cache-Control') + { + t.equal(value, 'private, max-age=' + maxAge); + } + }, + writeHead: function() {}, + removeHeader: function() {}, + end: function() {} + }); + +}); + +test('StaticFile set max-age when is 0', function (t) { + t.plan(1); + + var maxAge = 0, + staticFile = bee.staticFile('./index.js', 'text/javascript', maxAge); + + staticFile({ headers: {}, url: "/load-existing-static-file" }, { + setHeader: function(type, value) { + if(type === 'Cache-Control') + { + t.equal(value, 'private, max-age=' + maxAge); + } + }, + writeHead: function() {}, + removeHeader: function() {}, + end: function() {} + }); + +}); + +test('StaticDir defaults max-age to 31536000', function (t) { + t.plan(1); + + var staticDir = bee.staticDir("./", { ".json": "application/json", "js": "application/x-javascript" }); + staticDir({ headers: {}, url: "/load-existing-file-from-static-dir" }, { + setHeader: function(type, value) { + if(type === 'Cache-Control') + { + t.equal(value, 'private, max-age=31536000'); + } + }, + writeHead: function() {}, + removeHeader: function() {}, + end: function() {} + }, [ "package.json" ]); + +}); + +test('StaticDir set max-age when provided', function (t) { + t.plan(1); + + var maxAge = 123456; + debugger; + var staticDir = bee.staticDir("./", { ".json": "application/json", "js": "application/x-javascript" }, maxAge); + staticDir({ headers: {}, url: "/load-existing-file-from-static-dir" }, { + setHeader: function(type, value) { + if(type === 'Cache-Control') + { + t.equal(value, 'private, max-age=' + maxAge); + } + }, + writeHead: function() {}, + removeHeader: function() {}, + end: function() {} + }, [ "package.json" ]); + +}); + +test('StaticDir set max-age when is 0', function (t) { + t.plan(1); + + var maxAge = 0, + staticDir = bee.staticDir("./", { ".json": "application/json", "js": "application/x-javascript" }, maxAge); + staticDir({ headers: {}, url: "/load-existing-file-from-static-dir" }, { + setHeader: function(type, value) { + if(type === 'Cache-Control') + { + t.equal(value, 'private, max-age=' + maxAge); + } + }, + writeHead: function() {}, + removeHeader: function() {}, + end: function() {} + }, [ "package.json" ]); + +}); \ No newline at end of file