[Pkg-javascript-commits] [node-mocks-http] 65/296: parse query string from url to query object

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:21 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-mocks-http.

commit fcf9ccde707ee4163a3508ef37cd43c468839a43
Author: Alex Brown <alex at alexjamesbrown.com>
Date:   Wed Jan 7 01:07:06 2015 +0000

    parse query string from url to query object
---
 lib/mockRequest.js       |  5 +++++
 test/test-mockRequest.js | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index 820848b..4c88eda 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -48,6 +48,11 @@ function createRequest(options) {
     mockRequest.query = (options.query) ? options.query : {};
     mockRequest.files = (options.files) ? options.files : {};
 
+    //parse query string from url to object
+    if (Object.keys(mockRequest.query).length == 0) {
+        mockRequest.query = require('querystring').parse(mockRequest.url.split('?')[1])
+    }
+
     /**
      * Function: header
      *
diff --git a/test/test-mockRequest.js b/test/test-mockRequest.js
index 9ba728c..e437cd1 100644
--- a/test/test-mockRequest.js
+++ b/test/test-mockRequest.js
@@ -167,3 +167,32 @@ exports['.param - returns value in correct order (query)'] = function(test) {
 
     test.done();
 };
+
+exports['query object is parsed from url query string'] = function(test) {
+    var request = httpMocks.createRequest({
+        url: 'http://www.whatever.com?a=1&b=2&c=3'
+    });
+
+    test.equal(request.query['a'], '1');
+    test.equal(request.query['b'], '2');
+    test.equal(request.query['c'], '3');
+
+    test.done();
+};
+
+exports['query object is parsed from supplied options if provided'] = function(test) {
+    var request = httpMocks.createRequest({
+        url: 'http://www.whatever.com?a=1&b=2&c=3',
+        query: {
+            'a': '7',
+            'b': '8',
+            'c': '9'
+        }
+    });
+
+    test.equal(request.query['a'], '7');
+    test.equal(request.query['b'], '8');
+    test.equal(request.query['c'], '9');
+
+    test.done();
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-mocks-http.git



More information about the Pkg-javascript-commits mailing list