[Pkg-javascript-commits] [node-mocks-http] 50/296: Reorganizes code to remove potentally ambiguous reference to the this variable and allow for aliases to be created.

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:20 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 a21c6b8a65ad2e20cdb798402f464cc32fe1f43c
Author: Alan James <alanjames1987 at gmail.com>
Date:   Thu Sep 25 17:22:42 2014 -0400

    Reorganizes code to remove potentally ambiguous reference to the this variable and allow for aliases to be created.
---
 lib/mockRequest.js  | 314 +++++++++---------
 lib/mockResponse.js | 935 ++++++++++++++++++++++++++--------------------------
 2 files changed, 627 insertions(+), 622 deletions(-)

diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index d470ba8..0a49816 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -34,164 +34,164 @@ function createRequest(options) {
         options = {};
     }
 
-    var mockRequest = {
-
-        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: header
-         *
-         *   Returns a particular header by name.
-         */
-        header: function(name) {
-            return this.headers[name];
-        },
-
-        /**
-         * Function: get
-         *
-         *   An copy of header.
-         */
-        get: function(name) {
-            return this.headers[name];
-        },
-
-        /**
-         * 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;
-        }
+    // creat mockRequest
+
+    var mockRequest = {};
+
+    mockRequest.method = (options.method) ? options.method : 'GET';
+    mockRequest.url = (options.url) ? options.url : '';
+    mockRequest.params = (options.params) ? options.params : {};
+    mockRequest.session = (options.session) ? options.session : {};
+    mockRequest.cookies = (options.cookies) ? options.cookies : {};
+    mockRequest.headers = (options.headers) ? options.headers : {};
+    mockRequest.body = (options.body) ? options.body : {};
+    mockRequest.query = (options.query) ? options.query : {};
+    mockRequest.files = (options.files) ? options.files : {};
+
+    /**
+     * Function: header
+     *
+     *   Returns a particular header by name.
+     */
+    mockRequest.header = function(name) {
+        return mockRequest.headers[name];
+    };
+
+    /**
+     * Function: get
+     *
+     *   An copy of header.
+     */
+    mockRequest.get = function(name) {
+        return mockRequest.headers[name];
+    };
+
+    /**
+     * 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) {
+        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 headers.
+     *
+     * @param variable The variable to store in the headers
+     * @param value    The value associated with the variable
+     */
+    mockRequest._setHeadersVariable = function(variable, value) {
+        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;
+    };
+
+    /**
+     * 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:
+     *
+     *   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.
+     */
+    mockRequest._setURL = function(url) {
+        mockRequest.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.
+     */
+    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;
     };
 
     return mockRequest;
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index fa30d43..d1ba0b0 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -46,512 +46,517 @@ function createResponse(options) {
     var writableStream = options.writableStream ? new options.writableStream() : new WritableStream();
     var eventEmitter = options.eventEmitter ? new options.eventEmitter() : new EventEmitter();
 
-    var mockResponse = {
-
-        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.';
-            }
+    // create mockResponse
+
+    var mockResponse = {};
+
+    mockResponse.statusCode = -1;
+    mockResponse.cookies = {};
+
+    mockResponse.cookie = function(name, value, options) {
+
+        mockResponse.cookies[name] = {
+            value: value,
+            options: options
+        };
+
+    };
+
+    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.';
+        }
+
+        mockResponse.statusCode = statusCode;
+
+        // Note: Not sure if the headers given in this function
+        //       overwrite any headers specified earlier.
+        if (headers) {
+            _headers = headers;
+        } else {
+            _headers = phrase;
+        }
+
+    };
 
-            this.statusCode = statusCode;
+    /**
+     *  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(a) {
+
+            if (typeof a === 'object') {
+
+                if (a.statusCode) {
+                    mockResponse.statusCode = a.statusCode;
+                } else if (a.httpCode) {
+                    mockResponse.statusCode = a.statusCode;
+                }
+
+                if (a.body) {
+                    _data = a.body;
+                } else {
+                    _data = a;
+                }
 
-            // Note: Not sure if the headers given in this function
-            //       overwrite any headers specified earlier.
-            if (headers) {
-                _headers = headers;
             } else {
-                _headers = phrase;
+                _data += a;
             }
 
-        },
-
-        /**
-         *  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) {
-
-            var _self = this;
-
-            var _formatData = function(a) {
-                if (typeof a === 'object') {
-                    if (a.statusCode) {
-                        _self.statusCode = a.statusCode;
-                    } else if (a.httpCode) {
-                        _self.statusCode = a.statusCode;
-                    }
-                    if (a.body) {
-                        _data = a.body;
-                    } else {
-                        _data = a;
-                    }
+        };
+
+        switch (arguments.length) {
+
+            case 1:
+
+                if (typeof a === 'number') {
+                    mockResponse.statusCode = a;
                 } else {
-                    _data += a;
+                    _formatData(a);
                 }
-            };
-
-            switch (arguments.length) {
-                case 1:
-                    if (typeof a === 'number') {
-                        this.statusCode = a;
-                    } else {
-                        _formatData(a);
-                    }
-                    break;
-
-                case 2:
-                    if (typeof a === 'number') {
-                        _formatData(b);
-                        this.statusCode = a;
-                    } else if (typeof b === 'number') {
-                        _formatData(a);
-                        this.statusCode = b;
-                        console.warn('WARNING: Called send() with deprecated parameter order');
-                    } else {
-                        _formatData(a);
-                        _encoding = b;
-                    }
-                    break;
-
-                case 3:
+
+                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);
-                    _headers = b;
-                    this.statusCode = c;
-                    console.warn('WARNING: Called send() with deprecated three parameters');
-                    break;
+                    _encoding = b;
+                }
 
-                default:
-                    break;
-            }
+                break;
 
-            this.emit('send');
-            this.emit('end');
+            case 3:
 
-        },
+                _formatData(a);
+                _headers = b;
+                mockResponse.statusCode = c;
+                console.warn('WARNING: Called send() with deprecated three parameters');
 
-        /**
-         *  The 'json' function from node's HTTP API that returns JSON data
-         *  to the client.  Should not be called multiple times.
-         */
-        json: function(a, b) {
+                break;
 
-            this.setHeader('Content-Type', 'application/json');
+            default:
+                break;
 
-            switch (arguments.length) {
-                case 1:
-                    if (typeof a === 'number') {
-                        this.statusCode = a;
-                    } else {
-                        _data += JSON.stringify(a);
-                        this.statusCode = 200;
-                    }
-                    break;
+        }
 
-                case 2:
-                    this.statusCode = a;
-                    _data += JSON.stringify(b);
+        mockResponse.emit('send');
+        mockResponse.emit('end');
 
-                    break;
+    };
 
