Skip to content

Commit

Permalink
Directory handler path function result. Closes #1574
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 24, 2014
1 parent 3c04cd4 commit 0eb9a62
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Current Documentation
[v5.0.x](https://github.com/spumko/hapi/blob/master/docs/Reference.md)
[v5.1.x](https://github.com/spumko/hapi/blob/master/docs/Reference.md)

## Previous Documentation
[v5.0.x](https://github.com/spumko/hapi/blob/v5.0.0/docs/Reference.md)
[v4.1.x](https://github.com/spumko/hapi/blob/v4.1.0/docs/Reference.md)
[v4.0.x](https://github.com/spumko/hapi/blob/v4.0.0/docs/Reference.md)
[v3.1.x](https://github.com/spumko/hapi/blob/v3.1.0/docs/Reference.md)
Expand Down
5 changes: 3 additions & 2 deletions docs/Reference.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 5.0.x API Reference
# 5.1.x API Reference

- [`Hapi.Server`](#hapiserver)
- [`new Server([host], [port], [options])`](#new-serverhost-port-options)
Expand Down Expand Up @@ -401,7 +401,8 @@ The following options are available when adding a route:
Value can be:
- a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string.
- an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string).
- a function with the signature `function(request)` which returns the path string.
- a function with the signature `function(request)` which returns the path string or an array of path strings. If the function returns an
error, the error is passed back to the client in the response.
- `index` - optional boolean, determines if 'index.html' will be served if found in the folder when requesting a directory. Defaults to `true`.
- `listing` - optional boolean, determines if directory listing is generated when a directory is requested without an index document.
Defaults to `false`.
Expand Down
15 changes: 14 additions & 1 deletion lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,20 @@ exports.handler = function (route, options) {

var paths = normalized;
if (typeof settings.path === 'function') {
paths = [normalize(settings.path.call(null, request))];
var result = settings.path.call(null, request);
if (result instanceof Error) {
return reply(result);
}

if (Array.isArray(result)) {
paths = result.map(normalize);
}
else if (typeof result === 'string') {
paths = [normalize(result)];
}
else {
return reply(Boom.badImplementation('Invalid path function'));
}
}

// Append parameter
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hapi",
"description": "HTTP Server framework",
"homepage": "http://hapijs.com",
"version": "5.0.1",
"version": "5.1.0",
"repository": {
"type": "git",
"url": "git://github.com/spumko/hapi"
Expand Down
49 changes: 49 additions & 0 deletions test/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var Fs = require('fs');
var Path = require('path');
var Lab = require('lab');
var Boom = require('boom');
var Hapi = require('..');


Expand Down Expand Up @@ -84,6 +85,19 @@ describe('Directory', function () {
});
});

it('returns a file when requesting a file from multi directory function response', function (done) {

var server = new Hapi.Server({ files: { relativeTo: __dirname } });
server.route({ method: 'GET', path: '/multiple/{path*}', handler: { directory: { path: function () { return ['./', '../'] }, listing: true } } });

server.inject('/multiple/package.json', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('name": "hapi"');
done();
});
});

it('returns the correct file when requesting a file from a child directory', function (done) {

var server = new Hapi.Server({ files: { relativeTo: __dirname } });
Expand Down Expand Up @@ -556,4 +570,39 @@ describe('Directory', function () {
done();
});
});

it('returns error when path function returns error', function (done) {

var path = function () {

return Boom.badRequest('Really?!');
};

var server = new Hapi.Server();
server.route({ method: 'GET', path: '/test/{path*}', handler: { directory: { path: path } } });

server.inject('/test/index.html', function (res) {

expect(res.statusCode).to.equal(400);
expect(res.result.message).to.equal('Really?!');
done();
});
});

it('returns error when path function returns invalid response', function (done) {

var path = function () {

return 5;
};

var server = new Hapi.Server({ debug: false });
server.route({ method: 'GET', path: '/test/{path*}', handler: { directory: { path: path } } });

server.inject('/test/index.html', function (res) {

expect(res.statusCode).to.equal(500);
done();
});
});
});

0 comments on commit 0eb9a62

Please sign in to comment.