[Pkg-javascript-commits] [node-mocks-http] 09/296: Sometimes the prototype code would run and bubble all the way up. Now it is more reliable.
Thorsten Alteholz
alteholz at moszumanska.debian.org
Mon Feb 8 18:13:16 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 4b5fce99079e9a113c8507450ccee9db9da2024d
Author: Howard Abrams <howard at cloud-eco.com>
Date: Sun Feb 19 10:39:08 2012 -0800
Sometimes the prototype code would run and bubble all the way up. Now it is more reliable.
---
lib/mockResponse.js | 436 ++++++++++++++++++++++++++--------------------------
1 file changed, 220 insertions(+), 216 deletions(-)
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 59631d4..b7f8559 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -8,245 +8,249 @@
* @author Howard Abrams <howard.abrams at gmail.com>
*/
-Provider = function() {
- this._endCalled = false;
- this._data = "";
- this.statusCode = -1;
- this._headers = {};
-};
/**
- * Function: writeHead
- *
- * The 'writeHead' function from node's HTTP API.
- *
- * Parameters:
+ * Function: createResponse
*
- * statusCode - A number to send as a the HTTP status
- * headers - An object of properties that will be used for
- * the HTTP headers.
- */
-Provider.prototype.writeHead = function( statusCode, phrase, headers ) {
- if (this._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) {
- this._reasonPhrase = phrase;
- this._headers = headers;
- }
- else {
- this._headers = phrase;
- }
-};
-
-/**
- * Function: send
+ * Creates a new mock 'response' instance. All values are reset to the
+ * defaults.
*
- * The 'send' function from node's HTTP API that returns data
- * to the client. Can be called multiple times.
- *
* Parameters:
*
- * data - The data to return. Must be a string. Appended to
- * previous calls to data.
- * encoding - Optional encoding value.
- */
-Provider.prototype.send = function( data, encoding ) {
- this._data += data;
- if (encoding) {
- this._encoding = encoding;
- }
-};
-
-
-/**
- * Function: write
- *
- * This function has the same behavior as the 'send' function.
+ * options - An object of named parameters.
*
- * Parameters:
+ * Options:
*
- * data - The data to return. Must be a string. Appended to
- * previous calls to data.
- * encoding - Optional encoding value.
*/
-Provider.prototype.write = function( data, encoding ) {
- this.send(data, 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.
- */
-Provider.prototype.end = function( data, encoding ) {
- this._endCalled = true;
- if (data) {
- this._data += data;
- }
- if (encoding) {
- this._encoding = encoding;
+exports.createResponse = function(options) {
+ if (!options) {
+ options = {};
}
-};
-
-
-/**
- * Function: getHeader
- *
- * Returns a particular header by name.
- */
-Provider.prototype.getHeader = function(name) {
- return this._headers[name];
-};
-
-/**
- * Function: setHeader
- *
- * Set a particular header by name.
- */
-Provider.prototype.setHeader = function(name, value) {
- return this._headers[name] = value;
-};
-
-/**
- * Function: removeHeader
- *
- * Removes an HTTP header by name.
- */
-Provider.prototype.removeHeader = function(name) {
- delete this._headers[name];
-};
-/**
- * Function: setEncoding
- *
- * Sets the encoding for the data. Generally 'utf8'.
- *
- * Parameters:
- *
- * encoding - The string representing the encoding value.
- */
-Provider.prototype.setEncoding = function(encoding) {
- this._encoding = encoding;
-};
+ return {
+ _endCalled: false,
+ _data: "",
+ statusCode: -1,
+ _headers: {},
+ /**
+ * 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 (this._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) {
+ this._reasonPhrase = phrase;
+ this._headers = headers;
+ }
+ else {
+ this._headers = phrase;
+ }
+ },
+
+ /**
+ * Function: send
+ *
+ * The 'send' function from node's HTTP API that returns data
+ * to the client. Can be called multiple times.
+ *
+ * Parameters:
+ *
+ * data - The data to return. Must be a string. Appended to
+ * previous calls to data.
+ * encoding - Optional encoding value.
+ */
+ send: function( data, encoding ) {
+ this._data += data;
+ if (encoding) {
+ this._encoding = encoding;
+ }
+ },
+
+
+ /**
+ * 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 ) {
+ this.send(data, encoding);
+ },
-//This mock object stores some state as well
-//as some test-analysis functions:
+ /**
+ * 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 ) {
+ this._endCalled = true;
+ if (data) {
+ this._data += data;
+ }
+ if (encoding) {
+ this._encoding = encoding;
+ }
+ },
-/**
-* Function: _isEndCalled
-*
-* Since the <end> function must be called, this function
-* returns true if it has been called. False otherwise.
-*/
-Provider.prototype._isEndCalled = function() {
- return this._endCalled;
-};
+ /**
+ * Function: getHeader
+ *
+ * Returns a particular header by name.
+ */
+ getHeader: function(name) {
+ return this._headers[name];
+ },
-/**
- * Function: _getHeaders
- *
- * Returns all the headers that were set. This may be an
- * empty object, but probably will have "Content-Type" set.
- */
-Provider.prototype._getHeaders = function() {
- return this._headers;
-};
+ /**
+ * Function: setHeader
+ *
+ * Set a particular header by name.
+ */
+ setHeader: function(name, value) {
+ return this._headers[name] = value;
+ },
+ /**
+ * Function: removeHeader
+ *
+ * Removes an HTTP header by name.
+ */
+ removeHeader: function(name) {
+ delete this._headers[name];
+ },
+ /**
+ * Function: setEncoding
+ *
+ * Sets the encoding for the data. Generally 'utf8'.
+ *
+ * Parameters:
+ *
+ * encoding - The string representing the encoding value.
+ */
+ setEncoding: function(encoding) {
+ this._encoding = encoding;
+ },
+
+
+ //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 this._endCalled;
+ },
-/**
- * Function: _getData
- *
- * The data sent to the user.
- */
-Provider.prototype._getData = function() {
- return this._data;
-};
+
+ /**
+ * 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 this._headers;
+ },
-/**
- * Function: _getStatusCode
- *
- * The status code that was sent to the user.
- */
-Provider.prototype._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.
- */
-Provider.prototype._isJSON = function() {
- return (this._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".
- */
-Provider.prototype._isUTF8 = function() {
- if ( !this._encoding ) {
- return false;
- }
- return ( this._encoding === "utf8" );
-};
+ /**
+ * Function: _getData
+ *
+ * The data sent to the user.
+ */
+ _getData: function() {
+ return this._data;
+ },
-/**
- * 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.
- */
-Provider.prototype._isDataLengthValid = function() {
- if (this._headers["Content-Length"]) {
- return (this._headers["Content-Length"] == this._data.length);
- }
- return true;
-};
+ /**
+ * Function: _getStatusCode
+ *
+ * The status code that was sent to the user.
+ */
+ _getStatusCode: function() {
+ return this.statusCode;
+ },
-/**
- * Function: createResponse
- *
- * Creates a new mock 'response' instance. All values are reset to the
- * defaults.
- *
- * Parameters:
- *
- * options - An object of named parameters.
- *
- * Options:
- *
- */
+ /**
+ * Function: _isJSON
+ *
+ * Returns true if the data sent was defined as JSON.
+ * It doesn't validate the data that was sent.
+ */
+ _isJSON: function() {
+ return (this._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 ( !this._encoding ) {
+ return false;
+ }
+ return ( this._encoding === "utf8" );
+ },
-exports.createResponse = function(options) {
- return new Provider(options);
-};
+ /**
+ * 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 (this._headers["Content-Length"]) {
+ return (this._headers["Content-Length"] == this._data.length);
+ }
+ return true;
+ }
+ };
+};
\ 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