{"library":"mock-http","title":"Mock HTTP Request/Response for Node.js","description":"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.","language":"javascript","status":"maintenance","last_verified":"Tue Apr 21","install":{"commands":["npm install mock-http"],"cli":null},"imports":["import { Request, Response } from 'mock-http';","import * as mock from 'mock-http';","const { Request, Response } = require('mock-http');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import * as mock from 'mock-http';\nimport { strict as assert } from 'assert';\n\ndescribe('mock-http example', function(){\n    // a middleware function under test\n    var middleware = function(req, res, next) {\n        var regex = /^(?:\\/test)(\\/.*|$)/;\n        req.params = '';\n\n        req.on('data', function(data){\n            req.params += data; // a simple body parser\n        });\n        req.on('end', function(){\n            if (regex.test(req.url)) {\n                req.url = req.url.replace(regex, '$1') || '/';\n                res.writeHead(200, { 'Cache-Control': 'max-age=300'});\n                res.write('this is a test');\n                res.end();\n            }\n            else {\n                next && next();\n            }\n        });\n    };\n\n    it('shall respond with a 200', function(done){\n        var req = new mock.Request({\n                    url: '/test',\n                    method: 'POST',\n                    buffer: Buffer.from('name=mock&version=first')\n                });\n        var res = new mock.Response({\n                onEnd: function() {\n                    // the test ends here\n                    assert.equal(req.url, '/');\n                    assert.equal(req.params, 'name=mock&version=first');\n                    assert.equal(res.statusCode, 200);\n                    assert.equal(res.headersSent, true);\n                    assert.equal(res.getHeader('Cache-Control'), 'max-age=300');\n                    assert.equal(res.hasEnded(), true);\n                    done();\n                }\n            });\n        middleware(req, res, function(){\n            done(new Error('Next middleware should not have been called'));\n        });\n    });\n\n    it('shall call next middleware', function(done){\n        var req = new mock.Request({\n                    url: '/other',\n                    method: 'POST',\n                    buffer: Buffer.from('data=something')\n                });\n        var res = new mock.Response({\n                onEnd: function() {\n                    done(new Error('Response should not have ended, next middleware should be called'));\n                }\n            });\n        middleware(req, res, function(){\n            // This is the expected path for this test case\n            assert.equal(res.hasEnded(), false); // Ensure response was not ended by this middleware\n            done();\n        });\n    });\n});","lang":"javascript","description":"Demonstrates mocking HTTP requests and responses to test a simple Connect-style middleware function, verifying headers, body, status, and middleware chaining behavior.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}