Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E799211EFA for ; Sat, 7 Jun 2014 21:04:25 +0000 (UTC) Received: (qmail 55976 invoked by uid 500); 7 Jun 2014 21:04:25 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 55802 invoked by uid 500); 7 Jun 2014 21:04:25 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 55710 invoked by uid 99); 7 Jun 2014 21:04:25 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 07 Jun 2014 21:04:25 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 406C9462BA; Sat, 7 Jun 2014 21:04:25 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kxepal@apache.org To: commits@couchdb.apache.org Date: Sat, 07 Jun 2014 21:04:28 -0000 Message-Id: <5b243866f30a42528958e9cb5ce96960@git.apache.org> In-Reply-To: <18ae056366f1449bbf3a8bd1b9e9262b@git.apache.org> References: <18ae056366f1449bbf3a8bd1b9e9262b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/15] fauxton commit: updated refs/heads/import-master to 8cb432c Fauxton: Fix QueryParams Ensure we clone the parameters to Documents.QueryParams.parse and Documents.QueryParams.stringify. These functions should return new data structures rather than modifying the input in-place. Do not use the parsed query object for url generation. Instead, use the stringified representation. Fixes COUCHDB-2249. Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/f216c420 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/f216c420 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/f216c420 Branch: refs/heads/import-master Commit: f216c4205bb53b08ce96bc58e110b18527056662 Parents: 8c62d4d Author: Will Holley Authored: Tue May 27 06:50:36 2014 +0200 Committer: Garren Smith Committed: Wed May 28 11:33:28 2014 +0200 ---------------------------------------------------------------------- app/addons/documents/resources.js | 9 ++- app/addons/documents/routes.js | 6 +- app/addons/documents/tests/resourcesSpec.js | 97 +++++++++++++++++++++++- 3 files changed, 106 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/f216c420/app/addons/documents/resources.js ---------------------------------------------------------------------- diff --git a/app/addons/documents/resources.js b/app/addons/documents/resources.js index 99e79a2..ad61e78 100644 --- a/app/addons/documents/resources.js +++ b/app/addons/documents/resources.js @@ -21,13 +21,16 @@ function(app, FauxtonAPI, PagingCollection) { Documents.QueryParams = (function () { var _eachParams = function (params, action) { + // clone to avoid in-place modification + var result = _.clone(params); + _.each(['startkey', 'endkey', 'key'], function (key) { - if (_.has(params, key)) { - params[key] = action(params[key]); + if (_.has(result, key)) { + result[key] = action(result[key]); } }); - return params; + return result; }; return { http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/f216c420/app/addons/documents/routes.js ---------------------------------------------------------------------- diff --git a/app/addons/documents/routes.js b/app/addons/documents/routes.js index 5a2a04f..b8b01fb 100644 --- a/app/addons/documents/routes.js +++ b/app/addons/documents/routes.js @@ -194,10 +194,12 @@ function(app, FauxtonAPI, Documents, Databases) { }, createParams: function (options) { - var urlParams = Documents.QueryParams.parse(app.getParams(options)); + var urlParams = app.getParams(options); + var params = Documents.QueryParams.parse(urlParams); + return { urlParams: urlParams, - docParams: _.extend(_.clone(urlParams), {limit: this.getDocPerPageLimit(urlParams, 20)}) + docParams: _.extend(params, {limit: this.getDocPerPageLimit(params, 20)}) }; }, http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/f216c420/app/addons/documents/tests/resourcesSpec.js ---------------------------------------------------------------------- diff --git a/app/addons/documents/tests/resourcesSpec.js b/app/addons/documents/tests/resourcesSpec.js index 62506e6..e120582 100644 --- a/app/addons/documents/tests/resourcesSpec.js +++ b/app/addons/documents/tests/resourcesSpec.js @@ -10,7 +10,7 @@ // License for the specific language governing permissions and limitations under // the License. define([ - 'addons/documents/resources', + 'addons/documents/resources', 'testUtils' ], function (Models, testUtils) { var assert = testUtils.assert; @@ -53,5 +53,100 @@ define([ }); + describe('QueryParams', function() { + describe('parse', function() { + it('should not parse arbitrary parameters', function() { + var params = {"foo":"[1]]"}; + var result = Models.QueryParams.parse(params); + + assert.deepEqual(result, params); + }); + + it('parses startkey, endkey', function() { + var params = { + "startkey":"[\"a\",\"b\"]", + "endkey":"[\"c\",\"d\"]" + }; + var result = Models.QueryParams.parse(params); + + assert.deepEqual(result, { + "startkey":["a","b"], + "endkey":["c","d"] + }); + }); + + it('parses key', function() { + var params = { + "key":"[1,2]" + }; + var result = Models.QueryParams.parse(params); + + assert.deepEqual(result, {"key":[1,2]}); + }); + + it('does not modify input', function() { + var params = { + "key":"[\"a\",\"b\"]" + }; + var clone = _.clone(params); + var result = Models.QueryParams.parse(params); + + assert.deepEqual(params, clone); + }); + }); + + describe('stringify', function() { + it('should not stringify arbitrary parameters', function() { + var params = {"foo":[1,2,3]}; + var result = Models.QueryParams.stringify(params); + + assert.deepEqual(result, params); + }); + + it('stringifies startkey, endkey', function() { + var params = { + "startkey":["a","b"], + "endkey":["c","d"] + }; + + var result = Models.QueryParams.stringify(params); + + assert.deepEqual(result, { + "startkey":"[\"a\",\"b\"]", + "endkey":"[\"c\",\"d\"]" + }); + }); + + it('stringifies key', function() { + var params = {"key":["a","b"]}; + var result = Models.QueryParams.stringify(params); + + assert.deepEqual(result, { "key":"[\"a\",\"b\"]" }); + }); + + it('does not modify input', function() { + var params = {"key":["a","b"]}; + var clone = _.clone(params); + var result = Models.QueryParams.stringify(params); + + assert.deepEqual(params, clone); + }); + + it('is symmetrical with parse', function() { + var params = { + "startkey":["a","b"], + "endkey":["c","d"], + "foo": "[1,2]", + "bar": "abc" + }; + + var clone = _.clone(params); + var json = Models.QueryParams.stringify(params); + var result = Models.QueryParams.parse(json); + + assert.deepEqual(result, clone); + }); + }); + }); });