Skip to content
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

fix: query part of url not preserved across redirects #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/middlewares/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var pathFn = require('path');
var mime = require('mime');
var URL = require('url');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowercase please


module.exports = function(app) {
var config = this.config;
Expand All @@ -23,9 +24,12 @@ module.exports = function(app) {
if (!data) {
if (extname) return next();

var parsedUrl = URL.parse(req.url);
var search = parsedUrl.search || '';
var hash = parsedUrl.hash || '';
url = encodeURI(url);
res.statusCode = 302;
res.setHeader('Location', root + url + '/');
res.setHeader('Location', root + url + '/' + search + hash);
res.end('Redirecting');
return;
}
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ describe('server', function() {
});
});

it('preserve query part after appending trailing slash', function() {
return Promise.using(prepareServer(), function(app) {
return request(app).get('/foo?x=y')
.expect('Location', '/foo/?x=y')
.expect(302, 'Redirecting')
.end();
});
});

it('don\'t append trailing slash if URL has a extension name', function() {
return Promise.using(prepareServer(), function(app) {
return request(app).get('/bar.txt')
Expand Down