diff --git a/.editorconfig b/.editorconfig index 53ed1a3..1f78208 100644 --- a/.editorconfig +++ b/.editorconfig @@ -17,8 +17,9 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[*.md] +[*.{md,htmlt}] trim_trailing_whitespace = false +insert_final_newline = false [*.json] indent_size = 2 diff --git a/test/fixtures/serve/beforeAll-hook/compose.js b/test/fixtures/serve/beforeAll-hook/compose.js new file mode 100644 index 0000000..409d675 --- /dev/null +++ b/test/fixtures/serve/beforeAll-hook/compose.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = function (req, res, next) { + req.param2 = 'overwritten'; + next(); +}; diff --git a/test/fixtures/serve/beforeAll-hook/index.htmlt b/test/fixtures/serve/beforeAll-hook/index.htmlt new file mode 100644 index 0000000..57d0471 --- /dev/null +++ b/test/fixtures/serve/beforeAll-hook/index.htmlt @@ -0,0 +1 @@ +<%= request.param1 %>, <%= request.param2 %> \ No newline at end of file diff --git a/test/spec/beforeAll-hook.js b/test/spec/beforeAll-hook.js new file mode 100644 index 0000000..9e17edb --- /dev/null +++ b/test/spec/beforeAll-hook.js @@ -0,0 +1,72 @@ +'use strict'; + +var express = require('express'); +var http = require('http'); +var serveSpa = require('../../lib/index.js'); +var path = require('path'); +var rp = require('request-promise'); + + +describe('Regarding the beforeAll hook, Serve-SPA', function () { + + var server; + + before(function (done) { + var app = express(); + serveSpa(app, path.join(__dirname, '../fixtures/serve/'), { + + beforeAll: function (req, res, next) { + + switch (req.path) { + case '/beforeAll-hook/own': + res.send('own'); + return; + case '/beforeAll-hook/redirect': + res.redirect('http://localhost:4000/sub1/'); + return; + } + + req.param1 = 'original param1'; + req.param2 = 'original param2'; + next(); + + } + + }); + server = http.createServer(app); + server.listen(4000, function () { done(); }); + }); + + after(function () { + server.close(); + }); + + + it('should call the beforeAll middleware first', function () { + + return rp('http://localhost:4000/beforeAll-hook') + .then(function (body) { + expect(body).to.equal('original param1, overwritten'); + }); + + }); + + it('should allow serving its own response', function () { + + return rp('http://localhost:4000/beforeAll-hook/own') + .then(function (body) { + expect(body).to.equal('own'); + }); + + }); + + it('should allow a redirect', function () { + + return rp('http://localhost:4000/beforeAll-hook/redirect') + .then(function (body) { + expect(body).to.equal('sub1'); + }); + + }); + +}); diff --git a/test/spec/init.js b/test/spec/init.js index 262c3e0..bef59a5 100644 --- a/test/spec/init.js +++ b/test/spec/init.js @@ -34,6 +34,10 @@ describe('Regarding its initialization, Serve-SPA', function () { initServeSpa(app, '/absolute.path', { require: false }); }).to.throw('The require option must be if type function.'); + expect(function () { + initServeSpa(app, '/absolute.path', { beforeAll: false }); + }).to.throw('The beforeAll option must be if type function.'); + expect(function () { initServeSpa(app, path.join(__dirname, '../fixtures/invalid/compose-export/')); }).to.throw('The following composing module does not export a function: ' + path.join(__dirname, '../fixtures/invalid/compose-export/compose.js')); @@ -65,6 +69,9 @@ describe('Regarding its initialization, Serve-SPA', function () { '/async-compose', '/async-compose/compose.js', '/async-compose/index.htmlt', + '/beforeAll-hook', + '/beforeAll-hook/compose.js', + '/beforeAll-hook/index.htmlt', '/compose-with-router', '/compose-with-router/compose.js', '/compose-with-router/index.htmlt',