1

I am mocking an HTTP call (using Tesla) with Mox like this:

  test "Token expiry is handled by re-applying for a token on the fly", %{conn: conn} do
    TeslaMock
    |> expect(:call, 8, fn env, _opts ->
      case env.url do
        "http://fake.server.com/v1/token" ->
          %{env | status: 200, body: Poison.encode!(
                     %{token_type: "Bearer",
                     access_token: "cHBtKbMDNdc3uX2LIhCy9eVwkodhEc87s6e5Fk0lRS",
                     expires_in: 86400})
          }
        "http://fake.server.com/v1/system" ->
          %{env | status: 200, body: Poison.encode!(%{uptime: 10})}
      end
    end)

    assert json_response(get(conn, "/v1/system"), 200) == %{"uptime" => 10}

I need the fake server to first reply: 401 token expired to /v1/system, my Module issues /v1/token, and then retries /v1/system and succeeds. What's a good way to make this happen?

raarts
  • 2,711
  • 4
  • 25
  • 45

1 Answers1

0

You can use an Agent to keep track of the number of requests that have been made to /v1/system. Then, if it's the first request, return 401, otherwise 200.

{:ok, counter} = Agent.start_link(fn -> 0 end)

TeslaMock
|> expect(:call, 8, fn env, _opts ->
  case env.url do
    "http://fake.server.com/v1/token" ->
      ...
    "http://fake.server.com/v1/system" ->
      # Increment the counter.
      Agent.update(counter, &(&1 + 1))
      if Agent.get(counter, &(&1)) == 1 do
        # First request.
        %{env | status: 401, body: ...}
      else
        # Second or later request.
        %{env | status: 200, body: Poison.encode!(%{uptime: 10})}
      end
  end
end)
Dogbert
  • 212,659
  • 41
  • 396
  • 397