{"library":"mock-http-server","title":"Mock HTTP Server for Testing","description":"mock-http-server is a Node.js library designed to create controllable HTTP and HTTPS server mocks, primarily for use in functional and integration tests. It allows developers to define expected incoming requests by method and path, and then configure custom responses including status codes, headers, and body content. The current stable version is 1.4.5, with recent minor releases adding features like request history clearing, improved body parsing for various content types (text/plain, urlencoded), and specific port retrieval methods. Its release cadence appears to be feature-driven, with new functionalities and minor fixes arriving periodically. Key differentiators include its explicit control over request matching and response generation, the ability to inspect received requests for assertions, and support for both HTTP and HTTPS protocols. It aims to provide a reliable and isolated testing environment without actual network calls to external services, allowing for consistent and fast test execution.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mock-http-server"],"cli":null},"imports":["import ServerMock from 'mock-http-server';","const ServerMock = require('mock-http-server');","import fs from 'node:fs';\nconst server = new ServerMock({ host: \"localhost\", port: 80 }, { host: \"localhost\", port: 443, key: fs.readFileSync(\"private-key.pem\"), cert: fs.readFileSync(\"certificate.pem\") });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import ServerMock from 'mock-http-server';\nimport util from 'node:util';\n\ndescribe('Test with Mock HTTP Server', function() {\n\n    // Run an HTTP server on localhost:9000\n    var server = new ServerMock({ host: \"localhost\", port: 9000 });\n\n    const startServer = util.promisify(server.start).bind(server);\n    const stopServer = util.promisify(server.stop).bind(server);\n\n    beforeEach(async () => {\n        await startServer();\n    });\n\n    afterEach(async () => {\n        server.resetHandlers(); // Clear handlers between tests\n        server.clearRequests(); // Clear request history\n        await stopServer();\n    });\n\n    it('should mock a GET request to /resource', async () => {\n        server.on({\n            method: 'GET',\n            path: '/resource',\n            reply: {\n                status:  200,\n                headers: { \"content-type\": \"application/json\" },\n                body:    JSON.stringify({ message: \"hello world from mock\" })\n            }\n        });\n\n        // Simulate an HTTP request\n        const response = await fetch('http://localhost:9000/resource');\n        const data = await response.json();\n\n        expect(response.status).toBe(200);\n        expect(data).toEqual({ message: \"hello world from mock\" });\n\n        // Verify the request was received\n        const requests = server.requests({\n            method: 'GET',\n            path: '/resource'\n        });\n        expect(requests.length).toBe(1);\n    });\n});","lang":"javascript","description":"This quickstart demonstrates how to set up, start, and stop a mock HTTP server, define a request handler, make a request against it, and assert on the response and recorded requests within a test suite (e.g., Jest or Mocha).","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}