From c243baa4d1e795a1c2a7767711185469f4e0ccb9 Mon Sep 17 00:00:00 2001 From: dead_horse Date: Thu, 5 Nov 2015 10:55:28 +0800 Subject: [PATCH] ensure parseurl always working as expected --- lib/request.js | 4 ++++ test/request/path.js | 8 ++++++++ test/request/querystring.js | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/lib/request.js b/lib/request.js index 93a090f..0505c57 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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; @@ -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; diff --git a/test/request/path.js b/test/request/path.js index f42dca0..337a0ce 100644 --- a/test/request/path.js +++ b/test/request/path.js @@ -2,6 +2,7 @@ 'use strict'; var context = require('../context'); +var parseurl = require('parseurl'); describe('ctx.path', function(){ it('should return the pathname', function(){ @@ -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'); + }) }) diff --git a/test/request/querystring.js b/test/request/querystring.js index e689d84..9885c56 100644 --- a/test/request/querystring.js +++ b/test/request/querystring.js @@ -2,6 +2,7 @@ 'use strict'; var context = require('../context'); +var parseurl = require('parseurl'); describe('ctx.querystring', function(){ it('should return the querystring', function(){ @@ -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'); + }) })