Permalink
Browse files

Merge pull request #13 from Unroll-Me/directory-redirect

Force directory URLs to end with / (fixes #12)
Drops v0.6 from Travis config to avoid failing build.
  • Loading branch information...
2 parents ae07275 + ee63262 commit 3d55aa5aabcf63aeceb6c38d7fcda4fddb94fdc8 sp committed Nov 20, 2012
Showing with 18 additions and 19 deletions.
  1. +0 −1 .travis.yml
  2. +14 −5 lib/stages/FileServer.js
  3. +4 −13 testing/tests/stages/FileServerTest.js
View
@@ -1,4 +1,3 @@
language: node_js
node_js:
- - 0.6
- 0.8
View
@@ -107,13 +107,21 @@ var FileServer = Stage.extend({
if (stats.isDirectory()) {
// if we have a default file name, use it
- if (t.defaultFile) {
- filePath = pathUtils.join(filePath, t.defaultFile);
- }
- else {
- // fall through
+ if (!t.defaultFile) {
return t.pass(request);
}
+
+ filePath = pathUtils.join(filePath, t.defaultFile);
+
+ // Make sure that directory URLs have a trailing slash
+ if (!/\/$/.test(remainder)) {
+ return qfs.isFile(filePath).then(function(isFile){
+ if (isFile)
+ return new RedirectResponse(request.getBaseUrl() + reqPath + '/');
+ else
+ return t.pass(request);
+ });
+ }
}
return FileResponse.create(filePath).then(null,
@@ -141,4 +149,5 @@ var FileServer = Stage.extend({
exports.FileServer = FileServer;
var Response = require('../../').Response;
+var RedirectResponse = require('../../').Redirect;
var FileResponse = require('../../').FileResponse;
@@ -234,23 +234,14 @@ exports["default file"] = {
"test default file option without trailing slash": function(test) {
var fileServer = new FileServer("/resources", testbench.fixturesDir + '/fileserver', 'chickens.html');
- var request = new Request('GET', '/resources');
+ var request = new Request('GET', '/resources', { Host: 'localhost:42' });
var bodyBuffer = new Pipe(true);
var contentLength;
Q.when(fileServer.service(request),
function(response) {
- test.equal(response.statusCode, 200);
- contentLength = response.getHeader('content-length');
- test.equal(response.getHeader("Content-Type"), "text/html");
- test.equal(response.getHeader('Expires'), new Date(now + 365 * 86400 * 1000).toUTCString());
- response.sendBody(bodyBuffer);
- return bodyBuffer.getData();
- }
- ).then(
- function(data) {
- test.equal(data.length, contentLength);
- test.ok(data.toString().indexOf('<p>chickens</p>') > 0);
+ test.equal(response.statusCode, 301);
+ test.equal(response.getHeader('Location'), 'http://localhost:42/resources/');
test.done();
}
).done();
@@ -284,7 +275,7 @@ exports["default file"] = {
"test default file in subdirectory": function(test) {
var fileServer = new FileServer("/", testbench.fixturesDir + '/fileserver', 'main.css');
- var request = new Request('GET', '/styles');
+ var request = new Request('GET', '/styles/');
var bodyBuffer = new Pipe(true);
var contentLength;

0 comments on commit 3d55aa5

Please sign in to comment.