Package Exports
- fake-http-request
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fake-http-request) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Fake Node.js HTTP Request
Utility class to fake a HTTP/HTTPS request for unit testing Node.js projects. It captures arguments for outgoing requests and allows you to simulate network errors and responses easily.
Installation
npm install fake-http-requestUsage
Before the relevant HTTP/S requests, install the fake request:
var fake = require('fake-http-request');
fake.install('https');
To clean up and restore the original HTTP/S requests, after testing, use:
fake.uninstall('https');This will replace the system https.request with a test method that captures calls instead of sending them out to the network, so it will work with any client code that uses the system http/https libraries.
Both install and uninstall can take an argument -- the module name where to install the fake requests. By default, they will use https.
You can then use https.request.calls to inspect individual calls. Each call object will have the following structure:
args:array-- arguments passed to the requestbody:array-- chunks written to the request bodynetworkError:function (error)-- use this to simulate a network error for the call.respond:function(httpCode, statusMessage, body)-- use this to simulate a successful network response.
Example
var fakeRequest = require('fake-http-request'),
https = require('https'),
request = require('request');
fakeRequest.install();
request('https://www.google.com', function (error, response, body) {
console.log('got response', response.statusCode, response.statusMessage, body)
}).on('request', function () {
console.log('number of calls', https.request.calls.length);
console.log('first call', https.request.calls[0].args[0].host, https.request.calls[0].args[0].port, https.request.calls[0].args[0].path);
// simulate a response
https.request.calls[0].respond(404, 'Not found', 'some html here');
});
call = request('https://www.google.com', function (error, response, body) {
console.log('got error', error);
}).on('request', function () {
var mostRecent = https.request.calls.length - 1;
console.log('number of calls', https.request.calls.length);
console.log('second call', https.request.calls[mostRecent].args[0].host, https.request.calls[mostRecent].args[0].port, https.request.calls[mostRecent].args[0].path);
// simulate a response
https.request.calls[mostRecent].networkError('BOOM!');
});