[Pkg-javascript-commits] [node-mocks-http] 219/296: chore(lint): fix lint errors

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:38 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 aae161637011e13488667e187dcf46c28dac11d8
Author: Johnny Estilles <johnny.estilles at agentia.asia>
Date:   Thu May 7 20:37:59 2015 +0800

    chore(lint): fix lint errors
---
 lib/mockRequest.js        |  470 +++++++++----------
 lib/mockResponse.js       | 1094 ++++++++++++++++++++++-----------------------
 lib/mockWritableStream.js |   10 +-
 3 files changed, 787 insertions(+), 787 deletions(-)

diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index 8a93677..2b0c9e3 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -32,264 +32,264 @@
 var url = require('url');
 
 function convertKeysToLowerCase(map) {
-    var newMap = {};
-    for(var key in map) {
-        newMap[key.toLowerCase()] = map[key];
-    }
-    return newMap;
+  var newMap = {};
+  for(var key in map) {
+    newMap[key.toLowerCase()] = map[key];
+  }
+  return newMap;
 }
 
 function createRequest(options) {
 
-    if (!options) {
-        options = {};
-    }
+  if (!options) {
+    options = {};
+  }
 
-    // creat mockRequest
+  // creat mockRequest
 
-    var mockRequest = {};
+  var mockRequest = {};
 
-    mockRequest.method = (options.method) ? options.method : 'GET';
-    mockRequest.url = (options.url) ? options.url : '';
-    mockRequest.originalUrl = options.originalUrl || mockRequest.url;
-    mockRequest.path = (options.url) ? url.parse(options.url).pathname : '';
-    mockRequest.params = (options.params) ? options.params : {};
-    if (options.session) {
-        mockRequest.session = options.session;
-    }
-    mockRequest.cookies = (options.cookies) ? options.cookies : {};
-    if (options.signedCookies) {
-        mockRequest.signedCookies = options.signedCookies;
-    }
-    mockRequest.headers = (options.headers) ? convertKeysToLowerCase(options.headers) : {};
-    mockRequest.body = (options.body) ? options.body : {};
-    mockRequest.query = (options.query) ? options.query : {};
-    mockRequest.files = (options.files) ? options.files : {};
+  mockRequest.method = (options.method) ? options.method : 'GET';
+  mockRequest.url = (options.url) ? options.url : '';
+  mockRequest.originalUrl = options.originalUrl || mockRequest.url;
+  mockRequest.path = (options.url) ? url.parse(options.url).pathname : '';
+  mockRequest.params = (options.params) ? options.params : {};
+  if (options.session) {
+    mockRequest.session = options.session;
+  }
+  mockRequest.cookies = (options.cookies) ? options.cookies : {};
+  if (options.signedCookies) {
+    mockRequest.signedCookies = options.signedCookies;
+  }
+  mockRequest.headers = (options.headers) ? convertKeysToLowerCase(options.headers) : {};
+  mockRequest.body = (options.body) ? options.body : {};
+  mockRequest.query = (options.query) ? options.query : {};
+  mockRequest.files = (options.files) ? options.files : {};
 
-    //parse query string from url to object
-    if (Object.keys(mockRequest.query).length === 0) {
-        mockRequest.query = require('querystring').parse(mockRequest.url.split('?')[1]);
-    }
+  //parse query string from url to object
+  if (Object.keys(mockRequest.query).length === 0) {
+    mockRequest.query = require('querystring').parse(mockRequest.url.split('?')[1]);
+  }
 
-    /**
-     * Return request header.
-     *
-     * The `Referrer` header field is special-cased,
-     * both `Referrer` and `Referer` are interchangeable.
-     *
-     * Examples:
-     *
-     *     mockRequest.get('Content-Type');
-     *     // => "text/plain"
-     *
-     *     mockRequest.get('content-type');
-     *     // => "text/plain"
-     *
-     *     mockRequest.get('Something');
-     *     // => undefined
-     *
-     * Aliased as `mockRequest.header()`.
-     *
-     * @param {String} name
-     * @return {String}
-     * @api public
-     */
+  /**
+   * Return request header.
+   *
+   * The `Referrer` header field is special-cased,
+   * both `Referrer` and `Referer` are interchangeable.
+   *
+   * Examples:
+   *
+   *     mockRequest.get('Content-Type');
+   *     // => "text/plain"
+   *
+   *     mockRequest.get('content-type');
+   *     // => "text/plain"
+   *
+   *     mockRequest.get('Something');
+   *     // => undefined
+   *
+   * Aliased as `mockRequest.header()`.
+   *
+   * @param {String} name
+   * @return {String}
+   * @api public
+   */
 
-    mockRequest.get =
-    mockRequest.header = function(name) {
-        name = name.toLowerCase();
-        switch (name) {
-            case 'referer':
-            case 'referrer':
-                return mockRequest.headers.referrer || mockRequest.headers.referer;
-            default:
-                return mockRequest.headers[name];
-        }
-    };
+  mockRequest.get =
+  mockRequest.header = function(name) {
+    name = name.toLowerCase();
+    switch (name) {
+      case 'referer':
+      case 'referrer':
+        return mockRequest.headers.referrer || mockRequest.headers.referer;
+      default:
+        return mockRequest.headers[name];
+    }
+  };
 
-    /**
-     * Function: param
-     *
-     *   Return the value of param name when present.
-     *   Lookup is performed in the following order:
-     *   - req.params
-     *   - req.body
-     *   - req.query
-     */
-    mockRequest.param = function(parameterName) {
-        if (mockRequest.params[parameterName]) {
-            return mockRequest.params[parameterName];
-        } else if (mockRequest.body[parameterName]) {
-            return mockRequest.body[parameterName];
-        } else if (mockRequest.query[parameterName]) {
-            return mockRequest.query[parameterName];
-        }
-        return null;
-    };
+  /**
+   * Function: param
+   *
+   *   Return the value of param name when present.
+   *   Lookup is performed in the following order:
+   *   - req.params
+   *   - req.body
+   *   - req.query
+   */
+  mockRequest.param = function(parameterName) {
+    if (mockRequest.params[parameterName]) {
+      return mockRequest.params[parameterName];
+    } else if (mockRequest.body[parameterName]) {
+      return mockRequest.body[parameterName];
+    } else if (mockRequest.query[parameterName]) {
+      return mockRequest.query[parameterName];
+    }
+    return null;
+  };
 
-    /**
-     * 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.
-     */
-    mockRequest._setParameter = function(key, value) {
-        mockRequest.params[key] = value;
-    };
+  /**
+   * 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.
+   */
+  mockRequest._setParameter = function(key, value) {
+    mockRequest.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
-     */
-    mockRequest._setSessionVariable = function(variable, value) {
-        if (typeof mockRequest.session !== 'object') {
-            mockRequest.session = {};
-        }
-        mockRequest.session[variable] = 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
+   */
+  mockRequest._setSessionVariable = function(variable, value) {
+    if (typeof mockRequest.session !== 'object') {
+      mockRequest.session = {};
+    }
+    mockRequest.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
-     */
-    mockRequest._setCookiesVariable = function(variable, value) {
-        mockRequest.cookies[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
+   */
+  mockRequest._setCookiesVariable = function(variable, value) {
+    mockRequest.cookies[variable] = value;
+  };
 
-    /**
-     * Sets a variable that is stored in the signed cookies.
-     *
-     * @param variable The variable to store in the signed cookies
-     * @param value    The value associated with the variable
-     */
-    mockRequest._setSignedCookiesVariable = function(variable, value) {
-        if (typeof mockRequest.signedCookies !== 'object') {
-            mockRequest.signedCookies = {};
-        }
-        mockRequest.signedCookies[variable] = value;
-    };
+  /**
+   * Sets a variable that is stored in the signed cookies.
+   *
+   * @param variable The variable to store in the signed cookies
+   * @param value    The value associated with the variable
+   */
+  mockRequest._setSignedCookiesVariable = function(variable, value) {
+    if (typeof mockRequest.signedCookies !== 'object') {
+      mockRequest.signedCookies = {};
+    }
+    mockRequest.signedCookies[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
-     */
-    mockRequest._setHeadersVariable = function(variable, value) {
-        variable = variable.toLowerCase();
-        mockRequest.headers[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
+   */
+  mockRequest._setHeadersVariable = function(variable, value) {
+    variable = variable.toLowerCase();
+    mockRequest.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
-     */
-    mockRequest._setFilesVariable = function(variable, value) {
-        mockRequest.files[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
+   */
+  mockRequest._setFilesVariable = function(variable, value) {
+    mockRequest.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.
-     */
-    mockRequest._setMethod = function(method) {
-        mockRequest.method = method;
-    };
+  /**
+   * 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.
+   */
+  mockRequest._setMethod = function(method) {
+    mockRequest.method = method;
+  };
 
-    /**
-     * Function: _setURL
-     *
-     *    Sets the URL value that the client gets when the called the 'url'
-     *    property.
-     *
-     * Parameters:
-     *
-     *   value - 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.
-     */
-    mockRequest._setURL = function(value) {
-        mockRequest.url = value;
-    };
+  /**
+   * Function: _setURL
+   *
+   *    Sets the URL value that the client gets when the called the 'url'
+   *    property.
+   *
+   * Parameters:
+   *
+   *   value - 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.
+   */
+  mockRequest._setURL = function(value) {
+    mockRequest.url = value;
+  };
 
-    /**
-     * Function: _setOriginalUrl
-     *
-     *    Sets the URL value that the client gets when the called the 'originalUrl'
-     *    property.
-     *
-     * Parameters:
-     *
-     *   value - 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.
-     */
-    mockRequest._setOriginalUrl = function(value) {
-        mockRequest.originalUrl = value;
-    };
+  /**
+   * Function: _setOriginalUrl
+   *
+   *    Sets the URL value that the client gets when the called the 'originalUrl'
+   *    property.
+   *
+   * Parameters:
+   *
+   *   value - 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.
+   */
+  mockRequest._setOriginalUrl = function(value) {
+    mockRequest.originalUrl = value;
+  };
 
-    /**
-     * 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.
-     */
-    mockRequest._setBody = function(body) {
-        mockRequest.body = body;
-    };
+  /**
+   * 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.
+   */
+  mockRequest._setBody = function(body) {
+    mockRequest.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.
-     */
-    mockRequest._addBody = function(key, value) {
-        mockRequest.body[key] = value;
-    };
+  /**
+   * 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.
+   */
+  mockRequest._addBody = function(key, value) {
+    mockRequest.body[key] = value;
+  };
 
-    return mockRequest;
+  return mockRequest;
 
 }
 
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index d288cc4..73215b2 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -32,602 +32,602 @@ var http = require('./node/http');
 
 function createResponse(options) {
 
-    if (!options) {
-        options = {};
+  if (!options) {
+    options = {};
+  }
+
+  var _endCalled = false;
+  var _data = '';
+  var _headers = {};
+  var _encoding = options.encoding;
+
+  var _redirectUrl = '';
+  var _renderView = '';
+  var _renderData = {};
+
+  if (options.writableStream) {
+    WritableStream = options.writableStream;
+  }
+  if (options.eventEmitter) {
+    EventEmitter = options.eventEmitter;
+  }
+  var writableStream = new WritableStream();
+  var eventEmitter = new EventEmitter();
+
+  // create mockResponse
+
+  var mockResponse = {};
+
+  mockResponse.statusCode = 200;
+  mockResponse.cookies = {};
+
+  mockResponse.cookie = function(name, value, opt) {
+
+    mockResponse.cookies[name] = {
+      value: value,
+      options: opt
+    };
+
+  };
+
+  mockResponse.clearCookie = function(name) {
+    delete mockResponse.cookies[name];
+  };
+
+  mockResponse.status = function(code) {
+    mockResponse.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.
+   */
+  mockResponse.writeHead = function(statusCode, phrase, headers) {
+
+    if (_endCalled) {
+      throw 'The end() method has already been called.';
     }
 
-    var _endCalled = false;
-    var _data = '';
-    var _headers = {};
-    var _encoding = options.encoding;
+    mockResponse.statusCode = statusCode;
 
-    var _redirectUrl = '';
-    var _renderView = '';
-    var _renderData = {};
-
-    if (options.writableStream) {
-        WritableStream = options.writableStream;
-    }
-    if (options.eventEmitter) {
-        EventEmitter = options.eventEmitter;
+    // Note: Not sure if the headers given in this function
+    //       overwrite any headers specified earlier.
+    if (headers) {
+      _headers = headers;
+    } else {
+      _headers = phrase;
     }
-    var writableStream = new WritableStream();
-    var eventEmitter = new EventEmitter();
-
-    // create mockResponse
-
-    var mockResponse = {};
 
-    mockResponse.statusCode = 200;
-    mockResponse.cookies = {};
+  };
 
-    mockResponse.cookie = function(name, value, opt) {
+  /**
+   *  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.
+   */
+  mockResponse.send = function(a, b, c) {
 
-        mockResponse.cookies[name] = {
-            value: value,
-            options: opt
-        };
-
-    };
-
-    mockResponse.clearCookie = function(name) {
-        delete mockResponse.cookies[name];
-    };
+    var _formatData = function(data) {
 
-    mockResponse.status = function(code) {
-        mockResponse.statusCode = code;
-        return this;
-    };
+      if (typeof data === 'object') {
 
-    /**
-     * 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.
-     */
-    mockResponse.writeHead = function(statusCode, phrase, headers) {
-
-        if (_endCalled) {
-            throw 'The end() method has already been called.';
+        if (data.statusCode) {
+          mockResponse.statusCode = data.statusCode;
+        } else if (data.httpCode) {
+          mockResponse.statusCode = data.statusCode;
         }
 
-        mockResponse.statusCode = statusCode;
-
-        // Note: Not sure if the headers given in this function
-        //       overwrite any headers specified earlier.
-        if (headers) {
-            _headers = headers;
+        if (data.body) {
+          _data = data.body;
         } else {
-            _headers = phrase;
+          _data = data;
         }
 
-    };
-
-    /**
-     *  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.
-     */
-    mockResponse.send = function(a, b, c) {
-
-        var _formatData = function(data) {
-
-            if (typeof data === 'object') {
-
-                if (data.statusCode) {
-                    mockResponse.statusCode = data.statusCode;
-                } else if (data.httpCode) {
-                    mockResponse.statusCode = data.statusCode;
-                }
-
-                if (data.body) {
-                    _data = data.body;
-                } else {
-                    _data = data;
-                }
-
-            } else {
-                _data += data;
-            }
-
-        };
-
-        switch (arguments.length) {
-
-            case 1:
-
-                if (typeof a === 'number') {
-                    mockResponse.statusCode = a;
-                } else {
-                    _formatData(a);
-                }
-
-                break;
-
-            case 2:
-
-                if (typeof a === 'number') {
-                    _formatData(b);
-                    mockResponse.statusCode = a;
-                } else if (typeof b === 'number') {
-                    _formatData(a);
-                    mockResponse.statusCode = b;
-                    console.warn('WARNING: Called send() with deprecated parameter order');
-                } else {
-                    _formatData(a);
-                    _encoding = b;
-                }
-
-                break;
-
-            case 3:
-
-                _formatData(a);
-                _headers = b;
-                mockResponse.statusCode = c;
-                console.warn('WARNING: Called send() with deprecated three parameters');
-
-                break;
-
-            default:
-                break;
-
-        }
-
-        mockResponse.emit('send');
-        mockResponse.emit('end');
-
-    };
-
-    /**
-     * Send given HTTP status code.
-     *
-     * Sets the response status to `statusCode` and the body of the
-     * response to the standard description from node's http.STATUS_CODES
-     * or the statusCode number if no description.
-     *
-     * Examples:
-     *
-     *     mockResponse.sendStatus(200);
-     *
-     * @param {number} statusCode
-     * @api public
-     */
-
-    mockResponse.sendStatus = function sendStatus(statusCode) {
-        var body = http.STATUS_CODES[statusCode] || String(statusCode);
-
-        mockResponse.statusCode = statusCode;
-        mockResponse.type('txt');
-
-        return mockResponse.send(body);
-    };
-
-
-    /**
-     * Function: json
-     *
-     *   The 'json' function from node's HTTP API that returns JSON
-     *   data to the client.
-     *
-     *  Parameters:
-     *
-     *   a - Either a statusCode or string containing JSON payload
-     *   b - Either a statusCode or string containing JSON payload
-     *
-     *  If not specified, the statusCode defaults to 200.
-     *  Second parameter is optional.
-     */
-    mockResponse.json = function(a, b) {
-
-        mockResponse.setHeader('Content-Type', 'application/json');
-        if (a) {
-            if (typeof a === 'number') {
-                mockResponse.statusCode = a;
-            } else {
-                _data += JSON.stringify(a);
-            }
-        }
-        if (b) {
-            if (typeof b === 'number') {
-                mockResponse.statusCode = b;
-            } else {
-                _data += JSON.stringify(b);
-            }
-        }
-    };
-
-    /**
-     * Set "Content-Type" response header with `type` through `mime.lookup()`
-     * when it does not contain "/", or set the Content-Type to `type` otherwise.
-     *
-     * Examples:
-     *
-     *     res.type('.html');
-     *     res.type('html');
-     *     res.type('json');
-     *     res.type('application/json');
-     *     res.type('png');
-     *
-     * @param {String} type
-     * @return {ServerResponse} for chaining
-     * @api public
-     */
-    mockResponse.contentType = mockResponse.type = function(type){
-        return mockResponse.set('Content-Type', type.indexOf('/') >= 0 ? type : mime.lookup(type));
-    };
-
-    /**
-     * 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.
-     */
-
-    mockResponse.write = function(data, encoding) {
-
+      } else {
         _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.
-     */
-    mockResponse.end = function(data, encoding) {
-
-        _endCalled = true;
-
-        if (data) {
-            _data += data;
-        }
-
-        if (encoding) {
-            _encoding = encoding;
-        }
-
-        mockResponse.emit('end');
+    switch (arguments.length) {
 
-    };
+      case 1:
 
-   /**
-    * Set header `field` to `val`, or pass
-    * an object of header fields.
-    *
-    * Examples:
-    *
-    *    res.set('Foo', ['bar', 'baz']);
-    *    res.set('Accept', 'application/json');
-    *    res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
-    *
-    * Aliased as `mockResponse.header()`.
-    *
-    * @param {String|Object|Array} field
-    * @param {String} val
-    * @return {ServerResponse} for chaining
-    * @api public
-    */
-    mockResponse.set = mockResponse.header = function header(field, val) {
-        if (arguments.length === 2) {
-            if (Array.isArray(val)) {
-                val = val.map(String);
-            } else {
-                val = String(val);
-            }
-            mockResponse.setHeader(field, val);
+        if (typeof a === 'number') {
+          mockResponse.statusCode = a;
         } else {
-            for (var key in field) {
-                mockResponse.setHeader(key, field[key]);
-            }
+          _formatData(a);
         }
-        return mockResponse;
-    };
 
-    /**
-     * Function: getHeader
-     * Function: get
-     *
-     *   Returns a particular header by name.
-     */
-    mockResponse.get = mockResponse.getHeader = function(name) {
-        return _headers[name];
-    };
-
-    /**
-     * Function: setHeader
-     * Function: set
-     *
-     *   Set a particular header by name.
-     */
-    mockResponse.setHeader = function(name, value) {
-        _headers[name] = value;
-        return value;
-    };
-
-    /**
-     * Function: removeHeader
-     *
-     *   Removes an HTTP header by name.
-     */
-    mockResponse.removeHeader = function(name) {
-        delete _headers[name];
-    };
-
-    /**
-     * Function: setEncoding
-     *
-     *    Sets the encoding for the data. Generally 'utf8'.
-     *
-     * Parameters:
-     *
-     *   encoding - The string representing the encoding value.
-     */
-    mockResponse.setEncoding = function(encoding) {
-        _encoding = encoding;
-    };
+        break;
 
-    /**
-     * Function: redirect
-     *
-     *     Redirect to a url with response code
-     */
-    mockResponse.redirect = function(a, b) {
-
-        switch (arguments.length) {
-
-            case 1:
-
-                mockResponse.statusCode = 302;
-                _redirectUrl = a;
-                break;
-
-            case 2:
-
-                if (typeof a === 'number') {
-                    mockResponse.statusCode = a;
-                    _redirectUrl = b;
-                }
-
-                break;
-
-            default:
-                break;
-
-        }
-
-    };
-
-    /**
-     * Function: render
-     *
-     *     Render a view with a callback responding with the
-     *     rendered string.
-     */
-    mockResponse.render = function(a, b) {
-
-        _renderView = a;
-
-        switch (arguments.length) {
-
-            case 2:
-                _renderData = b;
-                break;
-
-            default:
-                break;
+      case 2:
 
+        if (typeof a === 'number') {
+          _formatData(b);
+          mockResponse.statusCode = a;
+        } else if (typeof b === 'number') {
+          _formatData(a);
+          mockResponse.statusCode = b;
+          console.warn('WARNING: Called send() with deprecated parameter order');
+        } else {
+          _formatData(a);
+          _encoding = b;
         }
 
-        mockResponse.emit('render');
-        mockResponse.emit('end');
-
-    };
-
-    // WritableStream.writable is not a function
-    // mockResponse.writable = function() {
-    //     return writableStream.writable.apply(this, arguments);
-    // };
-
-    // mockResponse.end = function(){
-    //  return writableStream.end.apply(this, arguments);
-    // };
+        break;
 
-    mockResponse.destroy = function() {
-        return writableStream.destroy.apply(this, arguments);
-    };
+      case 3:
 
-    mockResponse.destroySoon = function() {
-        return writableStream.destroySoon.apply(this, arguments);
-    };
+        _formatData(a);
+        _headers = b;
+        mockResponse.statusCode = c;
+        console.warn('WARNING: Called send() with deprecated three parameters');
 
-    mockResponse.addListener = function() {
-        return eventEmitter.addListener.apply(this, arguments);
-    };
-
-    mockResponse.on = function() {
-        return eventEmitter.on.apply(this, arguments);
-    };
-
-    mockResponse.once = function() {
-        return eventEmitter.once.apply(this, arguments);
-    };
-
-    mockResponse.removeListener = function() {
-        return eventEmitter.removeListener.apply(this, arguments);
-    };
+        break;
 
-    mockResponse.removeAllListeners = function() {
-        return eventEmitter.removeAllListeners.apply(this, arguments);
-    };
+      default:
+        break;
 
-    mockResponse.setMaxListeners = function() {
-        return eventEmitter.setMaxListeners.apply(this, arguments);
-    };
+    }
 
-    mockResponse.listeners = function() {
-        return eventEmitter.listeners.apply(this, arguments);
-    };
+    mockResponse.emit('send');
+    mockResponse.emit('end');
+
+  };
+
+  /**
+   * Send given HTTP status code.
+   *
+   * Sets the response status to `statusCode` and the body of the
+   * response to the standard description from node's http.STATUS_CODES
+   * or the statusCode number if no description.
+   *
+   * Examples:
+   *
+   *     mockResponse.sendStatus(200);
+   *
+   * @param {number} statusCode
+   * @api public
+   */
+
+  mockResponse.sendStatus = function sendStatus(statusCode) {
+    var body = http.STATUS_CODES[statusCode] || String(statusCode);
+
+    mockResponse.statusCode = statusCode;
+    mockResponse.type('txt');
+
+    return mockResponse.send(body);
+  };
+
+
+  /**
+   * Function: json
+   *
+   *   The 'json' function from node's HTTP API that returns JSON
+   *   data to the client.
+   *
+   *  Parameters:
+   *
+   *   a - Either a statusCode or string containing JSON payload
+   *   b - Either a statusCode or string containing JSON payload
+   *
+   *  If not specified, the statusCode defaults to 200.
+   *  Second parameter is optional.
+   */
+  mockResponse.json = function(a, b) {
+
+    mockResponse.setHeader('Content-Type', 'application/json');
+    if (a) {
+      if (typeof a === 'number') {
+        mockResponse.statusCode = a;
+      } else {
+        _data += JSON.stringify(a);
+      }
+    }
+    if (b) {
+      if (typeof b === 'number') {
+        mockResponse.statusCode = b;
+      } else {
+        _data += JSON.stringify(b);
+      }
+    }
+  };
+
+  /**
+   * Set "Content-Type" response header with `type` through `mime.lookup()`
+   * when it does not contain "/", or set the Content-Type to `type` otherwise.
+   *
+   * Examples:
+   *
+   *     res.type('.html');
+   *     res.type('html');
+   *     res.type('json');
+   *     res.type('application/json');
+   *     res.type('png');
+   *
+   * @param {String} type
+   * @return {ServerResponse} for chaining
+   * @api public
+   */
+  mockResponse.contentType = mockResponse.type = function(type){
+    return mockResponse.set('Content-Type', type.indexOf('/') >= 0 ? type : mime.lookup(type));
+  };
+
+  /**
+   * 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.
+   */
+
+  mockResponse.write = function(data, encoding) {
+
+    _data += data;
+
+    if (encoding) {
+      _encoding = encoding;
+    }
 
-    mockResponse.emit = function() {
-        return eventEmitter.emit.apply(this, arguments);
-    };
+  };
+
+  /**
+   *  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.
+   */
+  mockResponse.end = function(data, encoding) {
+
+    _endCalled = true;
+
+    if (data) {
+      _data += data;
+    }
 
-    //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.
-     */
-    mockResponse._isEndCalled = function() {
-        return _endCalled;
-    };
+    if (encoding) {
+      _encoding = encoding;
+    }
 
-    /**
-     * Function: _getHeaders
-     *
-     *  Returns all the headers that were set. This may be an
-     *  empty object, but probably will have "Content-Type" set.
-     */
-    mockResponse._getHeaders = function() {
-        return _headers;
-    };
+    mockResponse.emit('end');
+
+  };
+
+ /**
+  * Set header `field` to `val`, or pass
+  * an object of header fields.
+  *
+  * Examples:
+  *
+  *    res.set('Foo', ['bar', 'baz']);
+  *    res.set('Accept', 'application/json');
+  *    res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
+  *
+  * Aliased as `mockResponse.header()`.
+  *
+  * @param {String|Object|Array} field
+  * @param {String} val
+  * @return {ServerResponse} for chaining
+  * @api public
+  */
+  mockResponse.set = mockResponse.header = function header(field, val) {
+    if (arguments.length === 2) {
+      if (Array.isArray(val)) {
+        val = val.map(String);
+      } else {
+        val = String(val);
+      }
+      mockResponse.setHeader(field, val);
+    } else {
+      for (var key in field) {
+        mockResponse.setHeader(key, field[key]);
+      }
+    }
+    return mockResponse;
+  };
+
+  /**
+   * Function: getHeader
+   * Function: get
+   *
+   *   Returns a particular header by name.
+   */
+  mockResponse.get = mockResponse.getHeader = function(name) {
+    return _headers[name];
+  };
+
+  /**
+   * Function: setHeader
+   * Function: set
+   *
+   *   Set a particular header by name.
+   */
+  mockResponse.setHeader = function(name, value) {
+    _headers[name] = value;
+    return value;
+  };
+
+  /**
+   * Function: removeHeader
+   *
+   *   Removes an HTTP header by name.
+   */
+  mockResponse.removeHeader = function(name) {
+    delete _headers[name];
+  };
+
+  /**
+   * Function: setEncoding
+   *
+   *    Sets the encoding for the data. Generally 'utf8'.
+   *
+   * Parameters:
+   *
+   *   encoding - The string representing the encoding value.
+   */
+  mockResponse.setEncoding = function(encoding) {
+    _encoding = encoding;
+  };
+
+  /**
+   * Function: redirect
+   *
+   *     Redirect to a url with response code
+   */
+  mockResponse.redirect = function(a, b) {
+
+    switch (arguments.length) {
+
+      case 1:
+
+        mockResponse.statusCode = 302;
+        _redirectUrl = a;
+        break;
+
+      case 2:
+
+        if (typeof a === 'number') {
+          mockResponse.statusCode = a;
+          _redirectUrl = b;
+        }
 
-    /**
-     * Function: _getData
-     *
-     *  The data sent to the user.
-     */
-    mockResponse._getData = function() {
-        return _data;
-    };
+        break;
 
-    /**
-     * Function: _getStatusCode
-     *
-     *  The status code that was sent to the user.
-     */
-    mockResponse._getStatusCode = function() {
-        return mockResponse.statusCode;
-    };
+      default:
+        break;
 
-    /**
-     * Function: _isJSON
-     *
-     *  Returns true if the data sent was defined as JSON.
-     *  It doesn't validate the data that was sent.
-     */
-    mockResponse._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".
-     */
-    mockResponse._isUTF8 = function() {
-
-        if (!_encoding) {
-            return false;
-        }
+  };
 
-        return (_encoding === 'utf8');
+  /**
+   * Function: render
+   *
+   *     Render a view with a callback responding with the
+   *     rendered string.
+   */
+  mockResponse.render = function(a, b) {
 
-    };
+    _renderView = a;
 
-    /**
-     * 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.
-     */
-    mockResponse._isDataLengthValid = function() {
-
-        if (_headers['Content-Length']) {
-            return (_headers['Content-Length'].toString() === _data.length.toString());
-        }
+    switch (arguments.length) {
 
-        return true;
+      case 2:
+        _renderData = b;
+        break;
 
-    };
+      default:
+        break;
 
-    /**
-     * Function: _getRedirectUrl
-     *
-     *     Return redirect url of redirect method
-     *
-     * Returns:
-     *
-     *     Redirect url
-     */
-    mockResponse._getRedirectUrl = function() {
-        return _redirectUrl;
-    };
+    }
 
-    /**
-     * Function: _getRenderView
-     *
-     *     Return render view of render method
-     *
-     * Returns:
-     *
-     *     render view
-     */
-    mockResponse._getRenderView = function() {
-        return _renderView;
-    };
+    mockResponse.emit('render');
+    mockResponse.emit('end');
+
+  };
+
+  // WritableStream.writable is not a function
+  // mockResponse.writable = function() {
+  //     return writableStream.writable.apply(this, arguments);
+  // };
+
+  // mockResponse.end = function(){
+  //  return writableStream.end.apply(this, arguments);
+  // };
+
+  mockResponse.destroy = function() {
+    return writableStream.destroy.apply(this, arguments);
+  };
+
+  mockResponse.destroySoon = function() {
+    return writableStream.destroySoon.apply(this, arguments);
+  };
+
+  mockResponse.addListener = function() {
+    return eventEmitter.addListener.apply(this, arguments);
+  };
+
+  mockResponse.on = function() {
+    return eventEmitter.on.apply(this, arguments);
+  };
+
+  mockResponse.once = function() {
+    return eventEmitter.once.apply(this, arguments);
+  };
+
+  mockResponse.removeListener = function() {
+    return eventEmitter.removeListener.apply(this, arguments);
+  };
+
+  mockResponse.removeAllListeners = function() {
+    return eventEmitter.removeAllListeners.apply(this, arguments);
+  };
+
+  mockResponse.setMaxListeners = function() {
+    return eventEmitter.setMaxListeners.apply(this, arguments);
+  };
+
+  mockResponse.listeners = function() {
+    return eventEmitter.listeners.apply(this, arguments);
+  };
+
+  mockResponse.emit = function() {
+    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.
+   */
+  mockResponse._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.
+   */
+  mockResponse._getHeaders = function() {
+    return _headers;
+  };
+
+  /**
+   * Function: _getData
+   *
+   *  The data sent to the user.
+   */
+  mockResponse._getData = function() {
+    return _data;
+  };
+
+  /**
+   * Function: _getStatusCode
+   *
+   *  The status code that was sent to the user.
+   */
+  mockResponse._getStatusCode = function() {
+    return mockResponse.statusCode;
+  };
+
+  /**
+   * Function: _isJSON
+   *
+   *  Returns true if the data sent was defined as JSON.
+   *  It doesn't validate the data that was sent.
+   */
+  mockResponse._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".
+   */
+  mockResponse._isUTF8 = function() {
+
+    if (!_encoding) {
+      return false;
+    }
 
-    /**
-     * Function: _getRenderData
-     *
-     *     Return render data of render method
-     *
-     * Returns:
-     *
-     *     render data
-     */
-    mockResponse._getRenderData = function() {
-        return _renderData;
-    };
+    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.
+   */
+  mockResponse._isDataLengthValid = function() {
+
+    if (_headers['Content-Length']) {
+      return (_headers['Content-Length'].toString() === _data.length.toString());
+    }
 
-    return mockResponse;
+    return true;
+
+  };
+
+  /**
+   * Function: _getRedirectUrl
+   *
+   *     Return redirect url of redirect method
+   *
+   * Returns:
+   *
+   *     Redirect url
+   */
+  mockResponse._getRedirectUrl = function() {
+    return _redirectUrl;
+  };
+
+  /**
+   * Function: _getRenderView
+   *
+   *     Return render view of render method
+   *
+   * Returns:
+   *
+   *     render view
+   */
+  mockResponse._getRenderView = function() {
+    return _renderView;
+  };
+
+  /**
+   * Function: _getRenderData
+   *
+   *     Return render data of render method
+   *
+   * Returns:
+   *
+   *     render data
+   */
+  mockResponse._getRenderData = function() {
+    return _renderData;
+  };
+
+  return mockResponse;
 
 }
 
diff --git a/lib/mockWritableStream.js b/lib/mockWritableStream.js
index 7d82644..e6de0f0 100644
--- a/lib/mockWritableStream.js
+++ b/lib/mockWritableStream.js
@@ -7,11 +7,11 @@
 function WritableStream() {}
 
 Object.defineProperty(WritableStream, 'writable', {
-    configurable: true,
-    enumerable: true,
-    get: function() {
-        return true;
-    }
+  configurable: true,
+  enumerable: true,
+  get: function() {
+    return true;
+  }
 });
 
 // WritableStream.prototype.write = function(string, [encoding], [fd]){}

-- 
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