Skip to content

Commit

Permalink
Merge pull request #56 from TeeTeeHaa/master
Browse files Browse the repository at this point in the history
missing trailing slash in URL when using default file "index.html"
  • Loading branch information
phstc committed May 28, 2013
2 parents e294621 + 6af107c commit 544f7ad
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/node-static.js
Expand Up @@ -56,7 +56,14 @@ Server.prototype.serveDir = function (pathname, req, res, finish) {

fs.stat(htmlIndex, function (e, stat) {
if (!e) {
that.respond(null, 200, {}, [htmlIndex], stat, req, res, finish);
var status = 200;
var headers = {};
var originalPathname = decodeURI(url.parse(req.url).pathname);
if (originalPathname.length && originalPathname.charAt(originalPathname.length - 1) !== '/') {
return finish(301, { 'Location': originalPathname + '/' });
} else {
that.respond(null, status, headers, [htmlIndex], stat, req, res, finish);
}
} else {
if (pathname in indexStore) {
streamFiles(indexStore[pathname].files);
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/there/index.html
@@ -0,0 +1,8 @@
<html lang="en">
<head>
<title>Other page</title>
</head>
<body>
hello there!
</body>
</html>
49 changes: 49 additions & 0 deletions test/integration/node-static-test.js
Expand Up @@ -197,4 +197,53 @@ suite.addBatch({
assert.equal(static.mime.lookup('woff'), 'application/font-woff');
}
}
})
.addBatch({
'serving subdirectory index': {
topic : function(){
request.get(TEST_SERVER + '/there/', this.callback); // with trailing slash
},
'should respond with 200' : function(error, response, body){
assert.equal(response.statusCode, 200);
},
'should respond with text/html': function(error, response, body){
assert.equal(response.headers['content-type'], 'text/html');
}
}
})
.addBatch({
'redirecting to subdirectory index': {
topic : function(){
request.get({ url: TEST_SERVER + '/there', followRedirect: false }, this.callback); // without trailing slash
},
'should respond with 301' : function(error, response, body){
assert.equal(response.statusCode, 301);
},
'should respond with location header': function(error, response, body){
assert.equal(response.headers['location'], '/there/'); // now with trailing slash
},
'should respond with empty string body' : function(error, response, body){
assert.equal(body, '');
}
}
})
.addBatch({
'requesting a subdirectory (with trailing slash) not found': {
topic : function(){
request.get(TEST_SERVER + '/notthere/', this.callback); // with trailing slash
},
'should respond with 404' : function(error, response, body){
assert.equal(response.statusCode, 404);
}
}
})
.addBatch({
'requesting a subdirectory (without trailing slash) not found': {
topic : function(){
request.get({ url: TEST_SERVER + '/notthere', followRedirect: false }, this.callback); // without trailing slash
},
'should respond with 404' : function(error, response, body){
assert.equal(response.statusCode, 404);
}
}
}).export(module);

0 comments on commit 544f7ad

Please sign in to comment.