[Pkg-javascript-commits] [node-mocks-http] 22/296: Added cookies property to response object

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:17 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 2e8f96e5d0dee101c032d09e6015b6d8abb5bca2
Author: Andrew E Gall <agall at groupon.com>
Date:   Sat Mar 2 01:33:45 2013 +0100

    Added cookies property to response object
---
 lib/mockResponse.js       | 110 +++++++++++++++++++++++-----------------------
 test/test-mockResponse.js |  48 ++++++++++++--------
 2 files changed, 86 insertions(+), 72 deletions(-)

diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 5596879..0ed66e0 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -1,10 +1,10 @@
 /**
  * File: mockResponse
- * 
+ *
  *  This file implements node.js's implementation of a 'response' object.
- *  Like all good mocks, the response file that can be called and used in 
+ *  Like all good mocks, the response file that can be called and used in
  *  place of a real HTTP response object.
- *  
+ *
  * @author Howard Abrams <howard.abrams at gmail.com>
  */
 
@@ -13,7 +13,7 @@
  * Function: createResponse
  *
  *    Creates a new mock 'response' instance. All values are reset to the
- *    defaults. 
+ *    defaults.
  *
  * Parameters:
  *
@@ -37,22 +37,24 @@ exports.createResponse = function(options) {
     var _headers   = {};
     var _encoding  = options.encoding;
 
-		var writableStream = new (options.writableStream || WritableStream)();
-		var eventEmitter = new (options.eventEmitter || EventEmitter)();
+    var writableStream = new (options.writableStream || WritableStream)();
+    var eventEmitter = new (options.eventEmitter || EventEmitter)();
 
     return {
         statusCode: -1,
 
+        cookies: {},
+
         status: function(code) {
             this.statusCode = code;
             return this;
         },
-        
+
         /**
          * Function: writeHead
          *
          *  The 'writeHead' function from node's HTTP API.
-         * 
+         *
          * Parameters:
          *
          *  statusCode - A number to send as a the HTTP status
@@ -63,9 +65,9 @@ exports.createResponse = function(options) {
             if (_endCalled) {
                 throw "The end() method has already been called.";
             }
-            
+
             this.statusCode = statusCode;
-            
+
             // Note: Not sure if the headers given in this function
             //       overwrite any headers specified earlier.
             if (headers) {
@@ -76,11 +78,11 @@ exports.createResponse = function(options) {
                 _headers = phrase;
             }
         },
-        
+
         /**
          *  The 'send' function from node's HTTP API that returns data
          *  to the client. Can be called multiple times.
-         * 
+         *
          * @param data The data to return. Must be a string.
          */
         send: function( a, b, c ) {
@@ -88,36 +90,36 @@ exports.createResponse = function(options) {
                 case 1:
                     _data += a;
                     break;
-                    
+
                 case 2:
                     if (typeof a == 'number') {
-                        this.statusCode = a;                        
+                        this.statusCode = a;
                         _data += b;
                     }
                     else if (typeof b == 'number') {
                         _data += a;
                         this.statusCode = b;
-                        console.warn("WARNING: Called 'send' with deprecated parameter order"); 
+                        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"); 
+                    console.warn("WARNING: Called 'send' with deprecated three parameters");
                     break;
-                    
+
                 default:
                     break;
             }
         },
-        
-        
+
+
         /**
          * Function: write
          *
@@ -125,11 +127,11 @@ exports.createResponse = function(options) {
          *
          * Parameters:
          *
-         *  data - The data to return. Must be a string. Appended to 
+         *  data - The data to return. Must be a string. Appended to
          *         previous calls to data.
          *  encoding - Optional encoding value.
          */
-        
+
         write: function( data, encoding ) {
             _data += data;
             if (encoding) {
@@ -142,11 +144,11 @@ exports.createResponse = function(options) {
          *
          *  The 'end' function from node's HTTP API that finishes
          *  the connection request. This must be called.
-         * 
+         *
          * Parameters:
          *
          *  data - Optional data to return. Must be a string. Appended
-         *         to previous calls to <send>.  
+         *         to previous calls to <send>.
          *  encoding - Optional encoding value.
          */
         end: function( data, encoding ) {
@@ -200,46 +202,46 @@ exports.createResponse = function(options) {
             _encoding = encoding;
         },
 
-				writable: function(){ 
-					return writableStream.writable.apply(this, arguments); 
+				writable: function(){
+					return writableStream.writable.apply(this, arguments);
 				},
-				// end: function(){ 
-				// 	return writableStream.end.apply(this, arguments); 
+				// end: function(){
+				// 	return writableStream.end.apply(this, arguments);
 				// },
-				destroy: function(){ 
-					return writableStream.destroy.apply(this, arguments); 
+				destroy: function(){
+					return writableStream.destroy.apply(this, arguments);
 				},
-				destroySoon: function(){ 
-					return writableStream.destroySoon.apply(this, arguments); 
+				destroySoon: function(){
+					return writableStream.destroySoon.apply(this, arguments);
 				},
-				addListener: function(event, listener){ 
-					return eventEmitter.addListener.apply(this, arguments); 
+				addListener: function(event, listener){
+					return eventEmitter.addListener.apply(this, arguments);
 				},
-				on: function(event, listener){ 
-					return eventEmitter.on.apply(this, arguments); 
+				on: function(event, listener){
+					return eventEmitter.on.apply(this, arguments);
 				},
-				once: function(event, listener){ 
-					return eventEmitter.once.apply(this, arguments); 
+				once: function(event, listener){
+					return eventEmitter.once.apply(this, arguments);
 				},
-				removeListener: function(event, listener){ 
-					return eventEmitter.removeListener.apply(this, arguments); 
+				removeListener: function(event, listener){
+					return eventEmitter.removeListener.apply(this, arguments);
 				},
-				removeAllListeners: function(event){ 
-					return eventEmitter.removeAllListeners.apply(this, arguments); 
+				removeAllListeners: function(event){
+					return eventEmitter.removeAllListeners.apply(this, arguments);
 				},
-				setMaxListeners: function(n){ 
-					return eventEmitter.setMaxListeners.apply(this, arguments) 
+				setMaxListeners: function(n){
+					return eventEmitter.setMaxListeners.apply(this, arguments)
 				},
-				listeners: function(event){ 
-					return eventEmitter.listeners.apply(this, arguments); 
+				listeners: function(event){
+					return eventEmitter.listeners.apply(this, arguments);
 				},
-				emit: function(event){ 
-					return eventEmitter.emit.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:
-        
+
         /**
          * Function: _isEndCalled
          *
@@ -250,7 +252,7 @@ exports.createResponse = function(options) {
             return _endCalled;
         },
 
-        
+
         /**
          * Function: _getHeaders
          *
@@ -290,13 +292,13 @@ exports.createResponse = function(options) {
         _isJSON: function() {
             return (_headers["Content-Type"] == "application/json");
         },
-        
+
         /**
          * Function: _isUTF8
          *
          *    If the encoding was set, and it was set to UTF-8, then
          *    this function return true. False otherwise.
-         *    
+         *
          * Returns:
          *
          *   False if the encoding wasn't set and wasn't set to "utf8".
@@ -327,4 +329,4 @@ exports.createResponse = function(options) {
             return true;
         }
     };
-};
\ No newline at end of file
+};
diff --git a/test/test-mockResponse.js b/test/test-mockResponse.js
index 309f288..c46fd0d 100644
--- a/test/test-mockResponse.js
+++ b/test/test-mockResponse.js
@@ -8,7 +8,7 @@ var httpMocks = require('../lib/http-mock');
 
 exports['object - Simple verification'] = function( test ) {
     var response = httpMocks.createResponse();
-    
+
     response.send("Hello", 'utf8');
     response.send("World");
     test.equal("HelloWorld", response._getData());
@@ -31,7 +31,7 @@ exports['end - Simple Verification'] = function(test) {
 
     response.send("Hello");
     response.end("World");
-    
+
     test.equal("HelloWorld", response._getData());
 
     test.ok(response._isEndCalled());
@@ -42,7 +42,7 @@ exports['end - No Data Called'] = function(test) {
     var response = httpMocks.createResponse();
 
     response.end("Hello World");
-    
+
     test.equal("Hello World", response._getData());
 
     test.ok(response._isEndCalled());
@@ -51,10 +51,10 @@ exports['end - No Data Called'] = function(test) {
 
 exports['write - Simple verification'] = function( test ) {
     var response = httpMocks.createResponse();
-    
+
     response.write("Hello", 'utf8');
     response.end("World");
-    
+
     test.equal("HelloWorld", response._getData());
 
     test.ok( response._isUTF8());
@@ -64,25 +64,25 @@ exports['write - Simple verification'] = function( test ) {
 
 exports['setHeader - Simple verification'] = function(test) {
     var response = httpMocks.createResponse();
-    
+
     response.setHeader('foo', 'bar');
     response.setHeader('bling', 'blang');
-    
+
     test.equal('bar', response.getHeader('foo'));
     test.equal('blang', response.getHeader('bling'));
 
     response.removeHeader('bling');
     test.ok( !response.getHeader('bling'));
-    
+
     test.done();
 };
 
 exports['setHeader - Can not call after end'] = function(test) {
     var response = httpMocks.createResponse();
-    
+
     var body = 'hello world';
     response.end(body);
-    
+
     test.throws( function() {
         response.setHead('Content-Length', body.length);
     });
@@ -91,13 +91,13 @@ exports['setHeader - Can not call after end'] = function(test) {
 
 exports['writeHead - Simple verification'] = function(test) {
     var response = httpMocks.createResponse();
-    
+
     var body = 'hello world';
     response.writeHead(200, {
       'Content-Length': body.length,
       'Content-Type': 'text/plain' });
     response.end(body);
-    
+
     test.equal(200, response._getStatusCode() );
     test.equal(body, response._getData() );
     test.ok(response._isDataLengthValid() );
@@ -108,10 +108,10 @@ exports['writeHead - Simple verification'] = function(test) {
 
 exports['writeHead - Can not call after end'] = function(test) {
     var response = httpMocks.createResponse();
-    
+
     var body = 'hello world';
     response.end(body);
-    
+
     test.throws( function() {
         response.writeHead(200, {
             'Content-Length': body.length,
@@ -130,7 +130,7 @@ exports['status - Set the status code'] = function(test) {
 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);
 
@@ -142,7 +142,7 @@ exports['send - Status code at the beginning'] = function(test) {
 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);
 
@@ -152,7 +152,7 @@ exports['send - Status code at the end'] = function(test) {
 };
 
 exports['implement - WriteableStream'] = function(test){
-	var response = httpMocks.createResponse();	
+	var response = httpMocks.createResponse();
 	test.equal(typeof(response.writable), 'function');
 	test.equal(typeof(response.destroy), 'function');
 	test.equal(typeof(response.destroySoon), 'function');
@@ -160,7 +160,7 @@ exports['implement - WriteableStream'] = function(test){
 };
 
 exports['implement - EventEmitter'] = function(test){
-	var response = httpMocks.createResponse();	
+	var response = httpMocks.createResponse();
 	test.equal(typeof(response.addListener), 'function');
 	test.equal(typeof(response.on), 'function');
 	test.equal(typeof(response.once), 'function');
@@ -171,3 +171,15 @@ exports['implement - EventEmitter'] = function(test){
 	test.equal(typeof(response.emit), 'function');
 	test.done();
 };
+
+exports['cookies - Cookies creation'] = function(test) {
+  var response = httpMocks.createResponse();
+  test.deepEqual(response.cookies, {});
+  test.done();
+}
+exports['cookies - Cookies assignment'] = function(test) {
+  var response = httpMocks.createResponse();
+  response.cookies.egg = 'chicken';
+  test.deepEqual(response.cookies, {egg: 'chicken'});
+  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