[Pkg-javascript-commits] [node-mocks-http] 27/296: Add support for render and redirect

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 c17c65708a3b952c99ecbd0a86558b5a7cc90d88
Author: Ray Sun <xiaoquqi at gmail.com>
Date:   Tue Aug 20 22:02:34 2013 +0800

    Add support for render and redirect
    
    Add new mock method render and redirect, also add _getRedirectUrl() and
    _getRenderView() and _getRenderData() methods to get the specific data
    
    Update README.md to version v1.0.0
---
 README.md                 |   5 ++
 lib/mockResponse.js       | 162 +++++++++++++++++++++++++++++++++++-----------
 test/test-mockResponse.js |  38 +++++++++++
 3 files changed, 166 insertions(+), 39 deletions(-)

diff --git a/README.md b/README.md
index a6f61c0..99bdd2c 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,11 @@ 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.0
+-------
+
+  * Add support for response redirect and render
+
 v 0.0.9
 -------
 
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 90c770b..1c139b2 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -37,6 +37,10 @@ exports.createResponse = function(options) {
     var _headers   = {};
     var _encoding  = options.encoding;
 
+    var _redirectUrl = "";
+    var _renderView = "";
+    var _renderData = {};
+
     var writableStream = new (options.writableStream || WritableStream)();
     var eventEmitter = new (options.eventEmitter || EventEmitter)();
 
@@ -210,43 +214,86 @@ exports.createResponse = function(options) {
             _encoding = encoding;
         },
 
-				writable: function(){
-					return writableStream.writable.apply(this, arguments);
-				},
-				// end: function(){
-				// 	return writableStream.end.apply(this, arguments);
-				// },
-				destroy: function(){
-					return writableStream.destroy.apply(this, arguments);
-				},
-				destroySoon: function(){
-					return writableStream.destroySoon.apply(this, arguments);
-				},
-				addListener: function(event, listener){
-					return eventEmitter.addListener.apply(this, arguments);
-				},
-				on: function(event, listener){
-					return eventEmitter.on.apply(this, arguments);
-				},
-				once: function(event, listener){
-					return eventEmitter.once.apply(this, arguments);
-				},
-				removeListener: function(event, listener){
-					return eventEmitter.removeListener.apply(this, arguments);
-				},
-				removeAllListeners: function(event){
-					return eventEmitter.removeAllListeners.apply(this, arguments);
-				},
-				setMaxListeners: function(n){
-					return eventEmitter.setMaxListeners.apply(this, arguments)
-				},
-				listeners: function(event){
-					return eventEmitter.listeners.apply(this, arguments);
-				},
-				emit: function(event){
-					return eventEmitter.emit.apply(this, arguments);
-				},
-				//
+        /**
+         * Function: redirect
+         *
+         *     Redirect to a url with response code
+         */
+        redirect: function(a, b) {
+            switch(arguments.length) {
+                case 1:
+                    _redirectUrl = a;
+                    break;
+
+                case 2:
+                    if (typeof a == 'number') {
+                        this.statusCode = a;
+                        _redirectUrl = b;
+                    }
+                    break;
+
+                default:
+                    break;
+            }
+        },
+
+        /**
+         * Function: render
+         *
+         *     Render a view with a callback responding with the
+         *     rendered string.
+         */
+        render: function(a, b, c) {
+            _renderView = a;
+            switch(arguments.length) {
+                case 2:
+                    break;
+
+                case 3:
+                    _renderData = b;
+                    break;
+
+                default:
+                    break;
+            }
+        },
+
+        writable: function(){
+            return writableStream.writable.apply(this, arguments);
+        },
+        // end: function(){
+        // 	return writableStream.end.apply(this, arguments);
+        // },
+        destroy: function(){
+            return writableStream.destroy.apply(this, arguments);
+        },
+        destroySoon: function(){
+            return writableStream.destroySoon.apply(this, arguments);
+        },
+        addListener: function(event, listener){
+            return eventEmitter.addListener.apply(this, arguments);
+        },
+        on: function(event, listener){
+            return eventEmitter.on.apply(this, arguments);
+        },
+        once: function(event, listener){
+            return eventEmitter.once.apply(this, arguments);
+        },
+        removeListener: function(event, listener){
+            return eventEmitter.removeListener.apply(this, arguments);
+        },
+        removeAllListeners: function(event){
+            return eventEmitter.removeAllListeners.apply(this, arguments);
+        },
+        setMaxListeners: function(n){
+            return eventEmitter.setMaxListeners.apply(this, arguments)
+        },
+        listeners: function(event){
+            return eventEmitter.listeners.apply(this, arguments);
+        },
+        emit: function(event){
+            return eventEmitter.emit.apply(this, arguments);
+        },
         //This mock object stores some state as well
         //as some test-analysis functions:
 
@@ -271,8 +318,6 @@ exports.createResponse = function(options) {
             return _headers;
         },
 
-
-
         /**
          * Function: _getData
          *
@@ -335,6 +380,45 @@ exports.createResponse = function(options) {
                 return (_headers["Content-Length"] == _data.length);
             }
             return true;
+        },
+
+        /**
+         * Function: _getRedirectUrl
+         *
+         *     Return redirect url of redirect method
+         *
+         * Returns:
+         *
+         *     Redirect url
+         */
+        _getRedirectUrl: function() {
+            return _redirectUrl;
+        },
+
+        /**
+         * Function: _getRenderView
+         *
+         *     Return render view of render method
+         *
+         * Returns:
+         *
+         *     render view
+         */
+        _getRenderView: function() {
+            return _renderView;
+        },
+
+        /**
+         * Function: _getRenderData
+         *
+         *     Return render data of render method
+         *
+         * Returns:
+         *
+         *     render data
+         */
+        _getRenderData: function() {
+            return _renderData;
         }
     };
 };
diff --git a/test/test-mockResponse.js b/test/test-mockResponse.js
index 8eef04f..c709a6a 100644
--- a/test/test-mockResponse.js
+++ b/test/test-mockResponse.js
@@ -192,3 +192,41 @@ exports['cookies - Cookie deletion'] = function(test) {
   test.deepEqual(response.cookies, {});
   test.done();
 };
+
+exports['redirect - Redirect to a url with response code'] = function(test) {
+    var response = httpMocks.createResponse();
+    var url = '/index';
+    var responseCode = 200;
+    response.redirect(responseCode, url);
+    test.equal(response._getRedirectUrl(), url);
+    test.equal(response._getStatusCode(), responseCode);
+    test.done();
+};
+
+exports['redirect - Redirect to a url without response code'] = function(test) {
+    var response = httpMocks.createResponse();
+    var url = '/index';
+    response.redirect(url);
+    test.equal(response._getRedirectUrl(), url);
+    test.done();
+};
+
+exports['render - Render to a view with data'] = function(test) {
+    var response = httpMocks.createResponse();
+    var view = 'index';
+    var data = { 'name': 'bob' };
+    var callback = function() {};
+    response.render(view, data, callback);
+    test.equal(response._getRenderView(), view);
+    test.deepEqual(response._getRenderData(), data);
+    test.done();
+};
+
+exports['render - Render to a view without data'] = function(test) {
+    var response = httpMocks.createResponse();
+    var view = 'index';
+    var callback = function() {};
+    response.render(view, callback);
+    test.equal(response._getRenderView(), view);
+    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