Mock HTTP Request/Response for Node.js

1.1.1 · maintenance · verified Tue Apr 21

mock-http is a Node.js library that provides robust mock implementations of the core `http.IncomingMessage` and `http.ServerResponse` classes. This enables developers to conduct isolated unit testing of HTTP server-side logic, such as Connect, Express, or Koa middleware, without the overhead of creating a real HTTP server or managing network sockets. The library ensures full API compatibility with Node.js's native `http` module interfaces, allowing middleware to be tested in an environment closely mirroring production. Currently stable at version 1.1.1, its release cadence is generally aligned with Node.js LTS cycles for compatibility updates, rather than frequent feature additions, indicating a mature and maintenance-focused project. Its key differentiator is the faithful emulation of the standard HTTP objects, providing a reliable and predictable testing surface for complex server-side components.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates mocking HTTP requests and responses to test a simple Connect-style middleware function, verifying headers, body, status, and middleware chaining behavior.

import * as mock from 'mock-http';
import { strict as assert } from 'assert';

describe('mock-http example', function(){
    // a middleware function under test
    var middleware = function(req, res, next) {
        var regex = /^(?:\/test)(\/.*|$)/;
        req.params = '';

        req.on('data', function(data){
            req.params += data; // a simple body parser
        });
        req.on('end', function(){
            if (regex.test(req.url)) {
                req.url = req.url.replace(regex, '$1') || '/';
                res.writeHead(200, { 'Cache-Control': 'max-age=300'});
                res.write('this is a test');
                res.end();
            }
            else {
                next && next();
            }
        });
    };

    it('shall respond with a 200', function(done){
        var req = new mock.Request({
                    url: '/test',
                    method: 'POST',
                    buffer: Buffer.from('name=mock&version=first')
                });
        var res = new mock.Response({
                onEnd: function() {
                    // the test ends here
                    assert.equal(req.url, '/');
                    assert.equal(req.params, 'name=mock&version=first');
                    assert.equal(res.statusCode, 200);
                    assert.equal(res.headersSent, true);
                    assert.equal(res.getHeader('Cache-Control'), 'max-age=300');
                    assert.equal(res.hasEnded(), true);
                    done();
                }
            });
        middleware(req, res, function(){
            done(new Error('Next middleware should not have been called'));
        });
    });

    it('shall call next middleware', function(done){
        var req = new mock.Request({
                    url: '/other',
                    method: 'POST',
                    buffer: Buffer.from('data=something')
                });
        var res = new mock.Response({
                onEnd: function() {
                    done(new Error('Response should not have ended, next middleware should be called'));
                }
            });
        middleware(req, res, function(){
            // This is the expected path for this test case
            assert.equal(res.hasEnded(), false); // Ensure response was not ended by this middleware
            done();
        });
    });
});

view raw JSON →