You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by dc...@apache.org on 2014/08/14 01:26:02 UTC

[14/50] couchdb commit: updated refs/heads/1.6.x to eeb31cb

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/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/9fb9b4b7
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/9fb9b4b7
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/9fb9b4b7

Branch: refs/heads/1.6.x
Commit: 9fb9b4b799daea53e02a4e8ff7dee03d0ec0608c
Parents: f8d535c
Author: Will Holley <wi...@gmail.com>
Authored: Tue May 27 06:50:36 2014 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Wed May 28 11:33:28 2014 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/documents/resources.js   |  9 +-
 src/fauxton/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/blob/9fb9b4b7/src/fauxton/app/addons/documents/resources.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/documents/resources.js b/src/fauxton/app/addons/documents/resources.js
index 99e79a2..ad61e78 100644
--- a/src/fauxton/app/addons/documents/resources.js
+++ b/src/fauxton/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/blob/9fb9b4b7/src/fauxton/app/addons/documents/routes.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/documents/routes.js b/src/fauxton/app/addons/documents/routes.js
index 5a2a04f..b8b01fb 100644
--- a/src/fauxton/app/addons/documents/routes.js
+++ b/src/fauxton/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/blob/9fb9b4b7/src/fauxton/app/addons/documents/tests/resourcesSpec.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/documents/tests/resourcesSpec.js b/src/fauxton/app/addons/documents/tests/resourcesSpec.js
index 62506e6..e120582 100644
--- a/src/fauxton/app/addons/documents/tests/resourcesSpec.js
+++ b/src/fauxton/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);
+      });
+    });
+  });
 });