-                default:
-                    break;
-            }
+    /**
+     *  The 'json' function from node's HTTP API that returns JSON data
+     *  to the client.  Should not be called multiple times.
+     */
+    mockResponse.json = function(a, b) {
 
-        },
+        mockResponse.setHeader('Content-Type', 'application/json');
 
-        /**
-         * 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.
-         */
+        switch (arguments.length) {
 
-        write: function(data, encoding) {
+            case 1:
 
-            _data += data;
+                if (typeof a === 'number') {
+                    mockResponse.statusCode = a;
+                } else {
+                    _data += JSON.stringify(a);
+                    mockResponse.statusCode = 200;
+                }
 
-            if (encoding) {
-                _encoding = encoding;
-            }
+                break;
 
-        },
-
-        /**
-         *  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;
-            }
+            case 2:
 
-            if (encoding) {
-                _encoding = encoding;
-            }
+                mockResponse.statusCode = a;
+                _data += JSON.stringify(b);
 
-            this.emit('end');
+                break;
 
-        },
+            default:
+                break;
+        }
 
-        /**
-         * Function: header
-         *
-         *   An alias of either getHeader or setHeader depending on
-         *   the amount of passed parameters.
-         */
-        header: function(name, value) {
+    };
 
-            if (typeof value !== 'undefined') {
-                return this.setHeader(name, value);
-            } else {
-                return this.getHeader(name);
-            }
+    /**
+     * 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.
+     */
 
-        },
-
-        /**
-         * Function: getHeader
-         *
-         *   Returns a particular header by name.
-         */
-        getHeader: function(name) {
-            return _headers[name];
-        },
-
-        /**
-         * Function: get
-         *
-         *   An copy of getHeader.
-         */
-        get: function(name) {
-            return _headers[name];
-        },
-
-        /**
-         * Function: setHeader
-         *
-         *   Set a particular header by name.
-         */
-        setHeader: function(name, value) {
-            _headers[name] = value;
-            return value;
-        },
-
-        /**
-         * Function: set
-         *
-         *   An copy of setHeader.
-         */
-        set: 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) {
+    mockResponse.write = function(data, encoding) {
+
+        _data += data;
+
+        if (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) {
+    /**
+     *  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;
+        }
 
-            _renderView = a;
+        if (encoding) {
+            _encoding = encoding;
+        }
 
-            switch (arguments.length) {
-                case 2:
-                    break;
+        mockResponse.emit('end');
 
-                case 3:
-                    _renderData = b;
-                    break;
+    };
 
-                default:
-                    break;
-            }
+    /**
+     * Function: header
+     *
+     *   An alias of either getHeader or setHeader depending on
+     *   the amount of passed parameters.
+     */
+    mockResponse.header = function(name, value) {
+
+        if (typeof value !== 'undefined') {
+            return mockResponse.setHeader(name, value);
+        } else {
+            return mockResponse.getHeader(name);
+        }
 
-            this.emit('render');
-            this.emit('end');
-
-        },
-
-        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());
-            }
+    /**
+     * 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.set = 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:
+
+                _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, c) {
+
+        _renderView = a;
+
+        switch (arguments.length) {
+
+            case 2:
+                break;
+
+            case 3:
+                _renderData = b;
+                break;
+
+            default:
+                break;
+
+        }
+
+        mockResponse.emit('render');
+        mockResponse.emit('end');
+
+    };
+
+    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(event, listener) {
+        return eventEmitter.addListener.apply(this, arguments);
+    };
+
+    mockResponse.on = function(event, listener) {
+        return eventEmitter.on.apply(this, arguments);
+    };
+
+    mockResponse.once = function(event, listener) {
+        return eventEmitter.once.apply(this, arguments);
+    };
+
+    mockResponse.removeListener = function(event, listener) {
+        return eventEmitter.removeListener.apply(this, arguments);
+    };
+
+    mockResponse.removeAllListeners = function(event) {
+        return eventEmitter.removeAllListeners.apply(this, arguments);
+    };
+
+    mockResponse.setMaxListeners = function(n) {
+        return eventEmitter.setMaxListeners.apply(this, arguments);
+    };
 
-            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;
+    mockResponse.listeners = function(event) {
+        return eventEmitter.listeners.apply(this, arguments);
+    };
+
+    mockResponse.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.
+     */
+    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;
         }
 
+        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 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;

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