Skip to content

Commit

Permalink
fixes issue where redirect could be missing a slash
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnerd committed Mar 6, 2019
1 parent e89283c commit 9fe7ab0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports = (server, pluginOptions) => {
// support both '{ /path: /redirect}' and { redirects : { /path: /redirect} }
additionalRoutes = additionalRoutes.redirects || additionalRoutes;
Object.keys(additionalRoutes).forEach(path => {
if (!path.startsWith('/')) {
additionalRoutes[`/${path}`] = additionalRoutes[path];
path = `/${path}`;
}
server.route({
method: '*',
path,
Expand Down
18 changes: 18 additions & 0 deletions test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ lab.experiment('hapi-redirect', () => {
Code.expect(result.headers.location).to.equal('/it/works?query=1');
});

lab.test(' test -> /it/works', async() => {
await server.register({
plugin: redirectModule,
options: {
redirects: {
test: '/it/works',
},
}
});
await server.start();
const result = await server.inject({
method: 'get',
url: '/test'
});
Code.expect(result.statusCode).to.equal(301);
Code.expect(result.headers.location).to.equal('/it/works');
});

lab.test(' /from/{param}/?query=1 -> /to/{param}?query=1', async() => {
await server.register({
plugin: redirectModule,
Expand Down

0 comments on commit 9fe7ab0

Please sign in to comment.