Socket
Socket
Sign inDemoInstall

node-mocks-http

Package Overview
Dependencies
0
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1 to 0.0.2

.npmignore

206

lib/mockRequest.js

@@ -11,99 +11,3 @@ /**

Provider = function(){};
/**
* 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.
*/
Provider.prototype._setParameter = function( key, value ) {
this.params[key] = 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.
*/
Provider.prototype._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.
*/
Provider.prototype._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.
*/
Provider.prototype._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.
*/
Provider.prototype._addBody = function( key, value ) {
this.body[key] = value;
};
/**
* Function: createRequest

@@ -126,14 +30,106 @@ *

exports.createRequest = function(options){
var p = new Provider();
exports.createRequest = function(options) {
if (!options) {
options = {};
}
p.method = (options.method) ? options.method : 'GET';
p.url = (options.url ) ? options.url : '';
p.params = (options.params) ? options.params : {};
p.body = (options.body ) ? options.body : {};
return p;
return {
method: (options.method) ? options.method : 'GET',
url : (options.url ) ? options.url : '',
params: (options.params) ? options.params : {},
body : (options.body ) ? options.body : {},
/**
* 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;
},
/**
* 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;
}
};
};

@@ -11,245 +11,250 @@ /**

Provider = function(){};
/**
* Function: writeHead
* Function: createResponse
*
* The 'writeHead' function from node's HTTP API.
*
* Parameters:
* Creates a new mock 'response' instance. All values are reset to the
* defaults.
*
* 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
*
* 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
* options - An object of named parameters.
*
* This function has the same behavior as the 'send' function.
* Options:
*
* Parameters:
*
* data - The data to return. Must be a string. Appended to
* previous calls to data.
* encoding - Optional encoding value.
* encoding - The default encoding for the response
*/
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;
exports.createResponse = function(options) {
if (!options) {
options = {};
}
if (encoding) {
this._encoding = encoding;
}
};
var _endCalled = false;
var _data = "";
var _headers = {};
var _encoding = options.encoding;
/**
* Function: getHeader
*
* Returns a particular header by name.
*/
Provider.prototype.getHeader = function(name) {
return this._headers[name];
};
return {
statusCode: -1,
/**
* Function: setHeader
*
* Set a particular header by name.
*/
Provider.prototype.setHeader = function(name, value) {
return this._headers[name] = value;
};
/**
* Function: writeHead
*
* The 'writeHead' function from node's HTTP API.
*
* Parameters:
*
* statusCode - A number to send as a the HTTP status
* headers - An object of properties that will be used for
* the HTTP headers.
*/
writeHead: function( statusCode, phrase, headers ) {
if (_endCalled) {
throw "The end() method has already been called.";
}
this.statusCode = statusCode;
// Note: Not sure if the headers given in this function
// overwrite any headers specified earlier.
if (headers) {
_reasonPhrase = phrase;
_headers = headers;
}
else {
_headers = phrase;
}
},
/**
* 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 ) {
_data += data;
if (encoding) {
_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);
},
/**
* Function: removeHeader
*
* Removes an HTTP header by name.
*/
Provider.prototype.removeHeader = function(name) {
delete this._headers[name];
};
/**
* Function: end
*
* The 'end' function from node's HTTP API that finishes
* the connection request. This must be called.
*
* Parameters:
*
* data - Optional data to return. Must be a string. Appended
* to previous calls to <send>.
* encoding - Optional encoding value.
*/
end: function( data, encoding ) {
_endCalled = true;
if (data) {
_data += data;
}
if (encoding) {
_encoding = encoding;
}
},
/**
* Function: 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;
};
/**
* Function: getHeader
*
* Returns a particular header by name.
*/
getHeader: function(name) {
return _headers[name];
},
//This mock object stores some state as well
//as some test-analysis functions:
/**
* Function: setHeader
*
* Set a particular header by name.
*/
setHeader: function(name, value) {
return _headers[name] = value;
},
/**
* 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: removeHeader
*
* Removes an HTTP header by name.
*/
removeHeader: function(name) {
delete _headers[name];
},
/**
* Function: setEncoding
*
* Sets the encoding for the data. Generally 'utf8'.
*
* Parameters:
*
* encoding - The string representing the encoding value.
*/
setEncoding: function(encoding) {
_encoding = encoding;
},
//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.
*/
Provider.prototype._getHeaders = function() {
return this._headers;
};
/**
* 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.
*/
Provider.prototype._getData = function() {
return this._data;
};
/**
* Function: _getData
*
* The data sent to the user.
*/
_getData: function() {
return _data;
},
/**
* Function: _getStatusCode
*
* The status code that was sent to the user.
*/
Provider.prototype._getStatusCode = function() {
return this.statusCode;
};
/**
* 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.
*/
Provider.prototype._isJSON = function() {
return (this._headers["Content-Type"] == "application/json");
};
/**
* 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: _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: _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: createResponse
*
* Creates a new mock 'response' instance. All values are reset to the
* defaults.
*
* Parameters:
*
* options - An object of named parameters.
*
* Options:
*
*/
exports.createResponse = function(options) {
var p = new Provider();
p._endCalled = false;
p._data = "";
p.statusCode = -1;
p._headers = {};
return p;
};
/**
* Function: _isDataLengthValid
*
* If the Content-Length header was set, this will only
* return true if the length is actually the length of the
* data that was set.
*
* Returns:
*
* True if the "Content-Length" header was not
* set. Otherwise, it compares it.
*/
_isDataLengthValid: function() {
if (_headers["Content-Length"]) {
return (_headers["Content-Length"] == _data.length);
}
return true;
}
};
};

@@ -5,3 +5,3 @@ {

"description": "Mock 'http' objects for testing Express routing functions",
"version": "0.0.1",
"version": "0.0.2",
"homepage": "http://www.github.com/howardabrams/node-mocks-http",

@@ -8,0 +8,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc