Skip to content

Commit

Permalink
fix: empty querystring must be cached (eggjs#3535)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Mar 12, 2019
1 parent aaf008c commit 981bad5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 2 additions & 6 deletions app/extend/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ module.exports = {
// How to read query safely
// https://github.com/koajs/qs/issues/5
_customQuery(cacheName, filter) {
const str = this.querystring;
if (!str) {
return {};
}

const str = this.querystring || '';
let c = this[cacheName];
if (!c) {
c = this[cacheName] = {};
Expand All @@ -153,7 +149,7 @@ module.exports = {
cacheQuery = c[str] = {};
const isQueries = cacheName === _queriesCache;
// `querystring.parse` CANNOT parse something like `a[foo]=1&a[bar]=2`
const query = querystring.parse(str);
const query = str ? querystring.parse(str) : {};
for (const key in query) {
if (!key) {
// key is '', like `a=b&`
Expand Down
22 changes: 22 additions & 0 deletions test/app/extend/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ describe('test/app/extend/request.test.js', () => {
});
});

describe('this.query && this.queries can be modified', () => {
it('should success with querystring present', () => {
req.querystring = 'a=a&b=b1&b=b2';
assert.deepEqual(req.query, { a: 'a', b: 'b1' });
assert.deepEqual(req.queries, { a: [ 'a' ], b: [ 'b1', 'b2' ] });
req.query.a = 'aa';
req.queries.b = [ 'bb' ];
assert.deepEqual(req.query, { a: 'aa', b: 'b1' });
assert.deepEqual(req.queries, { a: [ 'a' ], b: [ 'bb' ] });
});

it('should success with empty querystring', () => {
req.querystring = '';
assert.deepEqual(req.query, {});
assert.deepEqual(req.queries, {});
req.query.a = 'aa';
req.queries.b = [ 'bb' ];
assert.deepEqual(req.query, { a: 'aa' });
assert.deepEqual(req.queries, { b: [ 'bb' ] });
});
});

describe('this.query[key] => String', () => {
function expectQuery(querystring, query) {
mm(req, 'querystring', querystring);
Expand Down

0 comments on commit 981bad5

Please sign in to comment.