
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
fake-http-request
Advanced tools
Utility class to fake HTTP/HTTPS requests for unit testing Node.js projects. It captures arguments for outgoing requests and allows you to simulate network errors and responses easily.
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. It can also pipe outgoing HTTP/S requests to custom functions, so you can asynchronously wait for them.
npm install fake-http-request
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.You can also use https.request.pipe
to pass in a function that will receive a call every time a network request is initiated.
var fakeRequest = require('fake-http-request'),
https = require('https'),
request = require('request');
fakeRequest.install();
// simulate a response
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
);
https.request.calls[0].respond(404, 'Not found', 'some html here');
});
// simulate errors
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
);
https.request.calls[mostRecent].networkError('BOOM!');
});
// pipe calls for async processing
https.request.pipe(function (options) {
console.log('Received call',
https.request.calls[0].args[0].host,
https.request.calls[0].args[0].port,
https.request.calls[0].args[0].path
);
});
request('https://www.google.com');
FAQs
Utility class to fake HTTP/HTTPS requests for unit testing Node.js projects. It captures arguments for outgoing requests and allows you to simulate network errors and responses easily.
We found that fake-http-request demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.