Skip to content

checkIfExists #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
'use strict';
const useragent = require('useragent');
const wreck = require('wreck');

module.exports = (server, options, allDone) => {
options = options || {};
if (!options.method) {
return allDone(new Error('hapi-trailing-slash plugin registered without specifiying which method to use'));
}
options.statusCode = options.statusCode || 301;

const redirectExists = (redirectPath, callback) => {
if (!options.checkIfExists) {
return callback(true);
}
wreck.request('head', redirectPath, {}, (err, res) => {
if (err) {
return callback(false);
}
return callback(res.statusCode < 400);
});
};

const doRedirect = (path, request, reply) => {
const redirectTo = request.url.search ? path + request.url.search : path;
if (options.verbose) {
Expand Down Expand Up @@ -39,10 +53,15 @@ module.exports = (server, options, allDone) => {
if (request.path.indexOf('.') !== -1) {
return reply.continue();
}

// pick a redirection based on either 'append' or 'remove' mode:
const redirectPath = options.method === 'append' ? `${request.path}/` : request.path.replace(/\/$/, '');
return doRedirect(redirectPath, request, reply);
return redirectExists(redirectPath, (exists) => {
if (exists) {
doRedirect(redirectPath, request, reply);
} else {
return reply.continue();
}
});
}
// otherwise it really is a 404:
return reply.continue();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lab": "^13.0.1"
},
"dependencies": {
"useragent": "^2.2.1"
"useragent": "^2.2.1",
"wreck": "^12.0.0"
}
}
66 changes: 66 additions & 0 deletions test/checkIfExists.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';
const Code = require('code'); // assertion library
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const Hapi = require('hapi');
const theModule = require('../index.js');

lab.experiment('hapi-trailing-slash checkIfExists', () => {
let server;

lab.beforeEach((done) => {
server = new Hapi.Server();
server.connection();

server.route([
{
method: 'GET',
path: '/no/slash',
handler: (request, reply) => {
reply('chinese democracy');
}
},
{
method: 'GET',
path: '/has/slash/',
handler: (request, reply) => {
reply('slither').code(301);
}
},
]);

server.register({
register: theModule,
options: {
checkIfExists: true,
method: 'remove',
verbose: true
}
}, (err) => {
if (err) {
throw err;
}
server.start(done);
});
});

lab.afterEach((done) => {
server.stop(done);
});

lab.test(' checkIfExists will do a HEAD check that the forward exists', (done) => {
server.inject({
method: 'get',
url: '/no/slash/'
}, (result) => {
Code.expect(result.statusCode).to.equal(404);
server.inject({
method: 'get',
url: '/has/slash/'
}, (result2) => {
Code.expect(result2.statusCode).to.equal(301);
done();
});
});
});
});