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

mock-http-server

v1.4.5

Published

Controllable HTTP Server Mock for your functional tests

Downloads

124,239

Readme

Node.js HTTP Server Mock

Mockable HTTP Server your functional tests.

npm install mock-http-server

Full working example

var ServerMock = require("mock-http-server");

describe('Test', function() {

    // Run an HTTP server on localhost:9000
    var server = new ServerMock({ host: "localhost", port: 9000 });

    beforeEach(function(done) {
        server.start(done);
    });

    afterEach(function(done) {
        server.stop(done);
    });

    it('should do something', function(done) {
        server.on({
            method: 'GET',
            path: '/resource',
            reply: {
                status:  200,
                headers: { "content-type": "application/json" },
                body:    JSON.stringify({ hello: "world" })
            }
        });

        // Now the server mock will handle a GET http://localhost:9000/resource
        // and will reply with 200 `{"hello": "world"}`
        done();
    });
});

Methods

Constructor

new ServerMock(httpConfig, httpsConfig) instance a new mockable HTTP/HTTPS Server. If httpConfig is defined, creates an HTTP server, while if httpsConfig is defined, creates an HTTPS server. They can be both defined.

Example:

var server = new ServerMock({
    host: "localhost",
    port: 80
}, {
    host: "localhost",
    port: 443,
    key: fs.readFileSync("private-key.pem"),
    cert: fs.readFileSync("certificate.pem")
});

start(callback)

Starts the server and invokes the callback once ready to accept connections.

Example:

beforeEach(function(done) {
    server.start(done);
});

stop(callback)

Stops the server and invokes the callback all resources have been released.

Example:

afterEach(function(done) {
    server.stop(done);
});

on(options)

Defines a request handler. Multiple calls to on() can be chained together.

| Option | Default | Description | | ------------------------ | ---------------------------------------- | ----------- | | method | GET | HTTP method to match. Can be * to match any method. | | path | | HTTP request path to match. Can be * to match any path (to be used in conjunction with filter to allow custom matching)| | filter | | The value is a filter function fn(request): if it returns true the handler gets executed. | | reply.status | 200 | HTTP response status code. Can be a number or a synchronous function fn(request) that returns the response status code. | | reply.headers | { "content-type": "application/json" } | HTTP response headers. content-length is managed by the server implementation. | | reply.headersOverrides | { "content-length": 1000 } | HTTP response headers to override to default headers (ie. content-length). If a value is set to undefined, the header gets removed from the response. | | reply.body | empty string | HTTP response body. Can be a string, a synchronous function fn(request) that returns the body, or an asynchronous function fn(request, reply) that send the response body invoking reply(body). | | reply.end | true | End the response once the body has been sent (default behaviour). If false, it will keep the response connection open indefinitely (useful to test special cases on the client side - ie. read timeout after partial body response sent). | | delay | 0 | Delays the response by X milliseconds. |

Example:

server.on({
    method: 'GET',
    path: '/resource',
    reply: {
        status:  200,
        headers: { "content-type": "application/json" },
        body:    JSON.stringify({ hello: "world" })
    }
});

or:

server.on({
    method: '*',
    path: '/resource',
    reply: {
        status:  200,
        headers: { "content-type": "application/json" },
        body:    function(req) {
            return req.method === "GET" ? JSON.stringify({ action: "read" }) : JSON.stringify({ action: "edit" });
        }
    }
});

or:

server.on({
    method: '*',
    path: '/resource',
    reply: {
        status:  200,
        headers: { "content-type": "application/json" },
        body:    function(req, reply) {
            setTimeout(function() {
                reply(req.method === "GET" ? JSON.stringify({ action: "read" }) : JSON.stringify({ action: "edit" }));
            }, 100);
        }
    }
});

or (JSON is parsed when the Content-Type is 'application/json'):

server.on({
    method: 'POST',
    path: '/resources',
    filter: function (req) {
      return _.isEqual(req.body, {
        name: 'someName',
        someOtherValue: 1234
      })
    },
    reply: {
        status:  201,
        headers: { "content-type": "application/json" },
        body: {
          id: 987654321,
          name: 'someName',
          someOtherValue: 1234
        }
    }
});

requests(filter)

Returns an array containing all requests received. If filter is defined, it allows to filter requests by method, path, or both.

| Filter | Description | | --------------- | ----------- | | method | Filter requests by method. | | path | Filter requests by path. |

Example:

// Returns all requests
server.requests();

// Returns all GET requests
server.requests({ method: "GET" });

// Returns all GET requests to /resource
server.requests({ method: "GET", path: "/resource" });

connections()

Returns an array containing all active connections.

getHttpPort()

Returns the port at which the HTTP server is listening to or null otherwise. This may be useful if you configure the HTTP server port to 0 which means the operating system will assign an arbitrary unused port.

getHttpsPort()

Returns the port at which the HTTPS server is listening to or null otherwise. This may be useful if you configure the HTTPS server port to 0 which means the operating system will assign an arbitrary unused port.

reset()

Clears request handlers and requests received by the HTTP server.

resetHandlers()

Clears all request handlers that were previously set using on() method.

resetRequests()

Clears all requests received by the HTTP server.

Contribute

How to publish a new version

Release npm package:

  1. Update version in package.json and package-lock.json
  2. Update CHANGES.md
  3. Release new version on GitHub
  4. Run npm publish

License

MIT