{"id":19621342,"url":"https://github.com/commenthol/mock-http","last_synced_at":"2025-04-28T03:32:17.840Z","repository":{"id":25203651,"uuid":"28627479","full_name":"commenthol/mock-http","owner":"commenthol","description":"Mock http request response","archived":false,"fork":false,"pushed_at":"2025-02-12T20:52:18.000Z","size":62,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T19:29:28.140Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/commenthol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-30T10:53:20.000Z","updated_at":"2025-02-12T20:52:22.000Z","dependencies_parsed_at":"2022-08-23T19:50:54.671Z","dependency_job_id":null,"html_url":"https://github.com/commenthol/mock-http","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fmock-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fmock-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fmock-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fmock-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/commenthol","download_url":"https://codeload.github.com/commenthol/mock-http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251246257,"owners_count":21558761,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T11:22:26.044Z","updated_at":"2025-04-28T03:32:17.123Z","avatar_url":"https://github.com/commenthol.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mock-http\n\n[![NPM version](https://badge.fury.io/js/mock-http.svg)](https://npmjs.com/package/mock-http)\n[![Build Status](https://secure.travis-ci.org/commenthol/mock-http.svg?branch=master)](https://travis-ci.org/commenthol/mock-http)\n\n\u003e Mock http request response\n\nThis module provides a mock to the server side request and response classes without the need of creating a socket.\nThe full API as documented on \u003chttp://nodejs.org/api/http.html\u003e is supported.\n\nAll methods can be used to mock a client requests on the server as well as server responses such allowing to unit-test e.g. connect middleware.\n\n\n## Table of Contents\n\n\u003c!-- !toc (minlevel=2 omit=\"Table of Contents\") --\u003e\n\n* [Request](#request)\n* [Response](#response)\n* [Usage](#usage)\n* [Documentation](#documentation)\n* [Contribution and License Agreement](#contribution-and-license-agreement)\n* [License](#license)\n\n\u003c!-- toc! --\u003e\n\n## Request\n\nMock implementation of Class [http.IncomingMessage](http://nodejs.org/api/http.html#http_http_incomingmessage)\n\nIt behaves like the class, apart from really handling a socket. I.e. it implements the Readable Stream Class as well.\nAll methods can be used to mock a client request on the server such allowing to unit-test e.g. connect middleware\n\n\n## Response\n\nMock implementation of Class [http.ServerResponse](http://nodejs.org/api/http.html#http_class_http_serverresponse)\n\nIt behaves like the class, apart from really handling a socket. I.e. it implements the Writable Stream Class as well.\nAll methods can be used to mock a server response such allowing to unit-test e.g. connect middleware\n\nStates are stored in the internal object `Response._internal` and can be queried from your unit-tests\n\n```js\n_internal: {\n  headers: {},             // {Object}  Response headers\n  trailers: {},            // {Object}  Trailing Response headers\n  buffer: Buffer.from(''), // {Buffer}  Internal buffer represents response body\n  timedout: false,         // {Boolean} If true than `Response.setTimeout` was called.\n  ended: false,            // {Boolean} If true than `Response.end` was called.\n}\n```\n\n## Usage\n\nThis is a unit-test using mocha which illustrates the usage. The example can be found in [./test/index.mocha.js](./test/index.mocha.js)\n\n```js\ndescribe('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 \u0026\u0026 next();\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\u0026version=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\u0026version=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            assert.equal('test never', 'reaches here');\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('name=mock\u0026version=first')\n                });\n        var res = new mock.Response({\n                onEnd: function() {\n                    assert.equal('test never', 'reaches here');\n                }\n            });\n        middleware(req, res, function(){\n            // the test ends here\n            assert.equal(req.url, '/other');\n            assert.equal(res.headersSent, false);\n            assert.equal(res.hasEnded(), false);\n            done();\n        });\n    });\n});\n```\n\n## Documentation\n\nDocumentation can be found in [./doc](./doc/index.html).\n\n\n## Contribution and License Agreement\n\nIf you contribute code to this project, you are implicitly allowing your code\nto be distributed under the MIT license. You are also implicitly verifying that\nall code is your original work.\n\n* `npm test` - runs the tests\n* `npm run lint` - runs jshint for linting\n* `npm run doc` - generates the docs in ./doc - requires `npm i -g jsdoc`\n\n## License\n\nCopyright (c) 2014-present Commenthol. (MIT License)\n\nSee [LICENSE][] for more info.\n\n[LICENSE]: ./LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fmock-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommenthol%2Fmock-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fmock-http/lists"}