Skip to content

Commit

Permalink
Fix tailing slash with query. Closes #1763
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Jul 14, 2014
1 parent 683258c commit 119047e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,19 @@ Hoek.inherits(internals.Request, Events.EventEmitter);

internals.Request.prototype._setUrl = function (url, stripTrailingSlash) {

if (stripTrailingSlash &&
url.length > 1 &&
url[url.length - 1] === '/') {

url = url.slice(0, -1);
}

this.url = Url.parse(url, false);
this.url.query = Qs.parse(this.url.query); // Override parsed value
this.query = this.url.query;
this.path = this.url.pathname || ''; // pathname excludes query

if (stripTrailingSlash &&
this.path.length > 1 &&
this.path[this.path.length - 1] === '/') {

this.path = this.path.slice(0, -1);
this.url.pathname = this.path;
}

if (this.path &&
this.path.indexOf('%') !== -1) {

Expand Down
11 changes: 11 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,17 @@ describe('Request', function () {
done();
});
});

it('strips trailing slash with query', function (done) {

var server = new Hapi.Server({ router: { stripTrailingSlash: true } });
server.route({ method: 'GET', path: '/test', handler: function (request, reply) { reply(); } });
server.inject('/test/?a=b', function (res) {

expect(res.statusCode).to.equal(200);
done();
});
});
});

describe('#log', { parallel: false }, function () {
Expand Down

0 comments on commit 119047e

Please sign in to comment.