ctx.search and ctx.request.search

This commit is contained in:
Jonathan Ong 2013-11-18 19:29:29 -08:00
parent 7d9c6ba66c
commit 28ca80758b
5 changed files with 91 additions and 1 deletions

View file

@ -243,6 +243,22 @@ module.exports = {
this.request.querystring = val; this.request.querystring = val;
}, },
/**
* Delegate to Request#search.
*/
get search() {
return this.request.search;
},
/**
* Delegate to Request#search=.
*/
set search(val) {
this.request.search = val;
},
/** /**
* Delegate to Request#host. * Delegate to Request#host.
*/ */

View file

@ -154,6 +154,31 @@ module.exports = {
this.url = stringify(url); this.url = stringify(url);
}, },
/**
* Get the search string. Same as the querystring
* except it includes the leading ?.
*
* @return {String}
* @api public
*/
get search() {
if (!this.querystring) return '';
return '?' + this.querystring;
},
/**
* Set the search string. Same as
* response.querystring= but included for ubiquity.
*
* @param {String} str
* @api public
*/
set search(str) {
this.querystring = str;
},
/** /**
* Parse the "Host" header field hostname * Parse the "Host" header field hostname
* and support X-Forwarded-Host when a * and support X-Forwarded-Host when a

View file

@ -16,11 +16,12 @@ describe('ctx.query', function(){
}) })
describe('ctx.query=', function(){ describe('ctx.query=', function(){
it('should stringify and replace the querystring', function(){ it('should stringify and replace the querystring and search', function(){
var ctx = context({ url: '/store/shoes' }); var ctx = context({ url: '/store/shoes' });
ctx.query = { page: 2, color: 'blue' }; ctx.query = { page: 2, color: 'blue' };
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.querystring.should.equal('page=2&color=blue'); ctx.querystring.should.equal('page=2&color=blue');
ctx.search.should.equal('?page=2&color=blue')
}) })
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', function(){

View file

@ -9,6 +9,17 @@ describe('ctx.querystring=', function(){
ctx.querystring.should.equal('page=2&color=blue'); ctx.querystring.should.equal('page=2&color=blue');
}) })
it('should update ctx.search and ctx.query', function(){
var ctx = context({ url: '/store/shoes' });
ctx.querystring = 'page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.search.should.equal('?page=2&color=blue');
ctx.query.should.eql({
page: '2',
color: 'blue'
});
})
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', function(){
var ctx = context({ url: '/store/shoes' }); var ctx = context({ url: '/store/shoes' });
ctx.querystring = 'page=2&color=blue'; ctx.querystring = 'page=2&color=blue';

37
test/request/search.js Normal file
View file

@ -0,0 +1,37 @@
var context = require('../context');
describe('ctx.search=', function(){
it('should replace the search', function(){
var ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.search.should.equal('?page=2&color=blue');
})
it('should update ctx.querystring and ctx.query', function(){
var ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.querystring.should.equal('page=2&color=blue');
ctx.query.should.eql({
page: '2',
color: 'blue'
});
})
it('should change .url but not .originalUrl', function(){
var ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.originalUrl.should.equal('/store/shoes');
ctx.request.originalUrl.should.equal('/store/shoes');
})
describe('when missing', function(){
it('should return ""', function(){
var ctx = context({ url: '/store/shoes' });
ctx.search.should.equal('');
})
})
})