Skip to content

Commit

Permalink
redirectToSlash defaults to true. Closes #1313
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Jan 9, 2014
1 parent 30fa044 commit 0214bff
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ The following options are available when adding a route:
Defaults to `false`.
- `showHidden` - optional boolean, determines if hidden files will be shown and served. Defaults to `false`.
- `redirectToSlash` - optional boolean, determines if requests for a directory without a trailing slash are redirected to the same path with
the missing slash. Useful for ensuring relative links inside the response are resolved correctly. Defaults to `false`.
the missing slash. Useful for ensuring relative links inside the response are resolved correctly. Defaults to `true`.
- `lookupCompressed` - optional boolean, instructs the file processor to look for the same filename with the '.gz' suffix for a precompressed
version of the file to serve if the request supports content encoding. Defaults to `false`.

Expand Down
2 changes: 1 addition & 1 deletion examples/directory/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internals.main = function () {

server.route([
{ method: 'GET', path: '/img/{path}', handler: { directory: { path: internals.images } } },
{ method: 'GET', path: '/files/{path*}', handler: { directory: { path: '../../', listing: true, redirectToSlash: true } } },
{ method: 'GET', path: '/files/{path*}', handler: { directory: { path: '../../', listing: true } } },
{ method: 'GET', path: '/{path?}', handler: { directory: { path: './' } } }
]);

Expand Down
4 changes: 2 additions & 2 deletions lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ exports.handler = function (route, options) {

// Directory

var index = (settings.index === false ? false : true); // Defaults to true
var index = (settings.index !== false); // Defaults to true
if (!index &&
!settings.listing) {

return reply(Boom.forbidden());
}

if (settings.redirectToSlash &&
if (settings.redirectToSlash !== false && // Defaults to true
!hasTrailingSlash) {

return reply().redirect(resource + '/');
Expand Down
25 changes: 19 additions & 6 deletions test/integration/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Directory', function () {
var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/showindex/{path*}', handler: { directory: { path: './', index: true, listing: true } } });

server.inject('/showindex/directory/subdir', function (res) {
server.inject('/showindex/directory/subdir/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('href="/showindex/directory/subdir/subsubdir"');
Expand All @@ -140,7 +140,7 @@ describe('Directory', function () {
var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/showindex/{path*}', handler: { directory: { path: './', index: true, listing: true } } });

server.inject('/showindex/directory/subdir', function (res) {
server.inject('/showindex/directory/subdir/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('href="/showindex/directory/subdir/sub%20subdir%3D"');
Expand All @@ -153,7 +153,7 @@ describe('Directory', function () {
var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/showindex/{path*}', handler: { directory: { path: './', index: true, listing: true } } });

server.inject('/showindex/directory/subdir/sub%20subdir%3D/subsubsubdir', function (res) {
server.inject('/showindex/directory/subdir/sub%20subdir%3D/subsubsubdir/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('href="/showindex/directory/subdir/sub%20subdir%3D/subsubsubdir/test.txt"');
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('Directory', function () {
var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/directorylist/{path*}', handler: { directory: { path: '../../', listing: true } } });

server.inject('/directorylist/test', function (res) {
server.inject('/directorylist/test/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('integration');
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('Directory', function () {
var server = new Hapi.Server({ files: { relativeTo: __dirname }, debug: false });
server.route({ method: 'GET', path: '/directoryIndex/{path*}', handler: { directory: { path: './directory/' } } });

server.inject('/directoryIndex/invalid', function (res) {
server.inject('/directoryIndex/invalid/', function (res) {

expect(res.statusCode).to.equal(500);
done();
Expand Down Expand Up @@ -333,7 +333,7 @@ describe('Directory', function () {
it('redirects to the same path with / appended if asking for a directory', function (done) {

var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/redirect/{path*}', handler: { directory: { path: './', index: true, listing: true, redirectToSlash: true } } });
server.route({ method: 'GET', path: '/redirect/{path*}', handler: { directory: { path: './', index: true, listing: true } } });

server.inject('http://example.com/redirect/directory/subdir', function (res) {

Expand All @@ -343,6 +343,19 @@ describe('Directory', function () {
});
});

it('does not redirect to the same path with / appended redirectToSlash disabled', function (done) {

var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/redirect/{path*}', handler: { directory: { path: './', index: true, listing: true, redirectToSlash: false } } });

server.inject('http://example.com/redirect/directory/subdir', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.contain('<html>');
done();
});
});

it('ignores unused path params', function (done) {

var server = new Hapi.Server({ files: { relativeTo: __dirname } });
Expand Down
11 changes: 0 additions & 11 deletions test/unit/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,6 @@ describe('Route', function () {
param: ''
}
},
'/path/{param*}': {
'/path': {
param: ''
},
'/path/a/b/to': {
param: 'a/b/to'
},
'/path/': {
param: ''
}
},
'/path/{p1}/{p2?}': {
'/path/a/c/d': false,
'/Path/a/c/d': false,
Expand Down

0 comments on commit 0214bff

Please sign in to comment.