diff --git a/lib/fs.js b/lib/fs.js index d48a3f6..94f0435 100755 --- a/lib/fs.js +++ b/lib/fs.js @@ -12,10 +12,10 @@ const internals = { methods: { promised: ['open', 'close', 'fstat', 'readdir'], raw: ['createReadStream'] - } + }, + notFound: new Set(['ENOENT', 'ENOTDIR']) }; - exports.File = class { constructor(path) { @@ -34,7 +34,7 @@ exports.File = class { catch (err) { const data = { path: this.path }; - if (this.path.indexOf('\u0000') !== -1 || err.code === 'ENOENT') { + if (this.path.indexOf('\u0000') !== -1 || internals.notFound.has(err.code)) { throw Boom.notFound(null, data); } diff --git a/test/directory.js b/test/directory.js index fd6d3e1..1963fc1 100755 --- a/test/directory.js +++ b/test/directory.js @@ -73,6 +73,16 @@ describe('directory', () => { expect(res.request.response._error.data.path).to.equal(Path.join(__dirname, 'xyz')); }); + it('returns a 404 when requesting an unknown file when part of the path matches a filename', async () => { + + const server = await provisionServer(); + server.route({ method: 'GET', path: '/directory/{path*}', handler: { directory: { path: './' } } }); + + const res = await server.inject('/directory/directory.js/xyz'); + expect(res.statusCode).to.equal(404); + expect(res.request.response._error.data.path).to.equal(Path.join(__dirname, 'directory.js', 'xyz')); + }); + it('returns a file when requesting a file from the directory', async () => { const server = await provisionServer();