Skip to content

Commit 0bd2431

Browse files
authored
Merge pull request #9 from firstandthird/onPreResponse
onPreResponse
2 parents d095a5d + f5b5d44 commit 0bd2431

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
22
node_js:
3+
- "8"
34
- "6"
4-
- "5"
5-
- "4"

index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,35 @@ module.exports = (server, options, allDone) => {
2424
};
2525

2626
if (options.method === 'append') {
27-
server.ext('onRequest', (request, reply) => {
27+
server.ext('onPreResponse', (request, reply) => {
28+
const statusCode = request.response.output ? request.response.output.statusCode : request.response.statusCode;
29+
// if the route was already found by hapi then just ignore it:
30+
if (statusCode !== 404) {
31+
return reply.continue();
32+
}
2833
const method = request.method.toLowerCase();
34+
// before failing, first check if there's a slashed route we can redirect to:
2935
if (['get', 'head'].indexOf(method) !== -1 && request.path[request.path.length - 1] !== '/') {
3036
const slashedPath = `${request.path}/`;
3137
return doRedirect(slashedPath, request, reply);
3238
}
39+
// otherwise it really is a 404:
3340
return reply.continue();
3441
});
3542
} else if (options.method === 'remove') {
36-
server.ext('onRequest', (request, reply) => {
43+
server.ext('onPreResponse', (request, reply) => {
44+
const statusCode = request.response.output ? request.response.output.statusCode : request.response.statusCode;
45+
// if the route was already found by hapi then just ignore it:
46+
if (statusCode !== 404) {
47+
return reply.continue();
48+
}
49+
// before failing, check if there's an unslashed route we can redirect to:
3750
const method = request.method.toLowerCase();
3851
if (['get', 'head'].indexOf(method) !== -1 && request.path !== '/' && request.path[request.path.length - 1] === '/') {
3952
const slashlessPath = request.path.replace(/\/$/, '');
4053
return doRedirect(slashlessPath, request, reply);
4154
}
55+
// otherwise it really is a 404:
4256
return reply.continue();
4357
});
4458
}

test/append.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,25 @@ lab.experiment('hapi-trailing-slash', () => {
7070
done();
7171
});
7272
});
73-
lab.test(' "append" /has/slash redirects to /has/slash/', (done) => {
73+
lab.test(' "append" /has/slash works normally if that route is specified', (done) => {
74+
server.route({
75+
path: '/has/slash',
76+
method: 'get',
77+
handler(request, reply) {
78+
return reply('slither');
79+
}
80+
});
81+
server.inject({
82+
method: 'get',
83+
url: '/has/slash'
84+
}, (result) => {
85+
Code.expect(result.statusCode).to.equal(200);
86+
Code.expect(result.payload).to.equal('slither');
87+
done();
88+
});
89+
});
90+
91+
lab.test(' "append" GET /has/slash redirects to /has/slash/', (done) => {
7492
server.inject({
7593
method: 'get',
7694
url: '/has/slash'

test/remove.test.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ lab.experiment('hapi-trailing-slash', () => {
2424
method: 'GET',
2525
path: '/no/slash',
2626
handler: (request, reply) => {
27-
reply('welcome to the jungle');
27+
reply('chinese democracy');
2828
}
2929
},
3030
{
@@ -80,23 +80,48 @@ lab.experiment('hapi-trailing-slash', () => {
8080
server.stop(done);
8181
});
8282

83-
lab.test(' "remove" /no/slash works normally', (done) => {
83+
lab.test(' "remove" /no/slash when called correctly returns 200', (done) => {
8484
server.inject({
8585
method: 'get',
8686
url: '/no/slash'
8787
}, (result) => {
8888
Code.expect(result.statusCode).to.equal(200);
89-
Code.expect(result.payload).to.equal('welcome to the jungle');
89+
Code.expect(result.payload).to.equal('chinese democracy');
9090
done();
9191
});
9292
});
93-
lab.test(' "remove" /no/slash/ redirects to /no/slash', (done) => {
93+
94+
lab.test(' "remove" /no/slash/ works normally if that route is specified', (done) => {
95+
server.route({
96+
path: '/no/slash/',
97+
method: 'get',
98+
handler(request, reply) {
99+
return reply('chinese democracy');
100+
}
101+
});
102+
server.inject({
103+
method: 'get',
104+
url: '/no/slash/'
105+
}, (result) => {
106+
Code.expect(result.statusCode).to.equal(200);
107+
Code.expect(result.payload).to.equal('chinese democracy');
108+
done();
109+
});
110+
});
111+
112+
lab.test(' "remove" /no/slash/ when called with trailing slash returns 301 Redirect to /no/slash', (done) => {
113+
let called = 0;
114+
server.ext('onRequest', (request, reply) => {
115+
called++;
116+
reply.continue();
117+
});
94118
server.inject({
95119
method: 'get',
96120
url: '/no/slash/'
97121
}, (result) => {
98122
Code.expect(result.statusCode).to.equal(301);
99123
Code.expect(result.headers.location).to.equal('/no/slash');
124+
Code.expect(called).to.equal(1);
100125
done();
101126
});
102127
});
@@ -110,10 +135,10 @@ lab.experiment('hapi-trailing-slash', () => {
110135
done();
111136
});
112137
});
113-
lab.test(' "remove" /no/slash GET works normally ', (done) => {
138+
lab.test(' "remove" /no/slash GET works normally with route params', (done) => {
114139
server.inject({
115140
method: 'get',
116-
url: '/no/slash/velvet_revolver'
141+
url: '/no/slash/velvet_revolver?p1=hi'
117142
}, (result) => {
118143
Code.expect(result.statusCode).to.equal(200);
119144
Code.expect(result.payload).to.equal('slither');

0 commit comments

Comments
 (0)