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

shmock

v0.8.1

Published

Simple HTTP Mocking library

Downloads

737

Readme

Shmock

Build Status

Express based http mocking library.

The reason for this to exist is because I believe that mocking functions to change their behavior is extremely horrible and prone to errors. Libraries like nock take this approach, and then there are a few cases where thing doesn't work. Maybe because of how http clients work, etc.

So this library provides a super nice API, like the one of superagent but it creates a real http server (using express). The nice thing about this is that you don't really care about the implementation, which http client is being used, or even if nodes native http api changes.

Make sure to view the test for examples.

Installation

$ npm install shmock

Usage

Initialize with or without port

var shmock = require('shmock');

var mock = shmock(); // will give some arbitrary port

var mock2 = shmock(9000); // will use port 9000

Define expectations

On http methods

mock.get("/foo").reply(200, "bar");

On http headers

mock.get("/foo").set("Authorization", "123456").reply(200, "bar");

Specifying response headers

mock.get("/foo").set("Authorization", "123456").reply(200, "bar", {"X-my-header", "My header value"});

On querystring parameters

mock.get("/foo").query("a=bi&c=d").reply(200, "bar");
mock.get("/foo").query({a: "b", c: "d"}).reply(200, "bar");

On request body

mock.post("/foo").send({a: "b"}).reply(200, "bar");
mock.post("/foo").send("123456").reply(200, "bar");

Add a delay to the reply

mock.get("/foo").delay(500).reply(200);

Make assertions on the handler

Check if expectation has been met

var handler = mock.get("/foo").reply(200);
...
...
handler.isDone.should.be.ok;
handler.done(); // Throws an error if isDone is false

Wait for expectation to be met

var handler = mock.get("/foo").reply(200);
...
...
handler.wait(function(err) {
  if(err) {
    // A default timeout of 2 seconds has passed and still the expectation hasn't been bet
  }
});

You can also specify a timeout in ms:

handler.wait(200, function(err) { ... });

Or if using mocha:

handler.wait(200, done);

Custom middleware

Custom middlewares can be injected into the Express stack in order to perform arbitrary manipulations on mock requests, for instance:

var shmock = shmock(9100, [function(req, res, next) {
  // do something with req/res then call next
  next();
}, function(req, res, next) {
  // do something else with the request object
  next()
}]);

As you would expect the arguments are the default arguments passed to request middleware by Express, so in order they are: req (the request object); res (the response object); and done (the callback).

License

MIT