Skip to content

Commit

Permalink
fix: #9492, keep query params on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Apr 15, 2021
1 parent 0cfccee commit 36f119a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/middleware/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ module.exports = function (middleware) {
if (!userslug) {
return next();
}
const path = req.path.replace(/^\/api/, '')
.replace('uid', 'user')
.replace(uid, () => userslug);
const path = req.url.replace(/^\/api/, '')
.replace(`/uid/${uid}`, () => `/user/${userslug}`);
controllers.helpers.redirect(res, path);
});

Expand Down
10 changes: 10 additions & 0 deletions test/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,16 @@ describe('Controllers', () => {
});
});

it('should redirect to userslug and keep query params', (done) => {
request(`${nconf.get('url')}/api/uid/${fooUid}/topics?foo=bar`, { json: true }, (err, res, body) => {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.equal(res.headers['x-redirect'], '/user/foo/topics?foo=bar');
assert.equal(body, '/user/foo/topics?foo=bar');
done();
});
});

it('should 404 if user does not exist', (done) => {
request(`${nconf.get('url')}/api/uid/123123`, { json: true }, (err, res) => {
assert.ifError(err);
Expand Down

0 comments on commit 36f119a

Please sign in to comment.