Skip to content

Commit

Permalink
fix: return 404 for ENOTDIR (#162)
Browse files Browse the repository at this point in the history
* fix: return 404 for ENOTDIR

* test: add test for ENOTDIR

* test: fix expected path

* test: windows coverage

* fix: follow Hapi style guide
  • Loading branch information
kalinkrustev committed Jan 16, 2022
1 parent ae77ea5 commit 67daaff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const internals = {
methods: {
promised: ['open', 'close', 'fstat', 'readdir'],
raw: ['createReadStream']
}
},
notFound: new Set(['ENOENT', 'ENOTDIR'])
};


exports.File = class {

constructor(path) {
Expand All @@ -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);
}

Expand Down
10 changes: 10 additions & 0 deletions test/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 67daaff

Please sign in to comment.