fake_server v1.1.0 FakeServer

Manage HTTP servers on your tests

Link to this section Summary

Functions

Returns the current server address

Returns the current server environment

Returns the number of requests made to the server

Adds a route to a server and the responses that will be given when a request reaches that route

Link to this section Functions

Link to this macro address() (macro)

Returns the current server address.

You can only call FakeServer.address/0 inside test_with_server/3.

Usage

  test_with_server "Getting the server address", [port: 5001] do
    assert FakeServer.address == "127.0.0.1:5001"
  end
Link to this macro env() (macro)

Returns the current server environment.

You can only call FakeServer.env/0 inside test_with_server/3.

Usage

  test_with_server "Getting the server env", [port: 5001] do
    assert FakeServer.env.ip == "127.0.0.1"
    assert FakeServer.env.port == 5001
  end
Link to this macro hits() (macro)

Returns the number of requests made to the server.

You can only call FakeServer.hits/0 inside test_with_server/3.

Usage

test_with_server "counting server hits" do
  route "/", do: Response.ok
  assert FakeServer.hits == 0
  HTTPoison.get! FakeServer.address <> "/"
  assert FakeServer.hits == 1
  HTTPoison.get! FakeServer.address <> "/"
  assert FakeServer.hits == 2
end
Link to this macro route(path, list) (macro)

Adds a route to a server and the responses that will be given when a request reaches that route.

Responses can be given in three formats:

  1. A single FakeServer.HTTP.Response. In this case, this response will be given by the server on the first request. The following requests will be replied with the default_response.

  2. A list of FakeServer.HTTP.Response. In this case, each request will be replied with the first element of the list, which is then removed. When the list is empty, the requests will be replied with default_respose.

  3. A FakeController. In this case, the responses will be given dynamically, according to request parameters. For more details see FakeController.

Link to this macro test_with_server(test_description, opts \\ [], list) (macro)

Runs a test with an HTTP server.

If you need an HTTP server on your test, just write it using test_with_server/3 instead of ExUnit.Case.test/3. Their arguments are similar: A description (the test_description argument), the implementation of the test case itself (the list argument) and an optional list of parameters (the opts argument).

The server will start just before your test block and will stop just before the test exits. Each test_with_server/3 has its own server. By default, all servers will start in a random unused port, which allows you to run your tests with ExUnit.Case async: true option enabled.

Environment

FakeServer defines an environment for each test_with_server/3. This environment is stored inside a FakeServer.Env structure, which has the following fields:

  • :ip: the current server IP
  • :port: the current server port
  • :routes: the list of server routes
  • :hits: the number of requests made to the server

To access this environment, you can use FakeServer.env/0, which returns the environment for the current test. For convenience, you can also use the FakeServer.address/0 or FakeServer.hits/0.

Server options

You can set some options to the server before it starts using the opts params. The following options are accepted:

:default_response: The response that will be given by the server if a route has no responses configured. :port: The port that the server will listen.

Usage:

defmodule SomeTest do
  use ExUnit.Case, async: true
  import FakeServer
  alias FakeServer.HTTP.Response

  test_with_server "without configured routes will always return 404 and hits will not be updated" do
    response = HTTPoison.get! FakeServer.address <> "/"
    assert response.status_code == 404
    response = HTTPoison.get! FakeServer.address <> "/test"
    assert response.status_code == 404
    response = HTTPoison.get! FakeServer.address <> "/test/1"
    assert response.status_code == 404
    assert FakeServer.env.hits == 0
  end

  test_with_server "server port configuration", [port: 5001] do
    assert FakeServer.env.port == 5001
    assert FakeServer.address == "127.0.0.1:5001"
  end

  test_with_server "setting a default response", [default_response: Response.forbidden] do
    route "/test", do: Response.bad_request

    response = HTTPoison.get! FakeServer.address <> "/test"
    assert response.status_code == 400

    response = HTTPoison.get! FakeServer.address <> "/test"
    assert response.status_code == 403
  end

  test_with_server "adding a route" do
    route "/", do: FakeServer.HTTP.Response.bad_request

    response = HTTPoison.get! FakeServer.address <> "/"
    assert response.status_code == 400
  end

  test_with_server "save server hits in the environment" do
    route "/", do: Response.ok
    assert FakeServer.hits == 0

    HTTPoison.get! FakeServer.address <> "/"
    assert FakeServer.hits == 1

    HTTPoison.get! FakeServer.address <> "/"
    assert FakeServer.hits == 2
  end

  test_with_server "adding body and headers to the response" do
    route "/", do: Response.ok(~s<{"response": "ok"}>, [{'x-my-header', 'fake-server'}])

    response = HTTPoison.get! FakeServer.address <> "/"
    assert Enum.any?(response.headers, fn(header) -> header == {"x-my-header", "fake-server"} end)
  end
end