http-fake

A HTTP fake implementation for test suites.

https://github.com/bkuhlmann/http-fake

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.7%) to scientific vocabulary

Keywords

api fake http rspec testing
Last synced: 4 months ago · JSON representation ·

Repository

A HTTP fake implementation for test suites.

Basic Info
Statistics
  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
api fake http rspec testing
Created over 3 years ago · Last pushed 4 months ago
Metadata Files
Readme Funding License Citation

README.adoc

:http_link: link:https://github.com/httprb/http[HTTP]

:toc: macro
:toclevels: 5
:figure-caption!:

= HTTP Fake

HTTP 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.

toc::[]

== Features

* Provides a fake HTTP client as a testing companion to the {http_link} gem.
* Supports the following HTTP verbs: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PURGE, PUT, and TRACE.
* Uses a simple DSL for defining HTTP endpoints, headers, bodies, and statuses.
* Works well with objects that use Dependency Injection.
* Speeds up your test suite when you don't need a live API.

== Requirements

. link:https://www.ruby-lang.org[Ruby].
. {http_link}.

== Setup

To install within an existing project, run:

[source,bash]
----
bundle add http-fake
----

You'll want to ensure this gem is part of your _test_ group since it's
only meant to aid in writing specs.

== Usage

This 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:

[source,ruby]
----
RSpec.describe Endpoint do
  subject(:endpoint) { described_class.new http: }

  let :http do
    HTTP::Fake::Client.new do
      get "/customers" do
        headers["Content-Type"] = "application/json"
        status 200

        <<~JSON
          {
            "customers": [
              {"name": "Jill Smith"}
            ]
          }
        JSON
      end
    end
  end

  describe "#customers" do
    it "answers customers array when successful" do
      response = endpoint.customers
      expect(response.parse).to eq(customers: [{name: "Jill Smith"}])
    end
  end
end
----

As 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.

Here's an example where multiple endpoints are defined for the same fake in case your implementation needs to test multiple endpoints at once:

[source,ruby]
----
let :http do
  HTTP::Fake::Client.new do
    connect("/") { status 200 }

    head("/") { status 200 }
    options("/") { status 204 }

    get "/customers" do
      headers["Content-Type"] = "application/json"
      status 200

      <<~JSON
        {
          "customers": [
            {"name": "Jill Smith"}
          ]
        }
      JSON
    end

    post "/customers" do
      headers["Content-Type"] = "application/json"
      status 201
      {}
    end

    put "/customers/1" do
      headers["Content-Type"] = "application/json"
      status 200
    end

    patch "/customers/1" do
      headers["Content-Type"] = "application/json"
      status 200
    end

    delete("/customers/1") { status 204 }
    trace("/") { status 200 }
  end
end
----

So far you've only seen usage of JSON responses but you might want to use other MIME types. For example, XML:

[source,ruby]
----
HTTP::Fake::Client.new do
  get "/customers/1" do
    headers["Content-Type"] = "application/xml"
    status 200

    <<~XML
      
        1
        Jill Smith
      
    XML
  end
end
----

Plain text would work too:

[source,ruby]
----
HTTP::Fake::Client.new do
  get "/customers" do
    headers["Content-Type"] = "text/plain"
    status 200

    "1 - Jill Smith"
    "2 - Tom Bombadill"
  end
end
----

You 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:

[source,ruby]
----
# Single
HTTP::Fake::Client.new do
  get "/customers/1" do
    headers["Content-Type"] = "application/json"
    status 200
    SPEC_ROOT.join("support/fixtures/customer.json").read
  end
end

# Multiple
HTTP::Fake::Client.new do
  get "/customers" do
    headers["Content-Type"] = "application/json"
    status 200

    <<~JSON
      [#{SPEC_ROOT.join("support/fixtures/customer.json").read}]
    JSON
  end
end
----

Since you have the ability to define your own headers and status codes, you can also test failure
response behavior as well. I'll leave that up to you to explore and experiment with further.

== Development

To contribute, run:

[source,bash]
----
git clone https://github.com/bkuhlmann/http-fake
cd http-fake
bin/setup
----

You can also use the IRB console for direct access to all objects:

[source,bash]
----
bin/console
----

== Tests

To test, run:

[source,bash]
----
bin/rake
----

== link:https://alchemists.io/policies/license[License]

== link:https://alchemists.io/policies/security[Security]

== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]

== link:https://alchemists.io/policies/contributions[Contributions]

== link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin]

== link:https://alchemists.io/projects/http-fake/versions[Versions]

== link:https://alchemists.io/community[Community]

== Credits

* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].
* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].

Owner

  • Name: Brooke Kuhlmann
  • Login: bkuhlmann
  • Kind: user
  • Location: Boulder, CO USA
  • Company: Alchemists

Quality over quantity.

Citation (CITATION.cff)

cff-version: 1.2.0
message: Please use the following metadata when citing this project in your work.
title: Http Fake
abstract: A HTTP fake implementation for test suites.
version: 4.3.0
license: Hippocratic-2.1
date-released: 2025-07-19
authors:
  - family-names: Kuhlmann
    given-names: Brooke
    affiliation: Alchemists
    orcid: https://orcid.org/0000-0002-5810-6268
keywords:
 - ruby
 - http
 - fake
 - rspec
 - testing
repository-code: https://github.com/bkuhlmann/http-fake
repository-artifact: https://rubygems.org/gems/http-fake
url: https://alchemists.io/projects/http-fake

GitHub Events

Total
  • Delete event: 50
  • Push event: 28
  • Create event: 7
Last Year
  • Delete event: 50
  • Push event: 28
  • Create event: 7

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 125
  • Total Committers: 1
  • Avg Commits per committer: 125.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 52
  • Committers: 1
  • Avg Commits per committer: 52.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Brooke Kuhlmann b****e@a****o 125
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • rubygems 12,245 total
  • Total dependent packages: 0
  • Total dependent repositories: 1
  • Total versions: 29
  • Total maintainers: 1
rubygems.org: http-fake

A HTTP fake implementation for test suites.

  • Versions: 29
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 12,245 Total
Rankings
Dependent packages count: 15.8%
Dependent repos count: 21.7%
Forks count: 31.6%
Average: 35.1%
Stargazers count: 35.2%
Downloads: 71.4%
Maintainers (1)
Funding
  • https://github.com/sponsors/bkuhlmann
Last synced: 4 months ago

Dependencies

Gemfile rubygems
  • amazing_print ~> 1.4 development
  • caliber ~> 0.25 development
  • debug ~> 1.7 development
  • git-lint ~> 5.0 development
  • guard-rspec ~> 4.7 development
  • rake ~> 13.0 development
  • reek ~> 6.1 development
  • rspec ~> 3.12 development
  • simplecov ~> 0.22 development
http-fake.gemspec rubygems
  • http ~> 5.1
  • mustermann ~> 3.0
  • refinements ~> 10.0
  • zeitwerk ~> 2.6