[Pkg-javascript-commits] [node-mocks-http] 71/296: Make res.set() work like express when passed an object

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:22 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 64da0b7c8fe17443a53cd904def3ccc322bbd316
Author: Gregor MacLennan <gmaclennan at digital-democracy.org>
Date:   Wed Feb 25 18:59:49 2015 -0800

    Make res.set() work like express when passed an object
---
 lib/mockResponse.js | 40 +++++++++++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 8b668ad..4a3211e 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -265,20 +265,34 @@ function createResponse(options) {
 
     };
 
-    /**
-     * Function: header
-     *
-     *   An alias of either getHeader or setHeader depending on
-     *   the amount of passed parameters.
-     */
-    mockResponse.header = function(name, value) {
-
-        if (typeof value !== 'undefined') {
-            return mockResponse.setHeader(name, value);
+   /**
+    * Set header `field` to `val`, or pass
+    * an object of header fields.
+    *
+    * Examples:
+    *
+    *    res.set('Foo', ['bar', 'baz']);
+    *    res.set('Accept', 'application/json');
+    *    res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
+    *
+    * Aliased as `mockResponse.header()`.
+    *
+    * @param {String|Object|Array} field
+    * @param {String} val
+    * @return {ServerResponse} for chaining
+    * @api public
+    */
+    mockResponse.set = mockResponse.header = function header(field, val) {
+        if (arguments.length === 2) {
+            if (Array.isArray(val)) val = val.map(String);
+            else val = String(val);
+            mockResponse.setHeader(field, val);
         } else {
-            return mockResponse.getHeader(name);
+            for (var key in field) {
+                mockResponse.setHeader(key, field[key]);
+            }
         }
-
+        return mockResponse;
     };
 
     /**
@@ -297,7 +311,7 @@ function createResponse(options) {
      *
      *   Set a particular header by name.
      */
-    mockResponse.set = mockResponse.setHeader = function(name, value) {
+    mockResponse.setHeader = function(name, value) {
         _headers[name] = value;
         return value;
     };

-- 
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