diff --git a/modules/__tests__/browseFile-test.js b/modules/__tests__/browseFile-test.js index 46c68711..debcf2ef 100644 --- a/modules/__tests__/browseFile-test.js +++ b/modules/__tests__/browseFile-test.js @@ -8,6 +8,18 @@ describe('A request to browse a file', () => { server = createServer(); }); + describe('when the URL does not contains filename', () => { + it('redirects to browseDirectory', done => { + request(server) + .get('/browse/react@16.8.0') + .end((err, res) => { + expect(res.statusCode).toBe(302); + expect(res.headers['location']).toBe('/browse/react@16.8.0/'); + done(); + }); + }); + }); + describe('when the file exists', () => { it('returns an HTML page', done => { request(server) diff --git a/modules/actions/serveFileBrowser.js b/modules/actions/serveFileBrowser.js index 640f26b3..1efaf872 100644 --- a/modules/actions/serveFileBrowser.js +++ b/modules/actions/serveFileBrowser.js @@ -54,6 +54,11 @@ async function findEntry(stream, filename) { async function serveFileBrowser(req, res) { const stream = await getPackage(req.packageName, req.packageVersion, req.log); + + if (!req.filename) { + return res.redirect(req.originalUrl + '/') + } + const entry = await findEntry(stream, req.filename); if (!entry) {