diff --git a/lib/context.js b/lib/context.js index dcf2857..595fd34 100644 --- a/lib/context.js +++ b/lib/context.js @@ -243,6 +243,22 @@ module.exports = { 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. */ diff --git a/lib/request.js b/lib/request.js index c2f2550..3389dd8 100644 --- a/lib/request.js +++ b/lib/request.js @@ -154,6 +154,31 @@ module.exports = { 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 * and support X-Forwarded-Host when a diff --git a/test/request/query.js b/test/request/query.js index 31a76b3..b6669be 100644 --- a/test/request/query.js +++ b/test/request/query.js @@ -16,11 +16,12 @@ 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' }); ctx.query = { page: 2, color: 'blue' }; ctx.url.should.equal('/store/shoes?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(){ diff --git a/test/request/querystring.js b/test/request/querystring.js index 53a4a20..ce45577 100644 --- a/test/request/querystring.js +++ b/test/request/querystring.js @@ -9,6 +9,17 @@ describe('ctx.querystring=', function(){ 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(){ var ctx = context({ url: '/store/shoes' }); ctx.querystring = 'page=2&color=blue'; diff --git a/test/request/search.js b/test/request/search.js new file mode 100644 index 0000000..6f5b1b2 --- /dev/null +++ b/test/request/search.js @@ -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(''); + }) + }) +}) \ No newline at end of file