diff --git a/src/__tests__/helpers.ts b/src/__tests__/helpers.ts index 7576f83..afee63b 100644 --- a/src/__tests__/helpers.ts +++ b/src/__tests__/helpers.ts @@ -5,7 +5,11 @@ import fetch from 'node-fetch'; export function runServer(opts: Object, url : string, callback: Function) { - return createServer(opts as Options).listen(1234, function () { - return fetch('http://0.0.0.0:1234' + url).then(callback); + const app = createServer(opts as Options); + const server = app.listen(1234, function () { + return fetch('http://0.0.0.0:1234' + url).then(function (response : any) { + server.close(); + callback(response); + }); }); } diff --git a/src/__tests__/server.test.ts b/src/__tests__/server.test.ts index 517b85f..c92bccf 100644 --- a/src/__tests__/server.test.ts +++ b/src/__tests__/server.test.ts @@ -1,5 +1,6 @@ import { expect } from 'chai'; import { runServer } from './helpers'; +import * as fs from 'fs'; describe('Server', function () { @@ -21,4 +22,41 @@ describe('Server', function () { done(); }); }); + + it('Should respond with 404 on bad path', function (done) { + runServer({ + allowed_ips: [], + basicAuth: [], + dirList: false, + serveDir: 'site/', + opbeat: false, + open: false + }, '/foo/bar', function (response : any) { + expect(response.ok).to.be.false; + done(); + }); + }); + + describe('index route', function () { + const body = fs.readFileSync(__dirname + '/../../site/index.html').toString(); + + ['', '/', '/index.html'].forEach(function (path : string) { + it('should render ' + path, function (done) { + runServer({ + allowed_ips: [], + basicAuth: [], + dirList: false, + serveDir: 'site/', + opbeat: false, + open: false + }, path, function (response : any) { + expect(response.status).to.equal(200); + response.text().then(function (text : any) { + expect(text).to.equal(body); + done(); + }); + }); + }); + }); + }); }); diff --git a/src/middleware/static-files.ts b/src/middleware/static-files.ts index 4430ff5..ba98171 100644 --- a/src/middleware/static-files.ts +++ b/src/middleware/static-files.ts @@ -1,6 +1,6 @@ import * as express from 'express'; import * as serveIndex from 'serve-index'; -import path from 'path'; +import * as path from 'path'; function isDirectory(url : string) : boolean { return /\/$/.test(url);