[Pkg-javascript-commits] [node-mocks-http] 29/296: Cleaned up to pass JSHint
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 0df9b3557edf459a4297dd22a806b976d1048857
Author: James Diacono <james at diacono.com.au>
Date: Fri Dec 13 16:16:08 2013 +1100
Cleaned up to pass JSHint
---
.jshintrc | 20 ++
examples/express-route.js | 74 +++--
lib/mockEventEmitter.js | 22 +-
lib/mockRequest.js | 308 +++++++++---------
lib/mockResponse.js | 804 +++++++++++++++++++++++-----------------------
lib/mockWritableStream.js | 14 +-
6 files changed, 639 insertions(+), 603 deletions(-)
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..a6027e4
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,20 @@
+{
+ "node": true,
+ "esnext": true,
+ "bitwise": true,
+ "camelcase": true,
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "indent": 2,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "quotmark": "single",
+ "regexp": true,
+ "undef": true,
+ "unused": false,
+ "strict": true,
+ "trailing": true,
+ "smarttabs": true
+}
diff --git a/examples/express-route.js b/examples/express-route.js
index 0e5542a..c9f9f08 100644
--- a/examples/express-route.js
+++ b/examples/express-route.js
@@ -1,3 +1,5 @@
+'use strict';
+
var httpMocks = require('../lib/http-mock');
// Suppose we have the following magical Express incantation:
@@ -6,44 +8,46 @@ var httpMocks = require('../lib/http-mock');
//
// And we have ourselves a function to answer that call:
-var aroute = function( request, response ) {
-
- var id = request.params.id;
- console.log("We have a '%s' request for %s (ID: %d)",
- request.method, request.url, id);
-
- var body = {
- name: 'Bob Dog',
- age: 42,
- email: 'bob at dog.com'
- };
- response.setHeader('Content-Type', 'application/json');
- response.statusCode = 200;
- response.send( JSON.stringify(body), 'utf8' );
- response.end();
+var aroute = function (request, response) {
+
+ var id = request.params.id;
+ console.log('We have a \'%s\' request for %s (ID: %d)',
+ request.method, request.url, id);
+
+ var body = {
+ name: 'Bob Dog',
+ age: 42,
+ email: 'bob at dog.com'
+ };
+ response.setHeader('Content-Type', 'application/json');
+ response.statusCode = 200;
+ response.send(JSON.stringify(body), 'utf8');
+ response.end();
};
// -----------------------------------------------------------------
// In another file, we can now test that function, like so:
-exports['aroute - Simple testing'] = function(test) {
- var request = httpMocks.createRequest({
- method: 'GET',
- url: '/user/42',
- params: { id: 42 }
- });
- var response = httpMocks.createResponse();
-
- aroute(request, response);
-
- var data = JSON.parse( response._getData() );
- test.equal("Bob Dog", data.name);
- test.equal(42, data.age);
- test.equal("bob at dog.com", data.email);
-
- test.equal(200, response.statusCode );
- test.ok( response._isEndCalled());
- test.ok( response._isJSON());
- test.ok( response._isUTF8());
- test.done();
+exports['aroute - Simple testing'] = function (test) {
+ var request = httpMocks.createRequest({
+ method: 'GET',
+ url: '/user/42',
+ params: {
+ id: 42
+ }
+ });
+ var response = httpMocks.createResponse();
+
+ aroute(request, response);
+
+ var data = JSON.parse(response._getData());
+ test.equal('Bob Dog', data.name);
+ test.equal(42, data.age);
+ test.equal('bob at dog.com', data.email);
+
+ test.equal(200, response.statusCode);
+ test.ok(response._isEndCalled());
+ test.ok(response._isJSON());
+ test.ok(response._isUTF8());
+ test.done();
};
\ No newline at end of file
diff --git a/lib/mockEventEmitter.js b/lib/mockEventEmitter.js
index 05b04fd..9662692 100644
--- a/lib/mockEventEmitter.js
+++ b/lib/mockEventEmitter.js
@@ -1,18 +1,18 @@
/*
* http://nodejs.org/api/events.html
-*/
+ */
-function EventEmitter(){}
+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.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.setMaxListeners = function (n) {};
+EventEmitter.prototype.listeners = function (event) {};
+EventEmitter.prototype.emit = function (event) {};
// EventEmitter.prototype.emit = function(event, [arg1], [arg2], [...]){}
-module.exports = EventEmitter;
+module.exports = EventEmitter;
\ No newline at end of file
diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index 5c2097b..849fe66 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -1,10 +1,12 @@
+'use strict';
+
/**
* File: mockRequest
- *
+ *
* This file implements node.js's implementation of a 'request' object.
* This is actually closer to what Express offers the user, in that the
* body is really a parsed object of values.
- *
+ *
* @author Howard Abrams <howard.abrams at gmail.com>
*/
@@ -12,7 +14,7 @@
* Function: createRequest
*
* Creates a new mock 'request' instance. All values are reset to the
- * defaults.
+ * defaults.
*
* Parameters:
*
@@ -23,156 +25,156 @@
* method - The method value, see <mockRequest._setMethod>
* url - The url value, see <mockRequest._setURL>
* params - The parameters, see <mockRequest._setParam>
- * body - The body values, , see <mockRequest._setBody>
+ * body - The body values, , see <mockRequest._setBody>
*/
-exports.createRequest = function(options) {
- if (!options) {
- options = {};
- }
+exports.createRequest = function (options) {
+ if (!options) {
+ options = {};
+ }
+
+ return {
+ method: (options.method) ? options.method : 'GET',
+ url: (options.url) ? options.url : '',
+ params: (options.params) ? options.params : {},
+ session: (options.session) ? options.session : {},
+ cookies: (options.cookies) ? options.cookies : {},
+ headers: (options.headers) ? options.headers : {},
+ body: (options.body) ? options.body : {},
+ query: (options.query) ? options.query : {},
+ files: (options.files) ? options.files : {},
+
+ /**
+ * Function: _setParameter
+ *
+ * Set parameters that the client can then get using the 'params'
+ * key.
+ *
+ * Parameters:
+ *
+ * key - The key. For instance, 'bob' would be accessed: request.params.bob
+ * value - The value to return when accessed.
+ */
+
+ _setParameter: function (key, value) {
+ this.params[key] = value;
+ },
+
+ /**
+ * Sets a variable that is stored in the session.
+ *
+ * @param variable The variable to store in the session
+ * @param value The value associated with the variable
+ */
+ _setSessionVariable: function (variable, value) {
+ this.session[variable] = value;
+ },
+
+ /**
+ * Sets a variable that is stored in the cookies.
+ *
+ * @param variable The variable to store in the cookies
+ * @param value The value associated with the variable
+ */
+ _setCookiesVariable: function (variable, value) {
+ this.cookies[variable] = value;
+ },
+
+ /**
+ * Sets a variable that is stored in the headers.
+ *
+ * @param variable The variable to store in the headers
+ * @param value The value associated with the variable
+ */
+ _setHeadersVariable: function (variable, value) {
+ this.headers[variable] = value;
+ },
- return {
- method : (options.method) ? options.method : 'GET',
- url : (options.url ) ? options.url : '',
- params : (options.params) ? options.params : {},
- session: (options.session) ? options.session : {},
- cookies: (options.cookies) ? options.cookies : {},
- headers: (options.headers) ? options.headers: {},
- body : (options.body ) ? options.body : {},
- query : (options.query ) ? options.query : {},
- files : (options.files ) ? options.files : {},
-
- /**
- * Function: _setParameter
- *
- * Set parameters that the client can then get using the 'params'
- * key.
- *
- * Parameters:
- *
- * key - The key. For instance, 'bob' would be accessed: request.params.bob
- * value - The value to return when accessed.
- */
-
- _setParameter: function( key, value ) {
- this.params[key] = value;
- },
-
- /**
- * Sets a variable that is stored in the session.
- *
- * @param variable The variable to store in the session
- * @param value The value associated with the variable
- */
- _setSessionVariable: function( variable, value ) {
- this.session[variable] = value;
- },
-
- /**
- * Sets a variable that is stored in the cookies.
- *
- * @param variable The variable to store in the cookies
- * @param value The value associated with the variable
- */
- _setCookiesVariable: function( variable, value ) {
- this.cookies[variable] = value;
- },
-
- /**
- * Sets a variable that is stored in the headers.
- *
- * @param variable The variable to store in the headers
- * @param value The value associated with the variable
- */
- _setHeadersVariable: function( variable, value ) {
- this.headers[variable] = value;
- },
-
- /**
- * Sets a variable that is stored in the files.
- *
- * @param variable The variable to store in the files
- * @param value The value associated with the variable
- */
- _setFilesVariable: function( variable, value ) {
- this.files[variable] = value;
- },
-
- /**
- * Function: _setMethod
- *
- * Sets the HTTP method that the client gets when the called the 'method'
- * property. This defaults to 'GET' if it is not set.
- *
- * Parameters:
- *
- * method - The HTTP method, e.g. GET, POST, PUT, DELETE, etc.
- *
- * Note: We don't validate the string. We just return it.
- */
-
- _setMethod: function( method ) {
- this.method = method;
- },
-
- /**
- * Function: _setURL
- *
- * Sets the URL value that the client gets when the called the 'url'
- * property.
- *
- * Parameters:
- *
- * url - The request path, e.g. /my-route/452
- *
- * Note: We don't validate the string. We just return it. Typically, these
- * do not include hostname, port or that part of the URL.
- */
-
- _setURL: function( url ) {
- this.url = url;
- },
-
- /**
- * Function: _setBody
- *
- * Sets the body that the client gets when the called the 'body'
- * parameter. This defaults to 'GET' if it is not set.
- *
- * Parameters:
- *
- * body - An object representing the body.
- *
- * If you expect the 'body' to come from a form, this typically means that
- * it would be a flat object of properties and values, as in:
- *
- * > { name: 'Howard Abrams',
- * > age: 522
- * > }
- *
- * If the client is expecting a JSON object through a REST interface, then
- * this object could be anything.
- */
-
- _setBody: function( body ) {
- this.body = body;
- },
-
- /**
- * Function: _addBody
- *
- * Adds another body parameter the client gets when calling the 'body'
- * parameter with another property value, e.g. the name of a form element
- * that was passed in.
- *
- * Parameters:
- *
- * key - The key. For instance, 'bob' would be accessed: request.params.bob
- * value - The value to return when accessed.
- */
-
- _addBody: function( key, value ) {
- this.body[key] = value;
- }
- };
-};
+ /**
+ * Sets a variable that is stored in the files.
+ *
+ * @param variable The variable to store in the files
+ * @param value The value associated with the variable
+ */
+ _setFilesVariable: function (variable, value) {
+ this.files[variable] = value;
+ },
+
+ /**
+ * Function: _setMethod
+ *
+ * Sets the HTTP method that the client gets when the called the 'method'
+ * property. This defaults to 'GET' if it is not set.
+ *
+ * Parameters:
+ *
+ * method - The HTTP method, e.g. GET, POST, PUT, DELETE, etc.
+ *
+ * Note: We don't validate the string. We just return it.
+ */
+
+ _setMethod: function (method) {
+ this.method = method;
+ },
+
+ /**
+ * Function: _setURL
+ *
+ * Sets the URL value that the client gets when the called the 'url'
+ * property.
+ *
+ * Parameters:
+ *
+ * url - The request path, e.g. /my-route/452
+ *
+ * Note: We don't validate the string. We just return it. Typically, these
+ * do not include hostname, port or that part of the URL.
+ */
+
+ _setURL: function (url) {
+ this.url = url;
+ },
+
+ /**
+ * Function: _setBody
+ *
+ * Sets the body that the client gets when the called the 'body'
+ * parameter. This defaults to 'GET' if it is not set.
+ *
+ * Parameters:
+ *
+ * body - An object representing the body.
+ *
+ * If you expect the 'body' to come from a form, this typically means that
+ * it would be a flat object of properties and values, as in:
+ *
+ * > { name: 'Howard Abrams',
+ * > age: 522
+ * > }
+ *
+ * If the client is expecting a JSON object through a REST interface, then
+ * this object could be anything.
+ */
+
+ _setBody: function (body) {
+ this.body = body;
+ },
+
+ /**
+ * Function: _addBody
+ *
+ * Adds another body parameter the client gets when calling the 'body'
+ * parameter with another property value, e.g. the name of a form element
+ * that was passed in.
+ *
+ * Parameters:
+ *
+ * key - The key. For instance, 'bob' would be accessed: request.params.bob
+ * value - The value to return when accessed.
+ */
+
+ _addBody: function (key, value) {
+ this.body[key] = value;
+ }
+ };
+};
\ No newline at end of file
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 1c139b2..f9011b5 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -1,3 +1,5 @@
+'use strict';
+
/**
* File: mockResponse
*
@@ -24,401 +26,409 @@
* encoding - The default encoding for the response
*/
-var WritableStream = require('./mockWritableStream')
- , EventEmitter = require('./mockEventEmitter');
-
-exports.createResponse = function(options) {
- if (!options) {
- options = {};
- }
-
- var _endCalled = false;
- var _data = "";
- var _headers = {};
- var _encoding = options.encoding;
-
- var _redirectUrl = "";
- var _renderView = "";
- var _renderData = {};
-
- var writableStream = new (options.writableStream || WritableStream)();
- var eventEmitter = new (options.eventEmitter || EventEmitter)();
-
- return {
- statusCode: -1,
-
- cookies: {},
-
- cookie: function(name, value, options) {
- this.cookies[name] = { value: value, options: options };
- },
-
- clearCookie: function(name) {
- delete this.cookies[name]
- },
-
- 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
- * headers - An object of properties that will be used for
- * the HTTP headers.
- */
- writeHead: function( statusCode, phrase, headers ) {
- 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) {
- _reasonPhrase = phrase;
- _headers = headers;
- }
- else {
- _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 ) {
- 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;
- }
- },
-
-
- /**
- * Function: write
- *
- * This function has the same behavior as the 'send' function.
- *
- * Parameters:
- *
- * 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) {
- _encoding = encoding;
- }
- },
-
- /**
- * Function: end
- *
- * 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>.
- * encoding - Optional encoding value.
- */
- end: function( data, encoding ) {
- _endCalled = true;
- if (data) {
- _data += data;
- }
- if (encoding) {
- _encoding = encoding;
- }
- },
-
-
- /**
- * Function: getHeader
- *
- * Returns a particular header by name.
- */
- getHeader: function(name) {
- return _headers[name];
- },
-
- /**
- * Function: setHeader
- *
- * Set a particular header by name.
- */
- setHeader: function(name, value) {
- return _headers[name] = value;
- },
-
- /**
- * Function: removeHeader
- *
- * Removes an HTTP header by name.
- */
- removeHeader: function(name) {
- delete _headers[name];
- },
-
- /**
- * Function: setEncoding
- *
- * Sets the encoding for the data. Generally 'utf8'.
- *
- * Parameters:
- *
- * encoding - The string representing the encoding value.
- */
- setEncoding: function(encoding) {
- _encoding = encoding;
- },
-
- /**
- * 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:
-
- /**
- * Function: _isEndCalled
- *
- * Since the <end> function must be called, this function
- * returns true if it has been called. False otherwise.
- */
- _isEndCalled: function() {
- return _endCalled;
- },
-
-
- /**
- * Function: _getHeaders
- *
- * Returns all the headers that were set. This may be an
- * empty object, but probably will have "Content-Type" set.
- */
- _getHeaders: function() {
- return _headers;
- },
-
- /**
- * Function: _getData
- *
- * The data sent to the user.
- */
- _getData: function() {
- return _data;
- },
-
- /**
- * Function: _getStatusCode
- *
- * The status code that was sent to the user.
- */
- _getStatusCode: function() {
- return this.statusCode;
- },
-
- /**
- * Function: _isJSON
- *
- * Returns true if the data sent was defined as JSON.
- * It doesn't validate the data that was sent.
- */
- _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".
- */
- _isUTF8: function() {
- if ( !_encoding ) {
- return false;
- }
- return ( _encoding === "utf8" );
- },
-
- /**
- * Function: _isDataLengthValid
- *
- * If the Content-Length header was set, this will only
- * return true if the length is actually the length of the
- * data that was set.
- *
- * Returns:
- *
- * True if the "Content-Length" header was not
- * set. Otherwise, it compares it.
- */
- _isDataLengthValid: function() {
- if (_headers["Content-Length"]) {
- 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;
+var WritableStream = require('./mockWritableStream'),
+ EventEmitter = require('./mockEventEmitter');
+
+exports.createResponse = function (options) {
+ if (!options) {
+ options = {};
+ }
+
+ var _endCalled = false;
+ var _data = '';
+ var _headers = {};
+ var _encoding = options.encoding;
+
+ var _redirectUrl = '';
+ var _renderView = '';
+ var _renderData = {};
+
+ var writableStream = options.writableStream ?
+ new options.writableStream() :
+ new WritableStream();
+ var eventEmitter = options.eventEmitter ?
+ new options.eventEmitter() :
+ new EventEmitter();
+
+ return {
+ statusCode: -1,
+
+ cookies: {},
+
+ cookie: function (name, value, options) {
+ this.cookies[name] = {
+ value: value,
+ options: options
+ };
+ },
+
+ clearCookie: function (name) {
+ delete this.cookies[name];
+ },
+
+ 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
+ * headers - An object of properties that will be used for
+ * the HTTP headers.
+ */
+ writeHead: function (statusCode, phrase, headers) {
+ 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) {
+ _headers = headers;
+ } else {
+ _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) {
+ switch (arguments.length) {
+ case 1:
+ if (typeof a === 'number') {
+ this.statusCode = a;
+ } else {
+ _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;
+ }
+ },
+
+
+ /**
+ * Function: write
+ *
+ * This function has the same behavior as the 'send' function.
+ *
+ * Parameters:
+ *
+ * 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) {
+ _encoding = encoding;
+ }
+ },
+
+ /**
+ * Function: end
+ *
+ * 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>.
+ * encoding - Optional encoding value.
+ */
+ end: function (data, encoding) {
+ _endCalled = true;
+ if (data) {
+ _data += data;
+ }
+ if (encoding) {
+ _encoding = encoding;
+ }
+ },
+
+
+ /**
+ * Function: getHeader
+ *
+ * Returns a particular header by name.
+ */
+ getHeader: function (name) {
+ return _headers[name];
+ },
+
+ /**
+ * Function: setHeader
+ *
+ * Set a particular header by name.
+ */
+ setHeader: function (name, value) {
+ _headers[name] = value;
+ return value;
+ },
+
+ /**
+ * Function: removeHeader
+ *
+ * Removes an HTTP header by name.
+ */
+ removeHeader: function (name) {
+ delete _headers[name];
+ },
+
+ /**
+ * Function: setEncoding
+ *
+ * Sets the encoding for the data. Generally 'utf8'.
+ *
+ * Parameters:
+ *
+ * encoding - The string representing the encoding value.
+ */
+ setEncoding: function (encoding) {
+ _encoding = encoding;
+ },
+
+ /**
+ * 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:
+
+ /**
+ * Function: _isEndCalled
+ *
+ * Since the <end> function must be called, this function
+ * returns true if it has been called. False otherwise.
+ */
+ _isEndCalled: function () {
+ return _endCalled;
+ },
+
+
+ /**
+ * Function: _getHeaders
+ *
+ * Returns all the headers that were set. This may be an
+ * empty object, but probably will have "Content-Type" set.
+ */
+ _getHeaders: function () {
+ return _headers;
+ },
+
+ /**
+ * Function: _getData
+ *
+ * The data sent to the user.
+ */
+ _getData: function () {
+ return _data;
+ },
+
+ /**
+ * Function: _getStatusCode
+ *
+ * The status code that was sent to the user.
+ */
+ _getStatusCode: function () {
+ return this.statusCode;
+ },
+
+ /**
+ * Function: _isJSON
+ *
+ * Returns true if the data sent was defined as JSON.
+ * It doesn't validate the data that was sent.
+ */
+ _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".
+ */
+ _isUTF8: function () {
+ if (!_encoding) {
+ return false;
+ }
+ return (_encoding === 'utf8');
+ },
+
+ /**
+ * Function: _isDataLengthValid
+ *
+ * If the Content-Length header was set, this will only
+ * return true if the length is actually the length of the
+ * data that was set.
+ *
+ * Returns:
+ *
+ * True if the "Content-Length" header was not
+ * set. Otherwise, it compares it.
+ */
+ _isDataLengthValid: function () {
+ if (_headers['Content-Length']) {
+ return (_headers['Content-Length'].toString() === _data.length.toString());
+ }
+ 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;
+ }
+ };
+};
\ No newline at end of file
diff --git a/lib/mockWritableStream.js b/lib/mockWritableStream.js
index baaceb6..585b929 100644
--- a/lib/mockWritableStream.js
+++ b/lib/mockWritableStream.js
@@ -1,16 +1,16 @@
/*
* http://nodejs.org/api/stream.html#stream_writable_stream
-*/
+ */
-function WritableStream(){}
+function WritableStream() {}
-WritableStream.prototype.writable = function(){}
+WritableStream.prototype.writable = function () {};
// WritableStream.prototype.write = function(string, [encoding], [fd]){}
// WritableStream.prototype.write = function(buffer){}
-WritableStream.prototype.end = function(){}
+WritableStream.prototype.end = function () {};
// WritableStream.prototype.end = function(string, encoding){}
// WritableStream.prototype.end = function(buffer){}
-WritableStream.prototype.destroy = function(){}
-WritableStream.prototype.destroySoon = function(){}
+WritableStream.prototype.destroy = function () {};
+WritableStream.prototype.destroySoon = function () {};
-module.exports = WritableStream;
+module.exports = WritableStream;
\ 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