ensure parseurl always working as expected

This commit is contained in:
dead_horse 2015-11-04 13:22:16 +08:00
parent ad6f752cff
commit 08057e386a
3 changed files with 20 additions and 0 deletions

View file

@ -127,6 +127,8 @@ module.exports = {
set path(path) { set path(path) {
const url = parse(this.req); const url = parse(this.req);
if (url.pathname === path) return;
url.pathname = path; url.pathname = path;
url.path = null; url.path = null;
@ -178,6 +180,8 @@ module.exports = {
set querystring(str) { set querystring(str) {
const url = parse(this.req); const url = parse(this.req);
if (url.search === `?${str}`) return;
url.search = str; url.search = str;
url.path = null; url.path = null;

View file

@ -2,6 +2,7 @@
'use strict'; 'use strict';
const context = require('../helpers/context'); const context = require('../helpers/context');
const parseurl = require('parseurl');
describe('ctx.path', () => { describe('ctx.path', () => {
it('should return the pathname', () => { it('should return the pathname', () => {
@ -28,4 +29,11 @@ describe('ctx.path=', () => {
ctx.originalUrl.should.equal('/login'); ctx.originalUrl.should.equal('/login');
ctx.request.originalUrl.should.equal('/login'); ctx.request.originalUrl.should.equal('/login');
}); });
it('should not affect parseurl', () => {
const ctx = context({ url: '/login?foo=bar' });
ctx.path = '/login';
const url = parseurl(ctx.req);
url.path.should.equal('/login?foo=bar');
});
}); });

View file

@ -2,6 +2,7 @@
'use strict'; 'use strict';
const context = require('../helpers/context'); const context = require('../helpers/context');
const parseurl = require('parseurl');
describe('ctx.querystring', () => { describe('ctx.querystring', () => {
it('should return the querystring', () => { it('should return the querystring', () => {
@ -44,4 +45,11 @@ describe('ctx.querystring=', () => {
ctx.originalUrl.should.equal('/store/shoes'); ctx.originalUrl.should.equal('/store/shoes');
ctx.request.originalUrl.should.equal('/store/shoes'); ctx.request.originalUrl.should.equal('/store/shoes');
}); });
it('should not affect parseurl', () => {
const ctx = context({ url: '/login?foo=bar' });
ctx.querystring = 'foo=bar';
const url = parseurl(ctx.req);
url.path.should.equal('/login?foo=bar');
});
}); });