[Pkg-javascript-commits] [node-mocks-http] 20/296: Adding Support for Writable Stream. This commit is related to the issue #1. https://github.com/howardabrams/node-mocks-http/issues/1

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 c12b4d06c14d8facc91513a6191017e33cd37c66
Author: Pablo Cantero <pablo at pablocantero.com>
Date:   Sun Jul 1 19:35:52 2012 -0300

    Adding Support for Writable Stream. This commit is related to the issue #1. https://github.com/howardabrams/node-mocks-http/issues/1
---
 lib/mockEventEmitter.js   | 18 ++++++++++++++++++
 lib/mockResponse.js       | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 lib/mockWritableStream.js | 16 ++++++++++++++++
 test/test-mockResponse.js | 23 ++++++++++++++++++++++-
 4 files changed, 100 insertions(+), 3 deletions(-)

diff --git a/lib/mockEventEmitter.js b/lib/mockEventEmitter.js
new file mode 100644
index 0000000..05b04fd
--- /dev/null
+++ b/lib/mockEventEmitter.js
@@ -0,0 +1,18 @@
+/*
+ * http://nodejs.org/api/events.html
+*/
+
+function EventEmitter(){}
+
+EventEmitter.prototype.addListener = function(event, listener){}
+EventEmitter.prototype.on = function(event, listener){}
+EventEmitter.prototype.once = function(event, listener){}
+EventEmitter.prototype.removeListener = function(event, listener){}
+EventEmitter.prototype.removeAllListeners = function(event){}
+// EventEmitter.prototype.removeAllListeners = function([event])
+EventEmitter.prototype.setMaxListeners = function(n){}
+EventEmitter.prototype.listeners = function(event){}
+EventEmitter.prototype.emit = function(event){}
+// EventEmitter.prototype.emit = function(event, [arg1], [arg2], [...]){}
+
+module.exports = EventEmitter;
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 327825c..5596879 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -24,6 +24,9 @@
  *   encoding - The default encoding for the response
  */
 
+var WritableStream = require('./mockWritableStream')
+	, EventEmitter = require('./mockEventEmitter');
+
 exports.createResponse = function(options) {
     if (!options) {
         options = {};
@@ -34,6 +37,9 @@ exports.createResponse = function(options) {
     var _headers   = {};
     var _encoding  = options.encoding;
 
+		var writableStream = new (options.writableStream || WritableStream)();
+		var eventEmitter = new (options.eventEmitter || EventEmitter)();
+
     return {
         statusCode: -1,
 
@@ -193,8 +199,44 @@ exports.createResponse = function(options) {
         setEncoding: function(encoding) {
             _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); 
+				},
+				//         
         //This mock object stores some state as well
         //as some test-analysis functions:
         
diff --git a/lib/mockWritableStream.js b/lib/mockWritableStream.js
new file mode 100644
index 0000000..baaceb6
--- /dev/null
+++ b/lib/mockWritableStream.js
@@ -0,0 +1,16 @@
+/*
+ * http://nodejs.org/api/stream.html#stream_writable_stream
+*/
+
+function WritableStream(){}
+
+WritableStream.prototype.writable = function(){}
+// WritableStream.prototype.write = function(string, [encoding], [fd]){}
+// WritableStream.prototype.write = function(buffer){}
+WritableStream.prototype.end = function(){}
+// WritableStream.prototype.end = function(string, encoding){}
+// WritableStream.prototype.end = function(buffer){}
+WritableStream.prototype.destroy = function(){}
+WritableStream.prototype.destroySoon = function(){}
+
+module.exports = WritableStream;
diff --git a/test/test-mockResponse.js b/test/test-mockResponse.js
index c0cb002..309f288 100644
--- a/test/test-mockResponse.js
+++ b/test/test-mockResponse.js
@@ -149,4 +149,25 @@ exports['send - Status code at the end'] = function(test) {
     test.equal(s, response._getStatusCode());
     test.equal(t, response._getData());
     test.done();
-};
\ No newline at end of file
+};
+
+exports['implement - WriteableStream'] = function(test){
+	var response = httpMocks.createResponse();	
+	test.equal(typeof(response.writable), 'function');
+	test.equal(typeof(response.destroy), 'function');
+	test.equal(typeof(response.destroySoon), 'function');
+	test.done();
+};
+
+exports['implement - EventEmitter'] = function(test){
+	var response = httpMocks.createResponse();	
+	test.equal(typeof(response.addListener), 'function');
+	test.equal(typeof(response.on), 'function');
+	test.equal(typeof(response.once), 'function');
+	test.equal(typeof(response.removeListener), 'function');
+	test.equal(typeof(response.removeAllListeners), 'function');
+	test.equal(typeof(response.setMaxListeners), 'function');
+	test.equal(typeof(response.listeners), 'function');
+	test.equal(typeof(response.emit), 'function');
+	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