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

hock

v1.4.1

Published

A mocking server for HTTP requests

Downloads

72,444

Readme

hock Build Status Join the chat at https://gitter.im/mmalecki/hock

An HTTP mocking server based on Nock.

Overview

Hock is an HTTP mocking server with an API designed to closely match that of Nock. The key difference between Nock and Hock is that nock works by overriding http.clientRequest, allowing requests to be intercepted before they go over the wire.

Hock is designed as a fully functioning HTTP service. You enqueue requests and responses in a similar fashion to Nock:


    var http = require('http'),
        hock = require('hock'),
        request = require('request');

    var mock = hock.createHock();
    mock
        .get('/some/url')
        .reply(200, 'Hello!');

    var server = http.createServer(mock.handler);
    server.listen(1337, function () {
        request('http://localhost:' + 1337 + '/some/url', function(err, res, body) {
           console.log(body);
        });
    });

HTTP Methods

Hock supports the 5 primary HTTP methods at this time:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • HEAD
  • COPY
  • OPTIONS
    // Returns a hock Request object
    var req = hockServer.get(url, requestHeaders);
    // Returns a hock Request object
    var req = hockServer.delete(url, requestHeaders);
    // Returns a hock Request object
    var req = hockServer.post(url, body, requestHeaders);
    // Returns a hock Request object
    var req = hockServer.put(url, body, requestHeaders);
    // Returns a hock Request object
    var req = hockServer.head(url, requestHeaders);

Request Object

All of these methods return an instance of a Request, a hock object which contains all of the state for a mocked request. To define the response and enqueue into the hockServer, call either reply or replyWithFile on the Request object:

    // returns the current hockServer instance
    req.reply(statusCode, body, responseHeaders);
    // returns the current hockServer instance
    req.replyWithFile(statusCode, filePath, responseHeaders);

You can optionally send a ReadableStream with reply, for example testing with large responses without having to use a file on disk:

    // returns the current hockServer instance
    req.reply(statusCode, new RandomStream(10000), responseHeaders);

You can also provide functions instead of concrete values. These functions will be called with the matching incoming http request, and it useful in cases where the response body or headers need to be constructed based on the incoming request data:

    // returns the current hockServer instance
    req.reply(
        statusCode,
        function replyWithBody(request) {
            return body;
        },
        function replyWithHeaders(request) {
            return responseHeaders;
        }
    );

Multiple matching requests

You can optionally tell hock to match multiple requests for the same route:

    hockServer.put('/path/one', {
        foo: 1,
        bar: {
            baz: true
            biz: 'asdf1234'
        }
    })
    .min(4)
    .max(10)
    .reply(202, {
        status: 'OK'
    })

Call many if you need to handle at least one, possibly many requests:

    hockServer.put('/path/one', {
        foo: 1,
        bar: {
            baz: true
            biz: 'asdf1234'
        }
    })
    .many() // min 1, max Unlimited
    .reply(202, {
        status: 'OK'
    })

Provide custom min and max options to many:

    hockServer.put('/path/one', {
        foo: 1,
        bar: {
            baz: true
            biz: 'asdf1234'
        }
    })
    .many({
        min: 4,
        max: 10
    })
    .reply(202, {
        status: 'OK'
    })

Set infinite number of requests with max(Infinity):

    hockServer.put('/path/one', {
        foo: 1,
        bar: {
            baz: true
            biz: 'asdf1234'
        }
    })
    .max(Infinity)
    .reply(202, {
        status: 'OK'
    })

If you don't care how many or how few requests are served, you can use any:

    hockServer.put('/path/one', {
        foo: 1,
        bar: {
            baz: true
            biz: 'asdf1234'
        }
    })
    .any() // equivalent to min(0), max(Infinity)
    .reply(202, {
        status: 'OK'
    })

hockServer.done() with many

hockServer.done() will verify the number of requests fits within the minimum and maximum constraints specified by min, max, many or any:

hockServer.get('/').min(2)
request.get('/', function() {
  hockServer.done(function(err) {
    console.error(err) // error, only made one request
  })
})

If the number of requests doesn't verify and you don't supply a callback to hockServer.done() it will throw!

Chaining requests

As the reply and replyWithFile methods return the current hockServer, you can chain them together:


    hockServer.put('/path/one', {
        foo: 1,
        bar: {
            baz: true
            biz: 'asdf1234'
        }
    })
    .reply(202, {
        status: 'OK'
    })
    .get('/my/file/should/be/here')
    .replyWithFile(200, __dirname + '/foo.jpg');

Matching requests

When a request comes in, hock iterates through the queue in a First-in-first-out approach, so long as the request matches. The criteria for matching is based on the method and the url, and additionally the request body if the request is a PUT, PATCH, or POST. If you specify request headers, they will also be matched against before sending the response.

Path filtering

You can filter paths using regex or a custom function, this is useful for things like timestamps that get appended to urls from clients.


    hockServer
        .filteringPathRegEx(/timestamp=[^&]*/g, 'timestamp=123')
        .get('/url?timestamp=123')
        .reply(200, 'Hi!');

    hockServer
        .filteringPath(function (p) {
            return '/url?timestamp=XXX';
        })
        .get('/url?timestamp=XXX')
        .reply(200, 'Hi!');