Skip to content

Commit 9953c4d

Browse files
authored
Merge pull request #11 from firstandthird/checkIfExists
checkIfExists
2 parents 253f8fa + dea9a48 commit 9953c4d

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
'use strict';
22
const useragent = require('useragent');
3+
const wreck = require('wreck');
4+
35
module.exports = (server, options, allDone) => {
46
options = options || {};
57
if (!options.method) {
68
return allDone(new Error('hapi-trailing-slash plugin registered without specifiying which method to use'));
79
}
810
options.statusCode = options.statusCode || 301;
911

12+
const redirectExists = (redirectPath, callback) => {
13+
if (!options.checkIfExists) {
14+
return callback(true);
15+
}
16+
wreck.request('head', redirectPath, {}, (err, res) => {
17+
if (err) {
18+
return callback(false);
19+
}
20+
return callback(res.statusCode < 400);
21+
});
22+
};
23+
1024
const doRedirect = (path, request, reply) => {
1125
const redirectTo = request.url.search ? path + request.url.search : path;
1226
if (options.verbose) {
@@ -39,10 +53,15 @@ module.exports = (server, options, allDone) => {
3953
if (request.path.indexOf('.') !== -1) {
4054
return reply.continue();
4155
}
42-
4356
// pick a redirection based on either 'append' or 'remove' mode:
4457
const redirectPath = options.method === 'append' ? `${request.path}/` : request.path.replace(/\/$/, '');
45-
return doRedirect(redirectPath, request, reply);
58+
return redirectExists(redirectPath, (exists) => {
59+
if (exists) {
60+
doRedirect(redirectPath, request, reply);
61+
} else {
62+
return reply.continue();
63+
}
64+
});
4665
}
4766
// otherwise it really is a 404:
4867
return reply.continue();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"lab": "^13.0.1"
3030
},
3131
"dependencies": {
32-
"useragent": "^2.2.1"
32+
"useragent": "^2.2.1",
33+
"wreck": "^12.0.0"
3334
}
3435
}

test/checkIfExists.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
const Code = require('code'); // assertion library
3+
const Lab = require('lab');
4+
const lab = exports.lab = Lab.script();
5+
const Hapi = require('hapi');
6+
const theModule = require('../index.js');
7+
8+
lab.experiment('hapi-trailing-slash checkIfExists', () => {
9+
let server;
10+
11+
lab.beforeEach((done) => {
12+
server = new Hapi.Server();
13+
server.connection();
14+
15+
server.route([
16+
{
17+
method: 'GET',
18+
path: '/no/slash',
19+
handler: (request, reply) => {
20+
reply('chinese democracy');
21+
}
22+
},
23+
{
24+
method: 'GET',
25+
path: '/has/slash/',
26+
handler: (request, reply) => {
27+
reply('slither').code(301);
28+
}
29+
},
30+
]);
31+
32+
server.register({
33+
register: theModule,
34+
options: {
35+
checkIfExists: true,
36+
method: 'remove',
37+
verbose: true
38+
}
39+
}, (err) => {
40+
if (err) {
41+
throw err;
42+
}
43+
server.start(done);
44+
});
45+
});
46+
47+
lab.afterEach((done) => {
48+
server.stop(done);
49+
});
50+
51+
lab.test(' checkIfExists will do a HEAD check that the forward exists', (done) => {
52+
server.inject({
53+
method: 'get',
54+
url: '/no/slash/'
55+
}, (result) => {
56+
Code.expect(result.statusCode).to.equal(404);
57+
server.inject({
58+
method: 'get',
59+
url: '/has/slash/'
60+
}, (result2) => {
61+
Code.expect(result2.statusCode).to.equal(301);
62+
done();
63+
});
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)