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

http-mockserver

v2.7.11

Published

Testing made easy with mocked http servers

Downloads

2,250

Readme

Install

$ npm install --save http-mockserver

Usage

const { mockServer } = require('http-mockserver');

// Static mock
mockServer.addMock({
	port: 8080,
	method: 'GET',
	uri: '/my/url',
	response: {
		body: 'Hello world'
	}
});

// Dynamic mock
let counter = 0;
mockServer.addMock({
	port: 8080,
	method: 'GET',
	uri: '/my/other/url',
	handler: function (req, res) {
		counter++;
		res.send(`Counter: ${counter}`);
	}
});

Mock object

To mock an endpoint, the mock object should follow one of these structures:

Static mock

  • port: port of request, eg. 4000
  • uri: uri of request, eg. /users/peter
  • method: method of request, eg. GET
  • response: Response object
    • body, eg. {data: "hello"}
    • statusCode, eg. 404 (default: 200)
    • headers, eg. {"Content-Type": "application/json"}

Dynamic mock

  • port: port of request, eg. 4000
  • uri: uri of request, eg. /users/peter
  • method: method of request, eg. GET
  • handler: function(req, res) {...}

API

The following methods are available on both mockServer and mockClient

addMock(mock)

Mock and endpoint of a port and route, with a specific response (static mock) or a handler function (dynamic mock).

Arguments:

clearAll()

Remove all mocks and clear the request log

create(port)

Returns a mockserver instance with the same API interface, but the methods do not require a port to be specified.

Arguments:

  • port: Port number.

Example:

const { mockServer } = require('http-mockserver');
const backendService = mockServer.create(8888);

backendService.addMock({
	uri: '/some/url/to/mock',
	method: 'GET',
	response: {
		body: 'Hello world!'
	}
});

$ curl localhost:8888/some/url/to/mock 
"Hello world!"

getRequests([port])

Returns request log. If port is specified, request will be filtered by this

Arguments:

  • port: Port number.

waitForRequest(port, predicate, [count = 1, delay = 500])

Returns a list of request logs that the predicate returns truthy for. The predicate is invoked with three arguments: (requestLog, index, requestLogs).

Arguments:

  • port: Port number.
  • predicate: The function invoked per iteration.
  • count: Exact number of request logs to match before returning
  • delay: Time between requests

MockServer API

The following methods are available only available on mockServer

mockServer.start(port)

Start mockserver on the specified port.

Arguments:

  • port: Port of MockServer (default: 3000)

mockServer.stop()

Stop mockserver

MockClient API

The following methods are available only available on mockClient. You only need to use the client if you are communicating with a mockServer that was started from commandline or by a separate Node process with mockServer.start().

mockClient.setServerHost(serverHost)

Set hostname and port of mockserver. This is necessary if you start MockServer on another port than the default (port 3000).

Arguments:

  • serverHost: Host of mockserver (Default: localhost:3000)

MockServer CLI Options

If you need to interact with the mockserver from other languages that Node.js, you can start it as a stand-alone process, and add mocks by interacting with the REST api. To start mockServer from the command-line run http-mockserver. You can also use the CLI tool to start a mockserver with some preconfigured mocks, and load them on startup with http-mockserver --mocks ./mock-folder

$ http-mockserver --help

  Usage: http-mockserver [options]

  Options
    --port     MockServer port, default: 3000
    --mocks    Path to mock config files