{"id":20994107,"url":"https://github.com/tradeshift/http-mockserver","last_synced_at":"2025-05-14T21:30:39.114Z","repository":{"id":13901739,"uuid":"74363529","full_name":"Tradeshift/http-mockserver","owner":"Tradeshift","description":"Simplify development and testing by mocking http services with ease.","archived":false,"fork":false,"pushed_at":"2025-04-25T15:12:52.000Z","size":694,"stargazers_count":10,"open_issues_count":20,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-12T21:57:03.163Z","etag":null,"topics":["cli","mock-server","mockclient","mocking","node","npm","test","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tradeshift.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-11-21T12:55:36.000Z","updated_at":"2024-12-09T08:04:10.000Z","dependencies_parsed_at":"2023-02-17T19:15:50.809Z","dependency_job_id":"55745d2a-7f42-4033-a7cd-8afed0f6b317","html_url":"https://github.com/Tradeshift/http-mockserver","commit_stats":{"total_commits":94,"total_committers":18,"mean_commits":5.222222222222222,"dds":0.7340425531914894,"last_synced_commit":"e900749283e41d500abf0357224b34840553ee96"},"previous_names":["tradeshift/node-mock-server"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tradeshift%2Fhttp-mockserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tradeshift%2Fhttp-mockserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tradeshift%2Fhttp-mockserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tradeshift%2Fhttp-mockserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tradeshift","download_url":"https://codeload.github.com/Tradeshift/http-mockserver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254230746,"owners_count":22036230,"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":["cli","mock-server","mockclient","mocking","node","npm","test","unit-testing"],"created_at":"2024-11-19T07:16:52.278Z","updated_at":"2025-05-14T21:30:38.575Z","avatar_url":"https://github.com/Tradeshift.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Install\n```\n$ npm install --save http-mockserver\n```\n\n## Usage\n\n```js\nconst { mockServer } = require('http-mockserver');\n\n// Static mock\nmockServer.addMock({\n\tport: 8080,\n\tmethod: 'GET',\n\turi: '/my/url',\n\tresponse: {\n\t\tbody: 'Hello world'\n\t}\n});\n\n// Dynamic mock\nlet counter = 0;\nmockServer.addMock({\n\tport: 8080,\n\tmethod: 'GET',\n\turi: '/my/other/url',\n\thandler: function (req, res) {\n\t\tcounter++;\n\t\tres.send(`Counter: ${counter}`);\n\t}\n});\n```\n\n## Mock object\nTo mock an endpoint, the mock object should follow one of these structures:\n\n#### Static mock\n* **port**: port of request, eg. `4000`\n* **uri**: uri of request, eg. `/users/peter`\n* **method**: method of request, eg. `GET`\n* **response**: Response object\n\t* **body**, eg. `{data: \"hello\"}`\n\t* **statusCode**, eg. `404` (default: 200)\n\t* **headers**, eg. `{\"Content-Type\": \"application/json\"}`\n\n#### Dynamic mock\n* **port**: port of request, eg. `4000`\n* **uri**: uri of request, eg. `/users/peter`\n* **method**: method of request, eg. `GET`\n* **handler**: `function(req, res) {...}`\n\n# API\nThe following methods are available on both `mockServer` and `mockClient`\n\n#### addMock(mock)\nMock and endpoint of a port and route, with a specific response (static mock) or a handler function (dynamic mock).\n\nArguments:\n* **mock**: Mock object. See [mock object](https://github.com/Tradeshift/http-mockserver/blob/master/README.md#mock-object) for details\n\n#### clearAll()\nRemove all mocks and clear the request log\n\n#### create(port)\nReturns a mockserver instance with the same API interface, but the methods do not require a port to be specified.\n\nArguments:\n* **port**: Port number.\n\nExample:\n```js\nconst { mockServer } = require('http-mockserver');\nconst backendService = mockServer.create(8888);\n\nbackendService.addMock({\n\turi: '/some/url/to/mock',\n\tmethod: 'GET',\n\tresponse: {\n\t\tbody: 'Hello world!'\n\t}\n});\n\n$ curl localhost:8888/some/url/to/mock \n\"Hello world!\"\n```\n\n#### getRequests([port])\nReturns request log. If port is specified, request will be filtered by this\n\nArguments:\n* **port**: Port number.\n\n#### waitForRequest(port, predicate, [count = 1, delay = 500])\nReturns a list of request logs that the predicate returns truthy for. The predicate is invoked with three arguments: (requestLog, index, requestLogs).\n\nArguments:\n* **port**: Port number.\n* **predicate**: The function invoked per iteration.\n* **count**: Exact number of request logs to match before returning\n* **delay**: Time between requests\n\n## MockServer API\nThe following methods are available only available on `mockServer`\n\n#### mockServer.start(port)\nStart mockserver on the specified port.\n\nArguments:\n* **port**: Port of MockServer (default: 3000)\n\n#### mockServer.stop()\nStop mockserver\n\n## MockClient API\nThe following methods are available only available on `mockClient`.\nYou only need to use the client if you are communicating with a mockServer that was started [from commandline](https://github.com/Tradeshift/http-mockserver#mockserver-cli-options) or by a separate Node process with [mockServer.start()](https://github.com/Tradeshift/http-mockserver#mockserverstartport). \n\n#### mockClient.setServerHost(serverHost)\nSet hostname and port of mockserver. This is necessary if you start MockServer on another port than the default (port 3000).\n\nArguments:\n* **serverHost**: Host of mockserver (Default: `localhost:3000`)\n\n## MockServer CLI Options\nIf you need to interact with the mockserver from other languages that Node.js, you can start it as a stand-alone process, and add mocks by interacting with the REST api.\nTo start mockServer from the command-line run `http-mockserver`.\nYou can also use the CLI tool to start a mockserver with some preconfigured mocks, and load them on startup with `http-mockserver --mocks ./mock-folder`\n\n```\n$ http-mockserver --help\n\n  Usage: http-mockserver [options]\n\n  Options\n    --port     MockServer port, default: 3000\n    --mocks    Path to mock config files\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftradeshift%2Fhttp-mockserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftradeshift%2Fhttp-mockserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftradeshift%2Fhttp-mockserver/lists"}