{"id":14991349,"url":"https://github.com/bkuhlmann/http-fake","last_synced_at":"2025-10-12T21:13:50.714Z","repository":{"id":42940840,"uuid":"492484927","full_name":"bkuhlmann/http-fake","owner":"bkuhlmann","description":"A HTTP fake implementation for test suites.","archived":false,"fork":false,"pushed_at":"2025-09-16T23:44:10.000Z","size":230,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-07T20:57:16.418Z","etag":null,"topics":["api","fake","http","rspec","testing"],"latest_commit_sha":null,"homepage":"https://alchemists.io/projects/http-fake","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bkuhlmann.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.adoc","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["bkuhlmann"]}},"created_at":"2022-05-15T12:46:35.000Z","updated_at":"2025-09-16T23:44:08.000Z","dependencies_parsed_at":"2023-02-12T17:31:09.827Z","dependency_job_id":"26bb6e63-55a3-43ab-b4ab-cb85308b1575","html_url":"https://github.com/bkuhlmann/http-fake","commit_stats":{"total_commits":125,"total_committers":1,"mean_commits":125.0,"dds":0.0,"last_synced_commit":"fde3eb75c8db963466ae7817278eebdee912e70a"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"purl":"pkg:github/bkuhlmann/http-fake","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fhttp-fake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fhttp-fake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fhttp-fake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fhttp-fake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bkuhlmann","download_url":"https://codeload.github.com/bkuhlmann/http-fake/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bkuhlmann%2Fhttp-fake/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279007484,"owners_count":26084313,"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-10-11T02:00:06.511Z","response_time":55,"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":["api","fake","http","rspec","testing"],"created_at":"2024-09-24T14:27:26.855Z","updated_at":"2025-10-12T21:13:50.670Z","avatar_url":"https://github.com/bkuhlmann.png","language":"Ruby","funding_links":["https://github.com/sponsors/bkuhlmann"],"categories":[],"sub_categories":[],"readme":":http_link: link:https://github.com/httprb/http[HTTP]\n\n:toc: macro\n:toclevels: 5\n:figure-caption!:\n\n= HTTP Fake\n\nHTTP Fake is a companion to the {http_link} gem when you want a convenient way to test HTTP requests by swapping out your _real_ HTTP client with this _fake_ HTTP client. Using a fake allows you to improve the performance of your test suite by answering fake responses without hitting a live API. You'll still want to test against a live API, eventually, within your integration tests but at a lower level, like your unit tests, you can use this gem instead. This gem is particularly useful when using _Dependency Injection_, especially when coupled with the link:https://alchemists.io/projects/infusible[Infusible] gem.\n\ntoc::[]\n\n== Features\n\n* Provides a fake HTTP client as a testing companion to the {http_link} gem.\n* Supports the following HTTP verbs: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PURGE, PUT, and TRACE.\n* Uses a simple DSL for defining HTTP endpoints, headers, bodies, and statuses.\n* Works well with objects that use Dependency Injection.\n* Speeds up your test suite when you don't need a live API.\n\n== Requirements\n\n. link:https://www.ruby-lang.org[Ruby].\n. {http_link}.\n\n== Setup\n\nTo install within an existing project, run:\n\n[source,bash]\n----\nbundle add http-fake\n----\n\nYou'll want to ensure this gem is part of your _test_ group since it's\nonly meant to aid in writing specs.\n\n== Usage\n\nThis gem works with any test framework. For demonstration purposes, we'll assume you're using link:https://rspec.info[RSpec] but you can adapt these examples to your test framework of choice. A simple spec might look like this:\n\n[source,ruby]\n----\nRSpec.describe Endpoint do\n  subject(:endpoint) { described_class.new http: }\n\n  let :http do\n    HTTP::Fake::Client.new do\n      get \"/customers\" do\n        headers[\"Content-Type\"] = \"application/json\"\n        status 200\n\n        \u003c\u003c~JSON\n          {\n            \"customers\": [\n              {\"name\": \"Jill Smith\"}\n            ]\n          }\n        JSON\n      end\n    end\n  end\n\n  describe \"#customers\" do\n    it \"answers customers array when successful\" do\n      response = endpoint.customers\n      expect(response.parse).to eq(customers: [{name: \"Jill Smith\"}])\n    end\n  end\nend\n----\n\nAs you can see, our _fake_ `http` client has been defined and injected into our `endpoint` subject. When the fake is defined, the path, headers, status, and body are registered as well. This allows the fake to match against your real implementation's URL path and swap out acquiring a real HTTP response with fake response instead. When asking the endpoint for its customers, we get back the fake response with all of the normal capabilities of the real HTTP client. This works because this gem uses link:https://github.com/sinatra/mustermann[Mustermann] for pattern matching against the routes you define and also means you can define routes that are explicit -- as shown above -- or fuzzy based on your testing needs.\n\nHere's an example where multiple endpoints are defined for the same fake in case your implementation needs to test multiple endpoints at once:\n\n[source,ruby]\n----\nlet :http do\n  HTTP::Fake::Client.new do\n    connect(\"/\") { status 200 }\n\n    head(\"/\") { status 200 }\n    options(\"/\") { status 204 }\n\n    get \"/customers\" do\n      headers[\"Content-Type\"] = \"application/json\"\n      status 200\n\n      \u003c\u003c~JSON\n        {\n          \"customers\": [\n            {\"name\": \"Jill Smith\"}\n          ]\n        }\n      JSON\n    end\n\n    post \"/customers\" do\n      headers[\"Content-Type\"] = \"application/json\"\n      status 201\n      {}\n    end\n\n    put \"/customers/1\" do\n      headers[\"Content-Type\"] = \"application/json\"\n      status 200\n    end\n\n    patch \"/customers/1\" do\n      headers[\"Content-Type\"] = \"application/json\"\n      status 200\n    end\n\n    delete(\"/customers/1\") { status 204 }\n    trace(\"/\") { status 200 }\n  end\nend\n----\n\nSo far you've only seen usage of JSON responses but you might want to use other MIME types. For example, XML:\n\n[source,ruby]\n----\nHTTP::Fake::Client.new do\n  get \"/customers/1\" do\n    headers[\"Content-Type\"] = \"application/xml\"\n    status 200\n\n    \u003c\u003c~XML\n      \u003ccustomer\u003e\n        \u003cid\u003e1\u003c/id\u003e\n        \u003cname\u003eJill Smith\u003c/name\u003e\n      \u003c/customer\u003e\n    XML\n  end\nend\n----\n\nPlain text would work too:\n\n[source,ruby]\n----\nHTTP::Fake::Client.new do\n  get \"/customers\" do\n    headers[\"Content-Type\"] = \"text/plain\"\n    status 200\n\n    \"1 - Jill Smith\"\n    \"2 - Tom Bombadill\"\n  end\nend\n----\n\nYou might even want to import a fixture which is especially handy when the response is verbose or needs to be reused in different ways. Example:\n\n[source,ruby]\n----\n# Single\nHTTP::Fake::Client.new do\n  get \"/customers/1\" do\n    headers[\"Content-Type\"] = \"application/json\"\n    status 200\n    SPEC_ROOT.join(\"support/fixtures/customer.json\").read\n  end\nend\n\n# Multiple\nHTTP::Fake::Client.new do\n  get \"/customers\" do\n    headers[\"Content-Type\"] = \"application/json\"\n    status 200\n\n    \u003c\u003c~JSON\n      [#{SPEC_ROOT.join(\"support/fixtures/customer.json\").read}]\n    JSON\n  end\nend\n----\n\nSince you have the ability to define your own headers and status codes, you can also test failure\nresponse behavior as well. I'll leave that up to you to explore and experiment with further.\n\n== Development\n\nTo contribute, run:\n\n[source,bash]\n----\ngit clone https://github.com/bkuhlmann/http-fake\ncd http-fake\nbin/setup\n----\n\nYou can also use the IRB console for direct access to all objects:\n\n[source,bash]\n----\nbin/console\n----\n\n== Tests\n\nTo test, run:\n\n[source,bash]\n----\nbin/rake\n----\n\n== link:https://alchemists.io/policies/license[License]\n\n== link:https://alchemists.io/policies/security[Security]\n\n== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]\n\n== link:https://alchemists.io/policies/contributions[Contributions]\n\n== link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin]\n\n== link:https://alchemists.io/projects/http-fake/versions[Versions]\n\n== link:https://alchemists.io/community[Community]\n\n== Credits\n\n* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].\n* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbkuhlmann%2Fhttp-fake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbkuhlmann%2Fhttp-fake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbkuhlmann%2Fhttp-fake/lists"}