couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [04/15] fauxton commit: updated refs/heads/import-master to 8cb432c
Date Sat, 07 Jun 2014 21:04:28 GMT
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 <willholley@gmail.com>
Authored: Tue May 27 06:50:36 2014 +0200
Committer: Garren Smith <garren.smith@gmail.com>
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);
+      });
+    });
+  });
 });
 


Mime
View raw message