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

httpmocker

v1.3.5

Published

http request mocker

Downloads

23

Readme

HTTP Mocker

Build Status

NPM

NPM

httpmocker is a testing tool for mocking any requests that you are not able to access in your test file.

Features

  • don't change any source codes from your lib directory
  • configurable (easy to filter requests)

Why?

Sometimes, you may use these third-party modules like node-aws, node-semantics3 those control how the program call requests over HTTP/HTTPS, so if you wanna accurately filter these requests, it's hacky and ugly.

Imagine the following aws codes:

// create the AWS.Request object
var request = new AWS.EC2().describeInstances();

// register a callback to report on the data
request.on('success', function(resp) {
  console.log(resp.data); // log the successful data response
});

// send the request
request.send();

if you wanna filter your program in your testing progress, you have to add 2 lines in your source code:

if (process.env.NODE_ENV === 'test')
  // return or callback

once there are the certain amount number of third-party function calls in your source, you then need to write the corresponding number of above blocks, that's aweful.

Let's be graceful from httpmocker

In your test file:

var configmock = require('httpmocker').config;
configmock({
  'https://api.aws.com/': {
    error: null,
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json'
    },
    body: {foo: 'bar'}
  }, {
  'https://example.com/users/:id': {
    error: null,
    statusCode: 404,
    body: 'not found'
  }
});

then when you do request:

request.send();

it will return:

function onresponse (res) {
  // res.statusCode = 200;
  // res.headers['Content-Type'] = 'application/json';
  // res.body = {foo: 'bar'};
}

API

httpmocker exports one function httpmocker.config(config), it provides a way to define responses which you are going to expect to mock.

The config is an object that maps the router, Object.keys(config) should return urls(prefix) that you wanna mock for testing. And every url prefix should hold a object that like:

{
  "error": "...your error if you wanna throw",
  "statusCode": 400, // or status
  "headers": {
    "head1_key": "head1_value"
  },
  "body": "string/buffer/object, will write to readable instance"
}

httpmocker exports 2nd function httpmocker.clear([url]), it provides a way to clear config that you set, it receives one optional argument, if provided, will remove config by url that you would pass, if not, will remove all configurations.

NOTE: please use httpmocker with NODE_ENV=test

Installation

Recommended command to install

$ npm install httpmocker --save-dev

License

MIT. Copyright (c) Yorkie Liu