npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fake-http-request

v1.4.0

Published

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.

Downloads

330

Readme

Fake Node.js HTTP Request

Build status

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`

Installation

Install using NPM, with

npm install fake-http-request -D

Usage

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 request
  • body: array -- chunks written to the request body
  • networkError: 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. The call is executed using setTimeout so you can also respond, knowing that the synchronous processing of the calling function is complete. The arguments to the pipe will be the arguments passed to the HTTP call, and this will be set to the fake request.

Example

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', options); 
  this.respond(200, 'OK', 'some html here');
});

request('https://www.google.com');

Usage with domain matchers

In case you want to block just a certain URLs, you can pass an object with request type (type) and regex matcher to the install method. If matcher is not provided, fake-http-request will match all URLs. Request type is optional and it defaults to https.

For example, this will fake only requests made to Github URL via HTTPS:

var fakeRequest = require('fake-http-request');

fakeRequest.install({
  type: 'https',
  matcher: /github/
});

// Do something with fake requests

fakeRequest.uninstall('https');