Skip to content

Commit

Permalink
ensure parseurl always working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Nov 5, 2015
1 parent 7fe29d9 commit c243baa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ module.exports = {

set path(path) {
var url = parse(this.req);
if (url.pathname === path) return;

url.pathname = path;
url.path = null;

Expand Down Expand Up @@ -180,6 +182,8 @@ module.exports = {

set querystring(str) {
var url = parse(this.req);
if (url.search === '?' + str) return;

url.search = str;
url.path = null;

Expand Down
8 changes: 8 additions & 0 deletions test/request/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var context = require('../context');
var parseurl = require('parseurl');

describe('ctx.path', function(){
it('should return the pathname', function(){
Expand All @@ -28,4 +29,11 @@ describe('ctx.path=', function(){
ctx.originalUrl.should.equal('/login');
ctx.request.originalUrl.should.equal('/login');
})

it('should not affect parseurl', function(){
const ctx = context({ url: '/login?foo=bar' });
ctx.path = '/login';
const url = parseurl(ctx.req);
url.path.should.equal('/login?foo=bar');
})
})
8 changes: 8 additions & 0 deletions test/request/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var context = require('../context');
var parseurl = require('parseurl');

describe('ctx.querystring', function(){
it('should return the querystring', function(){
Expand Down Expand Up @@ -44,4 +45,11 @@ describe('ctx.querystring=', function(){
ctx.originalUrl.should.equal('/store/shoes');
ctx.request.originalUrl.should.equal('/store/shoes');
})

it('should not affect parseurl', function(){
const ctx = context({ url: '/login?foo=bar' });
ctx.querystring = 'foo=bar';
const url = parseurl(ctx.req);
url.path.should.equal('/login?foo=bar');
})
})

0 comments on commit c243baa

Please sign in to comment.