[Pkg-javascript-commits] [node-mocks-http] 15/296: Allow both 'send' parameter options.

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:16 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 660d8769189f9c7376e23f8782dc313d1e56b26b
Author: Howard Abrams <howard at howardabrams.com>
Date:   Thu Mar 1 00:22:23 2012 -0800

    Allow both 'send' parameter options.
    
    We now allow both v2 and v3 of Express' response.send() parameter order
    and uses some interrogation to determine which is correct.
---
 lib/mockResponse.js       | 47 ++++++++++++++++++++++++++++++++++++-----------
 test/test-mockResponse.js | 31 +++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 9d8f64e..327825c 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -38,7 +38,7 @@ exports.createResponse = function(options) {
         statusCode: -1,
 
         status: function(code) {
-            statusCode = code;
+            this.statusCode = code;
             return this;
         },
         
@@ -77,16 +77,38 @@ exports.createResponse = function(options) {
          * 
          * @param data The data to return. Must be a string.
          */
-        send: function( data ) {
-            if (2 == arguments.length) {
-                this.statusCode = data;
-                data = arguments[1];
+        send: function( a, b, c ) {
+            switch (arguments.length) {
+                case 1:
+                    _data += a;
+                    break;
+                    
+                case 2:
+                    if (typeof a == 'number') {
+                        this.statusCode = a;                        
+                        _data += b;
+                    }
+                    else if (typeof b == 'number') {
+                        _data += a;
+                        this.statusCode = b;
+                        console.warn("WARNING: Called 'send' with deprecated parameter order"); 
+                    }
+                    else {
+                        _data += a;
+                        _encoding = b;
+                    }
+                    break;
+                    
+                case 3:
+                    _data += a;
+                    _headers = b;
+                    this.statusCode = c;
+                    console.warn("WARNING: Called 'send' with deprecated three parameters"); 
+                    break;
+                    
+                default:
+                    break;
             }
-
-            if (typeof data != 'String') {
-                console.warn("Called 'send' with '%s': %s", typeof data, data); 
-            }
-            _data += data;
         },
         
         
@@ -103,7 +125,10 @@ exports.createResponse = function(options) {
          */
         
         write: function( data, encoding ) {
-            this.send(data, encoding);
+            _data += data;
+            if (encoding) {
+                _encoding = encoding;
+            }
         },
 
         /**
diff --git a/test/test-mockResponse.js b/test/test-mockResponse.js
index eb2fdf1..c0cb002 100644
--- a/test/test-mockResponse.js
+++ b/test/test-mockResponse.js
@@ -118,4 +118,35 @@ exports['writeHead - Can not call after end'] = function(test) {
             'Content-Type': 'text/plain' });
     });
     test.done();
+};
+
+exports['status - Set the status code'] = function(test) {
+    var response = httpMocks.createResponse();
+    response.status(401);
+    test.equal(401, response._getStatusCode());
+    test.done();
+};
+
+exports['send - Status code at the beginning'] = function(test) {
+    var s = 123;
+    var t = 'This is a weird status code';
+    
+    var response = httpMocks.createResponse();
+    response.send(s, t);
+
+    test.equal(s, response._getStatusCode());
+    test.equal(t, response._getData());
+    test.done();
+};
+
+exports['send - Status code at the end'] = function(test) {
+    var s = 543;
+    var t = 'This is a weird status code';
+    
+    var response = httpMocks.createResponse();
+    response.send(t, s);
+
+    test.equal(s, response._getStatusCode());
+    test.equal(t, response._getData());
+    test.done();
 };
\ No newline at end of file

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