From 4f04f7e119d932272b277f7992e3851c91f3c2b3 Mon Sep 17 00:00:00 2001 From: David Beale Date: Tue, 5 May 2015 10:47:13 +0100 Subject: [PATCH] Stock 404 response --- lib/controllers.js | 19 ++++++++++++------- test/test.js | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/controllers.js b/lib/controllers.js index a6ad22eb..89261747 100644 --- a/lib/controllers.js +++ b/lib/controllers.js @@ -35,21 +35,26 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument) if (indexDocument) { + return notFoundResponse(req, res); + } + else + { + var template = templateBuilder.buildKeyNotFound(keyName); + return buildXmlResponse(res, 404, template); + } + }; + + + var notFoundResponse = function(req, res) + { var ErrorDoc = '\n404 - Resource Not Found

404 - Resource Not Found

'; return buildResponse(req, res, 404, { - md4: '', modifiedDate: new Date(), contentType: 'text/html', customMetaData: [], size: ErrorDoc.length }, ErrorDoc); - } - else - { - var template = templateBuilder.buildKeyNotFound(keyName); - return buildXmlResponse(res, 404, template); - } }; /** diff --git a/test/test.js b/test/test.js index c830dd6c..2e85e96a 100644 --- a/test/test.js +++ b/test/test.js @@ -673,4 +673,25 @@ describe('S3rver Tests with Static Web Hosting', function () { }); }); + + it('should get a 404 error page', function (done) { + request('http://localhost:5694/site/page/not-exists', function (error, response, body) { + if (error) + { + return done(error); + } + + if (response.statusCode !== 404) { + return done(new Error('Invalid status: ' + response.statusCode)); + } + + if (response.headers['content-type'] !== 'text/html; charset=utf-8') + { + return done(new Error('Invalid ContentType: ' + response.headers['content-type'])); + } + + done(); + }); + }); + });