From 08057e386a48f9bde50e5568f7b97ad691b162d8 Mon Sep 17 00:00:00 2001 From: dead_horse Date: Wed, 4 Nov 2015 13:22:16 +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 b0ed9ca..f7ed45c 100644 --- a/lib/request.js +++ b/lib/request.js @@ -127,6 +127,8 @@ module.exports = { set path(path) { const url = parse(this.req); + if (url.pathname === path) return; + url.pathname = path; url.path = null; @@ -178,6 +180,8 @@ module.exports = { set querystring(str) { const 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 994a45c..9ce711f 100644 --- a/test/request/path.js +++ b/test/request/path.js @@ -2,6 +2,7 @@ 'use strict'; const context = require('../helpers/context'); +const parseurl = require('parseurl'); describe('ctx.path', () => { it('should return the pathname', () => { @@ -28,4 +29,11 @@ describe('ctx.path=', () => { ctx.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'); + }); }); diff --git a/test/request/querystring.js b/test/request/querystring.js index f37bedb..fcda988 100644 --- a/test/request/querystring.js +++ b/test/request/querystring.js @@ -2,6 +2,7 @@ 'use strict'; const context = require('../helpers/context'); +const parseurl = require('parseurl'); describe('ctx.querystring', () => { it('should return the querystring', () => { @@ -44,4 +45,11 @@ describe('ctx.querystring=', () => { ctx.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'); + }); });