[Pkg-javascript-commits] [node-mocks-http] 37/296: Merged ericchaves Restify-oriented changes.

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:18 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 30c62bf8352488f9cf2d6a731b6b5d72478860a6
Author: Howard Abrams <howard.abrams at gmail.com>
Date:   Sun Mar 30 17:26:41 2014 -0700

    Merged ericchaves Restify-oriented changes.
---
 README.md                 | 31 +++++++++++++++++++++++++++++++
 lib/mockResponse.js       | 34 +++++++++++++++++++++++++++-------
 package.json              |  9 ++++++++-
 run-tests                 | 11 ++++++-----
 test/test-mockResponse.js | 25 ++++++++++++++++++++++++-
 5 files changed, 96 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index dee35ff..e300a4f 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,19 @@ before calling and inspecting afterwards.
 We are looking for more volunteers to value to this project, including the
 creation of more objects from the [HTTP module](http://nodejs.org/docs/latest/api/http.html).
 
+For Developers
+=========
+
+Obviously this project doesn't address all features that must be
+mocked, but it is a start. Feel free to send pull requests, and I
+promise to be timely in merging them.
+
+After making any changes, please verify your work:
+
+  * npm install -g jshint
+  * npm install
+  * ./run-tests
+
 Release Notes
 =============
 
@@ -69,6 +82,24 @@ Most releases fixes bugs with our mocks or add features similar to the
 actual `Request` and `Response` objects offered by Node.js and extended
 by Express.
 
+v 1.0.3
+-------
+
+  * Merged changes by [invernizzie](https://github.com/invernizzie):
+    to address [#11](https://github.com/howardabrams/node-mocks-http/pull/11)
+
+  * Merged changes by [ericchaves](https://github.com/ericchaves):
+    > I extended your library a little but so it could also handle
+    > some structured responses. By doing so res.send now evaluate the
+    > data passed and search for either a statusCode or httpCode to be
+    > used, and also for a body to send as _data.
+    >
+    > It still working as expected (at least tests passed) for regular
+    > HTTP responses.
+    >
+    > Although I did it with node-restify in mind, it should work well
+    > for all other libs.
+
 v 1.0.2
 -------
 
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 025b082..56c1660 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -27,7 +27,7 @@
  */
 
 var WritableStream = require('./mockWritableStream'),
-  EventEmitter = require('./mockEventEmitter');
+    EventEmitter = require('./mockEventEmitter');
 
 exports.createResponse = function (options) {
   if (!options) {
@@ -105,31 +105,51 @@ exports.createResponse = function (options) {
      * @param data The data to return. Must be a string.
      */
     send: function (a, b, c) {
+      var _self = this;
+      var _formatData = function(a) {
+        if (typeof a === 'object') {
+          if (a.statusCode) {
+            _self.statusCode = a.statusCode;
+          }
+          else if (a.httpCode) {
+            _self.statusCode = a.statusCode;
+          }
+          if (a.body) {
+            _data = a.body;
+          }
+          else {
+            _data = a;
+          }
+        } else {
+          _data += a;
+        }
+      };
+
       switch (arguments.length) {
       case 1:
         if (typeof a === 'number') {
           this.statusCode = a;
         } else {
-          _data += a;
+          _formatData(a);
         }
         break;
 
       case 2:
         if (typeof a === 'number') {
+          _formatData(b);
           this.statusCode = a;
-          _data += b;
         } else if (typeof b === 'number') {
-          _data += a;
+          _formatData(a);
           this.statusCode = b;
           console.warn('WARNING: Called send() with deprecated parameter order');
         } else {
-          _data += a;
+          _formatData(a);
           _encoding = b;
         }
         break;
 
       case 3:
-        _data += a;
+        _formatData(a);
         _headers = b;
         this.statusCode = c;
         console.warn('WARNING: Called send() with deprecated three parameters');
@@ -465,4 +485,4 @@ exports.createResponse = function (options) {
       return _renderData;
     }
   };
-};
\ No newline at end of file
+};
diff --git a/package.json b/package.json
index 3a4e0d8..72b6053 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
   "author": "Howard Abrams <howard.abrams at gmail.com> (http://www.github.com/howardabrams)",
   "name": "node-mocks-http",
   "description": "Mock 'http' objects for testing Express routing functions",
-  "version": "1.0.2",
+  "version": "1.0.3",
   "homepage": "http://www.github.com/howardabrams/node-mocks-http",
   "keywords": [
                "mock",
@@ -22,5 +22,12 @@
   "main": "./lib/http-mock.js",
   "engines": {
     "node": ">=0.6"
+  },
+  "devDependencies":{
+    "nodeunit":"",
+    "node-restify-errors":"git://github.com/m9dfukc/node-restify-errors.git"
+  },
+  "scripts":{
+    "test": "./run-tests"
   }
 }
diff --git a/run-tests b/run-tests
index 142dc79..b7562c6 100755
--- a/run-tests
+++ b/run-tests
@@ -1,5 +1,5 @@
 #!/bin/sh
-# 
+#
 #  This will run the NodeUnit tests. If `nodeunit` isn't installed, type:
 #
 #      sudo npm install nodeunit
@@ -9,7 +9,7 @@ if type jshint >/dev/null 2>&1
 then
     FILES="`find examples lib -name '*.js'`"
     TMPHINT=/tmp/jshint-results
-    
+
     for FILE in $FILES
     do
         echo "Analyzing: $FILE"
@@ -32,9 +32,9 @@ then
     done
 fi
 
-# Usage: nodeunit [options] testmodule1.js testfolder [...] 
+# Usage: nodeunit [options] testmodule1.js testfolder [...]
 # Options:
-# 
+#
 #   --config FILE     the path to a JSON file with options
 #   --reporter FILE   optional path to a reporter file to customize the output
 #   --list-reporters  list available build-in reporters
@@ -42,7 +42,7 @@ fi
 #   -h, --help        display this help and exit
 #   -v, --version     output version information and exit
 
-# Build-in reporters: 
+# Build-in reporters:
 #   * browser: Browser-based test reporter
 #   * default: Default tests reporter
 #   * eclipse: Reporter for eclipse plugin
@@ -61,4 +61,5 @@ echo "Executing tests..."
 FILES="`find test -name 'test-*.js'` `find examples -name '*.js'`"
 
 # echo nodeunit $OPTS $FILES
+PATH=./node_modules/nodeunit/bin:$PATH
 exec nodeunit $OPTS $FILES
diff --git a/test/test-mockResponse.js b/test/test-mockResponse.js
index 5416441..e60b42f 100644
--- a/test/test-mockResponse.js
+++ b/test/test-mockResponse.js
@@ -315,4 +315,27 @@ exports['events - render'] = function (test) {
   });
 
   response.render(view, data, callback);
-};
\ No newline at end of file
+};
+
+
+exports['send - sending response objects a.k.a restifyError with statusCode'] = function(test) {
+  var errors = require('node-restify-errors')
+  var response = httpMocks.createResponse();
+  response.send(409, new errors.InvalidArgumentError("I just dont like you"));
+
+  test.equal(409, response._getStatusCode());
+  test.equal('InvalidArgument', response._getData().code);
+  test.equal('I just dont like you', response._getData().message);
+  test.done();
+};
+
+exports['send - sending response objects a.k.a restifyError without statusCode'] = function(test) {
+  var errors = require('node-restify-errors')
+  var response = httpMocks.createResponse();
+  response.send(new errors.InvalidArgumentError("I just dont like you"));
+
+  test.equal(409, response._getStatusCode());
+  test.equal('InvalidArgument', response._getData().code);
+  test.equal('I just dont like you', response._getData().message);
+  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