From 266a644ca6ee217858c892ad5c8661f19df08a0e Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 6 Nov 2014 00:01:01 +0000 Subject: [PATCH] Preserve querystring when ensuring path ends with slash --- red/api/ui.js | 7 +++++-- test/red/api/ui_spec.js | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/red/api/ui.js b/red/api/ui.js index 1654a2838..fa35eac4f 100644 --- a/red/api/ui.js +++ b/red/api/ui.js @@ -31,8 +31,11 @@ events.on("node-icon-dir",function(dir) { module.exports = { ensureSlash: function(req,res,next) { - if (req.originalUrl.slice(-1) != "/") { - res.redirect(301,req.originalUrl+"/"); + var parts = req.originalUrl.split("?"); + if (parts[0].slice(-1) != "/") { + parts[0] += "/"; + var redirect = parts.join("?"); + res.redirect(301,redirect); } else { next(); } diff --git a/test/red/api/ui_spec.js b/test/red/api/ui_spec.js index 139a1aa22..920e9bf9e 100644 --- a/test/red/api/ui_spec.js +++ b/test/red/api/ui_spec.js @@ -39,6 +39,19 @@ describe("ui api", function() { .get('/foo') .expect(301,done); }); + it('redirects if the path, with query string, does not end in a slash',function(done) { + request(app) + .get('/foo?abc=def') + .expect(301) + .end(function(err,res) { + if (err) { + return done(err); + } + res.header['location'].should.equal("/foo/?abc=def"); + done(); + }); + }); + it('does not redirect if the path ends in a slash',function(done) { request(app) .get('/foo/')