{"id":13509156,"url":"https://github.com/pspdfkit-labs/bypass","last_synced_at":"2026-02-22T10:36:06.232Z","repository":{"id":41441292,"uuid":"42600815","full_name":"PSPDFKit-labs/bypass","owner":"PSPDFKit-labs","description":"Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests.","archived":false,"fork":false,"pushed_at":"2024-04-26T15:11:33.000Z","size":6668,"stargazers_count":942,"open_issues_count":24,"forks_count":107,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-05-18T13:41:46.845Z","etag":null,"topics":["http","https","mock","testing"],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/bypass","language":"Elixir","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/PSPDFKit-labs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2015-09-16T16:52:16.000Z","updated_at":"2024-05-17T09:55:00.000Z","dependencies_parsed_at":"2024-05-01T17:21:13.450Z","dependency_job_id":"351a35c7-8816-4252-83ef-9fb93d93eba9","html_url":"https://github.com/PSPDFKit-labs/bypass","commit_stats":{"total_commits":154,"total_committers":35,"mean_commits":4.4,"dds":0.7142857142857143,"last_synced_commit":"64365047ea58d2a45eed43b44b186a09d726f3aa"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PSPDFKit-labs%2Fbypass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PSPDFKit-labs%2Fbypass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PSPDFKit-labs%2Fbypass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PSPDFKit-labs%2Fbypass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PSPDFKit-labs","download_url":"https://codeload.github.com/PSPDFKit-labs/bypass/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222552633,"owners_count":17002126,"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":["http","https","mock","testing"],"created_at":"2024-08-01T02:01:03.751Z","updated_at":"2025-10-21T19:02:12.337Z","avatar_url":"https://github.com/PSPDFKit-labs.png","language":"Elixir","funding_links":[],"categories":["Testing"],"sub_categories":[],"readme":"# Bypass\n\n\u003c!-- MDOC !--\u003e\n\n[![Build Status](https://github.com/PSPDFKit-labs/bypass/actions/workflows/elixir.yml/badge.svg?branch=master)](https://github.com/PSPDFKit-labs/bypass/actions)\n[![Module Version](https://img.shields.io/hexpm/v/bypass.svg)](https://hex.pm/packages/bypass)\n[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/bypass/)\n[![Total Download](https://img.shields.io/hexpm/dt/bypass.svg)](https://hex.pm/packages/bypass)\n[![License](https://img.shields.io/hexpm/l/bypass.svg)](https://github.com/PSPDFKit-labs/bypass/blob/master/LICENSE)\n[![Last Updated](https://img.shields.io/github/last-commit/PSPDFKit-labs/bypass.svg)](https://github.com/PSPDFKit-labs/bypass/commits/master)\n\n\n`Bypass` provides a quick way to create a custom plug that can be put in place\ninstead of an actual HTTP server to return prebaked responses to client\nrequests. This is most useful in tests, when you want to create a mock HTTP\nserver and test how your HTTP client handles different types of responses from\nthe server.\n\nBypass supports Elixir 1.10 and OTP 21 and up. It works with Cowboy 2.\n\n## Usage\n\nTo use Bypass in a test case, open a connection and use its port to connect your\nclient to it.\n\nIf you want to test what happens when the HTTP server goes down, use\n`Bypass.down/1` to close the TCP socket and `Bypass.up/1` to start listening on\nthe same port again. Both functions block until the socket updates its state.\n\n### Expect Functions\n\nYou can take any of the following approaches:\n\n* `expect/2` or `expect_once/2` to install a generic function that all calls to\n  bypass will use\n* `expect/4` and/or `expect_once/4` to install specific routes (method and path)\n* `stub/4` to install specific routes without expectations\n* a combination of the above, where the routes will be used first, and then the\n  generic version will be used as default\n\n### Example\n\nIn the following example `TwitterClient.start_link()` takes the endpoint URL as\nits argument allowing us to make sure it will connect to the running instance of\nBypass.\n\n```elixir\ndefmodule TwitterClientTest do\n  use ExUnit.Case, async: true\n\n  setup do\n    bypass = Bypass.open()\n    {:ok, bypass: bypass}\n  end\n\n  test \"client can handle an error response\", %{bypass: bypass} do\n    Bypass.expect_once(bypass, \"POST\", \"/1.1/statuses/update.json\", fn conn -\u003e\n      Plug.Conn.resp(conn, 429, ~s\u003c{\"errors\": [{\"code\": 88, \"message\": \"Rate limit exceeded\"}]}\u003e)\n    end)\n\n    {:ok, client} = TwitterClient.start_link(url: endpoint_url(bypass.port))\n    assert {:error, :rate_limited} == TwitterClient.post_tweet(client, \"Elixir is awesome!\")\n  end\n\n  test \"client can recover from server downtime\", %{bypass: bypass} do\n    Bypass.expect(bypass, fn conn -\u003e\n      # We don't care about `request_path` or `method` for this test.\n      Plug.Conn.resp(conn, 200, \"\")\n    end)\n\n    {:ok, client} = TwitterClient.start_link(url: endpoint_url(bypass.port))\n\n    assert :ok == TwitterClient.post_tweet(client, \"Elixir is awesome!\")\n\n    # Blocks until the TCP socket is closed.\n    Bypass.down(bypass)\n\n    assert {:error, :noconnect} == TwitterClient.post_tweet(client, \"Elixir is awesome!\")\n\n    Bypass.up(bypass)\n\n    # When testing a real client that is using e.g. https://github.com/fishcakez/connection\n    # with https://github.com/ferd/backoff to handle reconnecting, we'd have to loop for\n    # a while until the client has reconnected.\n\n    assert :ok == TwitterClient.post_tweet(client, \"Elixir is awesome!\")\n  end\n\n  defp endpoint_url(port), do: \"http://localhost:#{port}/\"\nend\n```\n\nThat's all you need to do. Bypass automatically sets up an `on_exit` hook to\nclose its socket when the test finishes running.\n\nMultiple concurrent Bypass instances are supported, all will have a different\nunique port.  Concurrent requests are also supported on the same instance.\n\n\u003e Note: `Bypass.open/0` **must not** be called in a `setup_all` blocks due to\n\u003e the way Bypass verifies the expectations at the end of each test.\n\n## How to use with ESpec\n\nWhile Bypass primarily targets ExUnit, the official Elixir builtin test\nframework, it can also be used with [ESpec](https://hex.pm/packages/espec). The\ntest configuration is basically the same, there are only two differences:\n\n1. In your Mix config file, you must declare which test framework Bypass is\n   being used with (defaults to `:ex_unit`). This simply disables the automatic\n   integration with some hooks provided by `ExUnit`.\n\n   ```elixir\n   config :bypass, test_framework: :espec\n   ```\n\n2. In your specs, you must explicitly verify the declared expectations. You can\n   do it in the `finally` block.\n\n   ```elixir\n   defmodule TwitterClientSpec do\n     use ESpec, async: true\n\n     before do\n       bypass = Bypass.open()\n       {:shared, bypass: bypass}\n     end\n\n     finally do\n       Bypass.verify_expectations!(shared.bypass)\n     end\n\n     specify \"the client can handle an error response\" do\n       Bypass.expect_once(shared.bypass, \"POST\", \"/1.1/statuses/update.json\", fn conn -\u003e\n         Plug.Conn.resp(conn, 429, ~s\u003c{\"errors\": [{\"code\": 88, \"message\": \"Rate limit exceeded\"}]}\u003e)\n       end)\n\n       {:ok, client} = TwitterClient.start_link(url: endpoint_url(shared.bypass.port))\n       assert {:error, :rate_limited} == TwitterClient.post_tweet(client, \"Elixir is awesome!\")\n     end\n\n     defp endpoint_url(port), do: \"http://localhost:#{port}/\"\n   end\n   ```\n\n## Configuration options\n\nSet `:enable_debug_log` to `true` in the application environment to make Bypass\nlog what it's doing:\n\n```elixir\nconfig :bypass, enable_debug_log: true\n```\n\n\u003c!-- MDOC !--\u003e\n\n## Installation\n\nAdd `:bypass` to your list of dependencies in mix.exs:\n\n```elixir\ndef deps do\n  [\n    {:bypass, \"~\u003e 2.1\", only: :test}\n  ]\nend\n```\n\nWe do not recommended adding `:bypass` to the list of applications in your\n`mix.exs`.\n\n## License\n\nThis software is licensed under [the MIT license](LICENSE).\n\n## About\n\n\u003ca href=\"https://nutrient.io/\"\u003e\n  \u003cimg src=\"https://avatars2.githubusercontent.com/u/1527679?v=3\u0026s=200\" height=\"80\" /\u003e\n\u003c/a\u003e\n\nThis project is maintained and funded by [Nutrient](https://nutrient.io/).\n\nPlease ensure [you signed our\nCLA](https://www.nutrient.io/guides/web/miscellaneous/contributing/) so we\ncan accept your contributions.\n\nSee [our other open source projects](https://github.com/PSPDFKit-labs), read\n[our blog](https://nutrient.io/blog/) or say hello on X\n([@nutrientdocs](https://x.com/nutrientdocs)).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpspdfkit-labs%2Fbypass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpspdfkit-labs%2Fbypass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpspdfkit-labs%2Fbypass/lists"}