{"id":15692272,"url":"https://github.com/qix-/handler-agent","last_synced_at":"2025-07-29T02:07:43.647Z","repository":{"id":57260616,"uuid":"260514192","full_name":"Qix-/handler-agent","owner":"Qix-","description":"Zero-dependency (req,res) handler callback agent for use in client requests","archived":false,"fork":false,"pushed_at":"2022-09-15T11:01:53.000Z","size":11,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-16T22:54:13.106Z","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/Qix-.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"qix-"}},"created_at":"2020-05-01T17:08:35.000Z","updated_at":"2024-05-15T23:02:49.000Z","dependencies_parsed_at":"2022-08-25T02:10:43.399Z","dependency_job_id":null,"html_url":"https://github.com/Qix-/handler-agent","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Qix-/handler-agent","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qix-%2Fhandler-agent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qix-%2Fhandler-agent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qix-%2Fhandler-agent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qix-%2Fhandler-agent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Qix-","download_url":"https://codeload.github.com/Qix-/handler-agent/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qix-%2Fhandler-agent/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267617643,"owners_count":24116208,"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","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-10-03T18:30:26.980Z","updated_at":"2025-07-29T02:07:43.605Z","avatar_url":"https://github.com/Qix-.png","language":"JavaScript","funding_links":["https://github.com/sponsors/qix-"],"categories":[],"sub_categories":[],"readme":"# `handler-agent`\n\n`handler-agent` creates a connection `http.Agent` usable by most HTTP clients that\nexecutes an HTTP handler (`function (req, res) { res.end('hi') }`) instead of\ncreating a network connection.\n\nThe returned agent can be used by `http.request()`, [`got`](https://npmjs.org/package/got),\nand most other Node.js HTTP clients that support the use of `http.Agent`s.\n\nThis is primarily useful for testing HTTP APIs without touching the network.\n\n## Usage\n\n```console\nnpm install --save handler-agent\n```\n\n```javascript\nconst handlerAgent = require('handler-agent');\n\nconst myHttpHandler = (req, res) =\u003e {\n\tif (req.url === '/' \u0026\u0026 req.method === 'GET') {\n\t\tres.writeHead(200);\n\t\tres.end('Hello, world!');\n\t} else {\n\t\tres.writeHead(404);\n\t\tres.end('Not Found');\n\t}\n};\n\nconst agent = handlerAgent(myHttpHandler);\n\nconst got = require('got').extend({\n\tagent: {\n\t\thttp: agent,\n\t\thttps: agent\n\t}\n});\n\ngot.get('http://localhost/').then(response =\u003e {\n\tconsole.log('GET /: Got response:', response.body);\n});\n\ngot.post('http://localhost/foo').catch(err =\u003e {\n\tconsole.error('POST /foo:', err.stack);\n});\n```\n\nOutput:\n\n```\nGET /: Got response: Hello, world!\nPOST /foo: HTTPError: Response code 404 (Not Found)\n    at PromisableRequest.\u003canonymous\u003e (/src/qix-/handler-agent/node_modules/got/dist/source/as-promise/index.js:124:28)\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)\n```\n\n## But why?\n\nTesting an HTTP API in modern tooling seems to fall in two extremes; you either use\na bloated framework and desktop client, or you roll your own testing suite from scratch.\n\nFor me personally, I like rolling my own as I tend to put myself in unique scenarios that\ndon't quite fit into the nicely specified boxes the larger frameworks prescribe.\n\nThis module is a good middleground; I'm able to use `async`/`await` with my favorite HTTP\nrequest library (e.g. `got`) without having to start a real TCP server and deal with all of\nthe fragility that comes with it.\n\nUsing `handler-agent` prevents network issues such as re-used ports, latency (yes, really),\nmanaging port/host configuration, as well as removing the need for DNS lookups, modifying\n`/etc/hosts` just to test the `Host` header, etc.\n\n# License\n\nCopyright \u0026copy; 2020 by Josh Junon.\nReleased under the [MIT License](LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqix-%2Fhandler-agent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqix-%2Fhandler-agent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqix-%2Fhandler-agent/lists"}