{"id":15370171,"url":"https://github.com/danschultzer/test_server","last_synced_at":"2025-04-12T22:29:46.994Z","repository":{"id":59103457,"uuid":"535423655","full_name":"danschultzer/test_server","owner":"danschultzer","description":"No fuzz ExUnit test server to mock third party services","archived":false,"fork":false,"pushed_at":"2025-02-28T18:39:16.000Z","size":154,"stargazers_count":82,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-04T02:45:28.702Z","etag":null,"topics":["elixir","exunit","http2","https-server","test-server","testing","websockets"],"latest_commit_sha":null,"homepage":"","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/danschultzer.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},"funding":{"github":"danschultzer"}},"created_at":"2022-09-11T20:55:35.000Z","updated_at":"2025-03-20T20:33:05.000Z","dependencies_parsed_at":"2024-02-26T17:54:42.773Z","dependency_job_id":"38b0e0b1-bcae-4f6a-9f9e-55e6b5337a35","html_url":"https://github.com/danschultzer/test_server","commit_stats":{"total_commits":79,"total_committers":4,"mean_commits":19.75,"dds":0.07594936708860756,"last_synced_commit":"22ca602ab32311c883bff33cf99816d532e0444b"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Ftest_server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Ftest_server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Ftest_server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Ftest_server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danschultzer","download_url":"https://codeload.github.com/danschultzer/test_server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248639483,"owners_count":21137850,"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":["elixir","exunit","http2","https-server","test-server","testing","websockets"],"created_at":"2024-10-01T13:40:12.329Z","updated_at":"2025-04-12T22:29:46.974Z","avatar_url":"https://github.com/danschultzer.png","language":"Elixir","funding_links":["https://github.com/sponsors/danschultzer"],"categories":[],"sub_categories":[],"readme":"# TestServer\n\n[![Github CI](https://github.com/danschultzer/test_server/workflows/CI/badge.svg)](https://github.com/danschultzer/test_server/actions?query=workflow%3ACI)\n[![hex.pm](https://img.shields.io/hexpm/v/test_server.svg)](https://hex.pm/packages/test_server)\n\n\u003c!-- MDOC !--\u003e\n\nNo fuzz ExUnit test server to mock third party services.\n\nFeatures:\n\n- HTTP/1\n- HTTP/2\n- WebSocket\n- Built-in TLS with self-signed certificates\n- Plug route matching\n\n## Usage\n\nAdd route request expectations with `TestServer.add/2`:\n\n```elixir\ntest \"fetch_url/0\" do\n  # The test server will autostart the current test server, if not already running\n  TestServer.add(\"/\", via: :get)\n\n  # The URL is derived from the current test server instance\n  Application.put_env(:my_app, :fetch_url, TestServer.url())\n\n  {:ok, \"HTTP\"} = MyModule.fetch_url()\nend\n```\n\n`TestServer.add/2` can route a request to an anonymous function or plug with `:to` option.\n\n```elixir\nTestServer.add(\"/\", to: fn conn -\u003e\n  Plug.Conn.send_resp(conn, 200, \"OK\")\nend)\n\nTestServer.add(\"/\", to: MyPlug)\n```\n\nThe method listened to can be defined with `:via` option. By default any method is matched.\n\n```elixir\nTestServer.add(\"/\", via: :post)\n```\n\nA custom match function can be set with `:match` option:\n\n```elixir\nTestServer.add(\"/\", match: fn\n  %{params: %{\"a\" =\u003e \"1\"}} = _conn -\u003e true\n  _conn -\u003e false\nend)\n```\n\nWhen a route is matched it'll be removed from active routes list. The route will be triggered in the order they were added:\n\n```elixir\nTestServer.add(\"/\", via: :get, to: \u0026Plug.Conn.send_resp(\u00261, 200, \"first\"))\nTestServer.add(\"/\", via: :get, to: \u0026Plug.Conn.send_resp(\u00261, 200, \"second\"))\n\n{:ok, \"first\"} = fetch_request()\n{:ok, \"second\"} = fetch_request()\n```\n\nPlugs can be added to the pipeline with `TestServer.plug/1`. All plugs will run before any routes are matched. `Plug.Conn.fetch_query_params/1` is used if no plugs are set.\n\n```elixir\nTestServer.plug(fn conn -\u003e\n  Plug.Conn.fetch_query_params(conn)\nend)\n\nTestServer.plug(fn conn -\u003e\n  {:ok, body, _conn} = Plug.Conn.read_body(conn, [])\n\n  %{conn | body_params: Jason.decode!(body)}\nend)\n\nTestServer.plug(MyPlug)\n```\n\n### HTTPS\n\nBy default the test server is set up to serve plain HTTP. HTTPS can be enabled with the `:scheme` option when calling `TestServer.start/1`.\n\nCustom SSL certificates can also be used by defining the `:tls` option:\n\n```elixir\nTestServer.start(scheme: :https, tls: [keyfile: key, certfile: cert])\n```\n\nA self-signed certificate suite is automatically generated if you don't set the `:tls` options:\n\n```elixir\nTestServer.start(scheme: :https)\n\nreq_opts = [\n  connect_options: [\n    transport_opts: [cacerts: TestServer.x509_suite().cacerts],\n    protocols: [:http2]\n  ]\n]\n\nassert {:ok, %Req.Response{status: 200, body: \"HTTP/2\"}} =\n        Req.get(TestServer.url(), req_opts)\n```\n\n### WebSocket\n\nWebSocket endpoint can be set up by calling `TestServer.websocket_init/2`. By default, `TestServer.websocket_handle/2` will echo the message received. Messages can be send from the test server with `TestServer.websocket_info/2`.\n\n```elixir\ntest \"WebSocketClient\" do\n  {:ok, socket} = TestServer.websocket_init(\"/ws\")\n\n  :ok = TestServer.websocket_handle(socket)\n  :ok = TestServer.websocket_handle(socket, to: fn {:text, \"ping\"}, state -\u003e {:reply, {:text, \"pong\"}, state} end)\n  :ok = TestServer.websocket_handle(socket, match: fn {:text, message}, _state -\u003e message == \"hi\")\n\n  {:ok, client} = WebSocketClient.start_link(TestServer.url(\"/ws\"))\n\n  :ok = WebSocketClient.send(client, \"hello\")\n  {:ok, \"hello\"} = WebSocketClient.receive(client)\n\n  :ok = WebSocketClient.send(client, \"ping\")\n  {:ok, \"pong\"} = WebSocketClient.receive(client)\n\n  :ok = WebSocketClient.send(\"hi\")\n  {:ok, \"hi\"} = WebSocketClient.receive(client)\n\n  :ok = TestServer.websocket_info(socket, fn state -\u003e {:reply, {:text, \"ping\"}, state} end)\n  {:ok, \"ping\"} = WebSocketClient.receive(client)\nend\n```\n\n*Note: WebSocket is not supported by the `:httpd` adapter.*\n\n### HTTP Server Adapter\n\nTestServer supports `Bandit`, `Plug.Cowboy`, and `:httpd` out of the box. The HTTP adapter will be selected in this order depending which is available in the dependencies. You can also explicitly set the http server in the configuration when calling `TestServer.start/1`:\n\n```elixir\nTestServer.start(http_server: {TestServer.HTTPServer.Bandit, []})\n```\n\nYou can create your own plug based HTTP Server Adapter by using the `TestServer.HTTPServer` behaviour.\n\n### IPv6\n\nUse the `:ipfamily` option to test with IPv6 when starting the test server with `TestServer.start/1`:\n\n```elixir\nTestServer.start(ipfamily: :inet6)\n\nassert :ok =\n          TestServer.add(\"/\",\n            to: fn conn -\u003e\n              assert conn.remote_ip == {0, 0, 0, 0, 0, 65_535, 32_512, 1}\n\n              Plug.Conn.resp(conn, 200, \"OK\")\n            end\n          )\n```\n\n\u003c!-- MDOC !--\u003e\n\n## Installation\n\nAdd `test_server` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:test_server, \"~\u003e 0.1.20\", only: [:test]}\n  ]\nend\n```\n\n## LICENSE\n\n(The MIT License)\n\nCopyright (c) 2022 Dan Schultzer \u0026 the Contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanschultzer%2Ftest_server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanschultzer%2Ftest_server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanschultzer%2Ftest_server/lists"}