Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bernardolins/fake_server
FakeServer integrates with ExUnit to make external APIs testing simpler
https://github.com/bernardolins/fake_server
elixir exunit fake fake-server http http-test macros stub test
Last synced: 2 days ago
JSON representation
FakeServer integrates with ExUnit to make external APIs testing simpler
- Host: GitHub
- URL: https://github.com/bernardolins/fake_server
- Owner: bernardolins
- License: apache-2.0
- Created: 2016-09-07T17:45:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-03-06T12:03:31.000Z (almost 5 years ago)
- Last Synced: 2025-01-02T03:11:59.875Z (10 days ago)
- Topics: elixir, exunit, fake, fake-server, http, http-test, macros, stub, test
- Language: Elixir
- Homepage:
- Size: 315 KB
- Stars: 72
- Watchers: 4
- Forks: 22
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - FakeServer is an HTTP server that simulates response and makes testing external APIs easier. (Testing)
- fucking-awesome-elixir - fake_server - FakeServer is an HTTP server that simulates response and makes testing external APIs easier. (Testing)
- awesome-elixir - fake_server - FakeServer is an HTTP server that simulates response and makes testing external APIs easier. (Testing)
README
# FakeServer
[![Build Status](https://travis-ci.org/bernardolins/fake_server.svg?branch=master)](https://travis-ci.org/bernardolins/fake_server)
[![Coverage Status](https://coveralls.io/repos/github/bernardolins/fake_server/badge.svg?branch=master)](https://coveralls.io/github/bernardolins/fake_server?branch=master)
[![Hex.pm](https://img.shields.io/hexpm/dt/fake_server.svg)](https://hex.pm/packages/fake_server)FakeServer 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.
It 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.
`FakeServer` can also be used through functions, when ExUnit is not available (in the console, for example).
## Installation
**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.
FakeServer is available on [Hex](https://hex.pm/packages/fake_server). First, add it to `mix.exs` as a test dependency:
```elixir
def deps do
[
{:fake_server, "~> 2.1", only: :test}
]
end
```Then, start `fake_server` application on `test/test_helper.exs`.
```elixir
{:ok, _} = Application.ensure_all_started(:fake_server)
```## Basic Usage
For more examples you can see the [docs](https://hexdocs.pm/fake_server/api-reference.html).
### ExUnit
FakeServer 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.
You 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.
```elixir
defmodule MyTest do
use ExUnit.Case
import FakeServertest_with_server "returns 404 if a request is made to a non-configured route" do
response = HTTPoison.get!("#{FakeServer.address}/not/configured")
assert response.status_code == 404
endtest_with_server "when the response is a structure it returns the given response" do
route "/test", Response.no_content!()
response = HTTPoison.get!("#{FakeServer.address}/test")
assert response.status_code == 204
endtest_with_server "when the response is a list it returns the first element of the list and removes it" do
route "/test", [Response.ok!(), Response.no_content!()]
response = HTTPoison.get!("#{FakeServer.address}/test")
assert response.status_code == 200
response = HTTPoison.get!("#{FakeServer.address}/test")
assert response.status_code == 204
endtest_with_server "when the response is a function it runs the function" do
route "/say/hi", fn(_) -> IO.puts "HI!" end
response = HTTPoison.get! "#{FakeServer.address}/say/hi"
endtest_with_server "computes hits for the corresponding route" do
route "/test", Response.no_content!()
assert hits() == 0
assert hits("/test") == 0
HTTPoison.get!("#{FakeServer.address}/test")
assert hits() == 1
assert hits("/test") == 1
endtest_with_server "supports inline port configuration", [port: 55_000] do
assert FakeServer.port() == 55_000
end
end
```
#### Setup ServerIf 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.
### Standalone Server
You 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.
```elixir
iex> {:ok, pid} = FakeServer.start(:my_server)
{:ok, #PID<0.302.0>}iex> :ok = FakeServer.put_route(pid, "/say/hi", fn(_) -> IO.puts "HI!" end)
:okiex> {:ok, port} = FakeServer.port(:my_server)
{:ok, 62698}
```
For more examples you can see the [docs](https://hexdocs.pm/fake_server/api-reference.html).