{"id":13509194,"url":"https://github.com/bernardolins/fake_server","last_synced_at":"2025-04-07T19:16:50.236Z","repository":{"id":60775478,"uuid":"67630107","full_name":"bernardolins/fake_server","owner":"bernardolins","description":"FakeServer integrates with ExUnit to make external APIs testing simpler","archived":false,"fork":false,"pushed_at":"2020-03-06T12:03:31.000Z","size":323,"stargazers_count":72,"open_issues_count":2,"forks_count":22,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-31T16:21:36.193Z","etag":null,"topics":["elixir","exunit","fake","fake-server","http","http-test","macros","stub","test"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bernardolins.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-07T17:45:53.000Z","updated_at":"2023-08-23T10:58:51.000Z","dependencies_parsed_at":"2022-10-04T17:02:16.694Z","dependency_job_id":null,"html_url":"https://github.com/bernardolins/fake_server","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bernardolins%2Ffake_server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bernardolins%2Ffake_server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bernardolins%2Ffake_server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bernardolins%2Ffake_server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bernardolins","download_url":"https://codeload.github.com/bernardolins/fake_server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247713258,"owners_count":20983683,"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","fake","fake-server","http","http-test","macros","stub","test"],"created_at":"2024-08-01T02:01:04.385Z","updated_at":"2025-04-07T19:16:50.205Z","avatar_url":"https://github.com/bernardolins.png","language":"Elixir","funding_links":[],"categories":["Testing","Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# FakeServer\n[![Build Status](https://travis-ci.org/bernardolins/fake_server.svg?branch=master)](https://travis-ci.org/bernardolins/fake_server)\n[![Coverage Status](https://coveralls.io/repos/github/bernardolins/fake_server/badge.svg?branch=master)](https://coveralls.io/github/bernardolins/fake_server?branch=master)\n[![Hex.pm](https://img.shields.io/hexpm/dt/fake_server.svg)](https://hex.pm/packages/fake_server)\n\nFakeServer is an HTTP server that simulates responses. It can be used in test and development environments, helping to validate the behavior of your application if there are errors or unexpected responses from some external HTTP service.\n\nIt provides the `test_with_server` macro to be used together with [ExUnit](https://hexdocs.pm/ex_unit/ExUnit.html), making it easier to write tests that need to request external services. Instead of creating a mock when you need make a request, you can use a real HTTP server that will reply a deterministic response. This way you can validate if your application can handle it.\n\n`FakeServer` can also be used through functions, when ExUnit is not available (in the console, for example).\n\n## Installation\n\n**Important**: From the version 2.0, FakeServer only supports cowboy 2.x. If you have cowboy 1.x as dependency, use FakeServer version 1.5.\n\nFakeServer is available on [Hex](https://hex.pm/packages/fake_server). First, add it to `mix.exs` as a test dependency:\n\n```elixir\ndef deps do\n  [\n    {:fake_server, \"~\u003e 2.1\", only: :test}\n  ]\nend\n```\n\nThen, start `fake_server` application on `test/test_helper.exs`.\n\n```elixir\n{:ok, _} = Application.ensure_all_started(:fake_server)\n```\n\n## Basic Usage\n\nFor more examples you can see the [docs](https://hexdocs.pm/fake_server/api-reference.html).\n\n### ExUnit\n\nFakeServer provides the macro `FakeServer.test_with_server`. It works like ExUnit's `test` macro, but before your test starts it will run an HTTP server in a random port (by default). The server will be available until test case is finished.\n\nYou can use the `FakeServer.route` macro to add a route and setup it's response. Use `FakeServer.http_address` to get the address of the server running in the current test. Each test will start its own HTTP server.\n\n```elixir\ndefmodule MyTest do\n  use ExUnit.Case\n  import FakeServer\n\n  test_with_server \"returns 404 if a request is made to a non-configured route\" do\n    response = HTTPoison.get!(\"#{FakeServer.address}/not/configured\")\n    assert response.status_code == 404\n  end\n\n  test_with_server \"when the response is a structure it returns the given response\" do\n    route \"/test\", Response.no_content!()\n    response = HTTPoison.get!(\"#{FakeServer.address}/test\")\n    assert response.status_code == 204\n  end\n\n  test_with_server \"when the response is a list it returns the first element of the list and removes it\" do\n    route \"/test\", [Response.ok!(), Response.no_content!()]\n    response = HTTPoison.get!(\"#{FakeServer.address}/test\")\n    assert response.status_code == 200\n    response = HTTPoison.get!(\"#{FakeServer.address}/test\")\n    assert response.status_code == 204\n  end\n\n  test_with_server \"when the response is a function it runs the function\" do\n    route \"/say/hi\", fn(_) -\u003e IO.puts \"HI!\" end\n    response = HTTPoison.get! \"#{FakeServer.address}/say/hi\"\n  end\n\n  test_with_server \"computes hits for the corresponding route\" do\n    route \"/test\", Response.no_content!()\n    assert hits() == 0\n    assert hits(\"/test\") == 0\n    HTTPoison.get!(\"#{FakeServer.address}/test\")\n    assert hits() == 1\n    assert hits(\"/test\") == 1\n  end\n\n  test_with_server \"supports inline port configuration\", [port: 55_000] do\n    assert FakeServer.port() == 55_000\n  end\nend\n```\n#### Setup Server\n\nIf you need to do some setup before every `test_with_server` tests, you can define a `setup_test_with_server/1` function in your module. This function will receive a %FakeServer.Instance{} struct as a parameter.\n\n\n### Standalone Server\n\nYou can use a fake server without ExUnit with `FakeServer.start` and other helper functions available. Functions work similar to macros, but can be used outside the tests.\n\n```elixir\niex\u003e {:ok, pid} = FakeServer.start(:my_server)\n{:ok, #PID\u003c0.302.0\u003e}\n\niex\u003e :ok = FakeServer.put_route(pid, \"/say/hi\", fn(_) -\u003e IO.puts \"HI!\" end)\n:ok\n\niex\u003e {:ok, port} = FakeServer.port(:my_server)\n{:ok, 62698}\n```\nFor more examples you can see the [docs](https://hexdocs.pm/fake_server/api-reference.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbernardolins%2Ffake_server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbernardolins%2Ffake_server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbernardolins%2Ffake_server/lists"}