[Pkg-javascript-commits] [node-mocks-http] 56/296: added .param method on mockrequest
Thorsten Alteholz
alteholz at moszumanska.debian.org
Mon Feb 8 18:13:20 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 c385b99096e119bf5fb279ef34d95b9199695d41
Author: Alex Brown <alex at alexjamesbrown.com>
Date: Mon Dec 29 02:49:53 2014 +0000
added .param method on mockrequest
---
lib/mockRequest.js | 24 +++++++++++++-
test/test-mockRequest.js | 81 ++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 98 insertions(+), 7 deletions(-)
diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index 0a49816..820848b 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -67,6 +67,28 @@ function createRequest(options) {
};
/**
+ * Function: param
+ *
+ * Return the value of param name when present.
+ * Lookup is performed in the following order:
+ * - req.params
+ * - req.body
+ * - req.query
+ */
+ mockRequest.param = function(parameterName) {
+ if (mockRequest.params[parameterName])
+ return mockRequest.params[parameterName];
+
+ if (mockRequest.body[parameterName])
+ return mockRequest.body[parameterName];
+
+ if (mockRequest.query[parameterName])
+ return mockRequest.query[parameterName];
+
+ return null;
+ };
+
+ /**
* Function: _setParameter
*
* Set parameters that the client can then get using the 'params'
@@ -198,4 +220,4 @@ function createRequest(options) {
}
-module.exports.createRequest = createRequest;
\ No newline at end of file
+module.exports.createRequest = createRequest;
diff --git a/test/test-mockRequest.js b/test/test-mockRequest.js
index ac53840..9ba728c 100644
--- a/test/test-mockRequest.js
+++ b/test/test-mockRequest.js
@@ -49,7 +49,7 @@ exports['url - Setting a POST'] = function(test) {
exports['addBody - Simple verification'] = function(test) {
var request = httpMocks.createRequest();
-
+
var username = 'bob';
request._addBody('user', username);
test.equal(username, request.body.user);
@@ -58,9 +58,11 @@ exports['addBody - Simple verification'] = function(test) {
exports['setBody - Simple verification'] = function(test) {
var request = httpMocks.createRequest();
-
+
var username = 'bob';
- request._setBody( { 'user': username } );
+ request._setBody({
+ 'user': username
+ });
test.equal(username, request.body.user);
test.done();
};
@@ -73,12 +75,12 @@ exports['body - Unset value'] = function(test) {
exports['Object creation - All values set'] = function(test) {
-
+
var methodValue = "PUT";
var idValue = 34;
var urlValue = 'http://localhost:6522/blingbling';
var usernameValue = "mittens";
-
+
var request = httpMocks.createRequest({
method: methodValue,
url: urlValue,
@@ -91,10 +93,77 @@ exports['Object creation - All values set'] = function(test) {
email: 'bob at dog.com'
}
});
-
+
test.equal(methodValue, request.method);
test.equal(urlValue, request.url);
test.equal(idValue, request.params.id);
test.equal(usernameValue, request.body.username);
test.done();
};
+
+exports['.param - returns value from params if exists'] = function(test) {
+ var request = httpMocks.createRequest();
+ request._setParameter('paramitem', 'abc');
+
+ test.equal(request.param('paramitem'), 'abc');
+ test.done();
+};
+
+exports['.param - returns value in body if exists'] = function(test) {
+ var request = httpMocks.createRequest();
+ request._addBody('bodyitem', 'abc');
+
+ test.equal(request.param('bodyitem'), 'abc');
+ test.done();
+};
+
+exports['.param - returns value in querystring if exists'] = function(test) {
+ var request = httpMocks.createRequest({
+ query: {
+ 'queryitem': 'abc'
+ }
+ });
+
+ test.equal(request.param('queryitem'), 'abc');
+ test.done();
+};
+
+
+exports['.param - returns value in correct order (params)'] = function(test) {
+ var request = httpMocks.createRequest({
+ query: {
+ 'thing': '3'
+ }
+ });
+
+ request._setParameter('thing', '1');
+ request._addBody('thing', '2');
+
+ test.equal(request.param('thing'), '1');
+ test.done();
+};
+
+exports['.param - returns value in correct order (body)'] = function(test) {
+ var request = httpMocks.createRequest({
+ query: {
+ 'thing': '2'
+ }
+ });
+
+ request._addBody('thing', '1');
+
+ test.equal(request.param('thing'), '1');
+ test.done();
+};
+
+exports['.param - returns value in correct order (query)'] = function(test) {
+ var request = httpMocks.createRequest({
+ query: {
+ 'thing': '1'
+ }
+ });
+
+ test.equal(request.param('thing'), '1');
+
+ 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