Salemove.HttpClientCase (salemove_http_client v2.1.0-rc.1)

This case template can be used to easily mock Tesla responses without messing up manually with Tesla.Env using helper allow_http_request/1 and assert_requested/2 macro.

Client adapter must be set to Tesla.Mock.

Example

# in config/test.exs

config :my_app, GithubClient,
  base_url: "https://api.github.com/",
  adapter: Tesla.Mock

defmodule GithubClient do
  use Salemove.HttpClient, Application.get_env(:my_app, __MODULE__)
end

defmodule GithubClientTest do
  use Salemove.HttpClientCase

  test "sends request" do
    allow_http_request fn env ->
      env
      |> status(200)
      |> json(%{status: "ok"})
    end

    assert {:ok, %Salemove.HttpClient.Response{body: response}} = GithubClient.get("/")

    assert_requested %{url: "https://api.github.com/"}
  end
end

Link to this section Summary

Functions

Setup mock for the current test

Verify that previously set up HTTP mock has been called

Set body (as string) for mocked response

Add arbitrary header to the mocked response

Set JSON body for mocked response

Set status for mocked response

Link to this section Functions

Link to this function

allow_http_request(mock_func)

Setup mock for the current test

Link to this macro

assert_requested(pattern, timeout \\ 100)

(macro)

Verify that previously set up HTTP mock has been called

Link to this function

body(env, body)

Set body (as string) for mocked response

Link to this function

header(env, name, value)

Add arbitrary header to the mocked response

Link to this function

json(env, body)

Set JSON body for mocked response

Link to this function

status(env, status)

Set status for mocked response