Skip to content

Instantly share code, notes, and snippets.

@dominykas
Created September 11, 2014 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominykas/cdb139c615b983937b68 to your computer and use it in GitHub Desktop.
Save dominykas/cdb139c615b983937b68 to your computer and use it in GitHub Desktop.
describe('route specificity', function () {
var server = new Hapi.Server();
var reflectRoutePath = function (request, reply) {
reply(request.route.path)
};
server.route({ method: 'GET', path: '/products1/search', config: { handler: reflectRoutePath } });
server.route({ method: 'GET', path: '/products1/search/{path*}', config: { handler: reflectRoutePath } });
server.route({
method: 'GET',
path: '/products1/{providerId}/{productId}',
config: {
// validate: {
// params: {
// providerId: Joi.number(),
// productId: Joi.any()
// }
// },
handler: reflectRoutePath
}
});
server.route({
method: 'GET',
path: '/products2/{providerId}/{productId}',
config: {
// validate: {
// params: {
// providerId: Joi.number(),
// productId: Joi.any()
// }
// },
handler: reflectRoutePath
}
});
server.route({ method: 'GET', path: '/products2/search', config: { handler: reflectRoutePath } });
server.route({ method: 'GET', path: '/products2/search/{path*1}', config: { handler: reflectRoutePath } });
var testCases = [
["/products1/search", "/products1/search"],
["/products1/search/one", "/products1/search/{path*}"], // fails, because {path*} is moved last
["/products1/5/one", "/products1/{providerId}/{productId}"],
["/products2/search", "/products2/search"],
["/products2/search/one", "/products2/search/{path*1}"],
["/products2/5/one", "/products2/{providerId}/{productId}"],
];
testCases.forEach(function (v) {
var requestUrl = v[0], expectedRoute = v[1];
it(requestUrl, function (done) {
server.inject(requestUrl, function (res) {
expect(res.result).to.equal(expectedRoute);
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment