node-mocks-http
Advanced tools
Comparing version 1.11.0 to 1.12.1
@@ -81,2 +81,4 @@ 'use strict'; | ||
mockRequest.destroy = function () {}; | ||
//parse query string from url to object | ||
@@ -83,0 +85,0 @@ if (Object.keys(mockRequest.query).length === 0) { |
@@ -379,2 +379,33 @@ 'use strict'; | ||
}; | ||
/** | ||
* Function: getEndArguments | ||
* | ||
* Utility function that parses and names parameters for the various | ||
* mockResponse.end() signatures. Reference: | ||
* https://nodejs.org/api/http.html#http_response_end_data_encoding_callback | ||
* | ||
*/ | ||
function getEndArguments(args) { | ||
var data, encoding, callback; | ||
if (args[0]) { | ||
if (typeof args[0] === 'function') { | ||
callback = args[0]; | ||
} else { | ||
data = args[0]; | ||
} | ||
} | ||
if (args[1]) { | ||
var type = typeof args[1]; | ||
if (type === 'function') { | ||
callback = args[1]; | ||
} else if (type === 'string' || args[1] instanceof String){ | ||
encoding = args[1]; | ||
} | ||
} | ||
if (args[2] && typeof args[2] === 'function') { | ||
callback = args[2]; | ||
} | ||
return { data: data, encoding: encoding, callback: callback }; | ||
} | ||
@@ -386,10 +417,14 @@ /** | ||
* the connection request. This must be called. | ||
* | ||
* Signature: response.end([data[, encoding]][, callback]) | ||
* | ||
* Parameters: | ||
* Parameters: | ||
* | ||
* data - Optional data to return. Must be a string. Appended | ||
* to previous calls to <send>. | ||
* data - Optional data to return. Must be a string or Buffer instance. | ||
* Appended to previous calls to <send>. | ||
* encoding - Optional encoding value. | ||
* callback - Optional callback function, called once the logic has run | ||
* | ||
*/ | ||
mockResponse.end = function(data, encoding) { | ||
mockResponse.end = function() { | ||
if (_endCalled) { | ||
@@ -405,9 +440,11 @@ // Do not emit this event twice. | ||
_endCalled = true; | ||
if (data) { | ||
if (data instanceof Buffer) { | ||
_chunks.push(data); | ||
_size += data.length; | ||
var args = getEndArguments(arguments); | ||
if (args.data) { | ||
if (args.data instanceof Buffer) { | ||
_chunks.push(args.data); | ||
_size += args.data.length; | ||
} else { | ||
_data += data; | ||
_data += args.data; | ||
} | ||
@@ -432,11 +469,15 @@ } | ||
if (encoding) { | ||
_encoding = encoding; | ||
if (args.encoding) { | ||
_encoding = args.encoding; | ||
} | ||
mockResponse.emit('end'); | ||
mockResponse.writableFinished = true; //Reference: https://nodejs.org/docs/latest-v12.x/api/http.html#http_request_writablefinished | ||
mockResponse.emit('finish'); | ||
if (args.callback) { | ||
args.callback(); | ||
} | ||
}; | ||
@@ -443,0 +484,0 @@ /** |
@@ -5,3 +5,3 @@ { | ||
"description": "Mock 'http' objects for testing Express routing functions", | ||
"version": "1.11.0", | ||
"version": "1.12.1", | ||
"homepage": "https://github.com/howardabrams/node-mocks-http", | ||
@@ -8,0 +8,0 @@ "bugs": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1951
72249
17