HTTPower.Test (HTTPower v0.16.0)

View Source

Adapter-agnostic testing utilities for HTTPower.

This module provides a testing interface that works independently of which adapter (Req, Tesla) you have installed. It allows you to write tests that mock HTTP requests without coupling to adapter-specific test utilities.

Example

defmodule MyApp.ApiClientTest do
  use ExUnit.Case

  setup do
    HTTPower.Test.setup()
  end

  test "fetches users" do
    HTTPower.Test.stub(fn conn ->
      case {conn.method, conn.request_path} do
        {"GET", "/users"} ->
          HTTPower.Test.json(conn, %{users: ["alice", "bob"]})

        {"POST", "/users"} ->
          HTTPower.Test.json(conn, %{created: true})
      end
    end)

    {:ok, response} = HTTPower.get("https://api.example.com/users")

    assert response.status == 200
    assert response.body == %{"users" => ["alice", "bob"]}
  end
end

Benefits

  • Adapter independence: No need to know if you're using Req or Tesla
  • Simple API: One stub/1 function for all mocking needs
  • Zero coupling: Doesn't depend on Req.Test or Tesla.Mock
  • Convenient helpers: json/2, html/2, text/2 for responses

Summary

Functions

Sends an HTML response with the given data.

Sends a JSON response with the given data.

Sets up HTTPower.Test for the current test.

Registers a stub function to handle HTTP requests in tests.

Sends a text response with the given data.

Simulates a network transport error.

Functions

html(conn, data, opts \\ [])

Sends an HTML response with the given data.

Options

  • :status - HTTP status code (default: 200)

Examples

HTTPower.Test.html(conn, "<h1>Hello</h1>")
HTTPower.Test.html(conn, "<h1>Not Found</h1>", status: 404)

json(conn, data, opts \\ [])

Sends a JSON response with the given data.

Options

  • :status - HTTP status code (default: 200)

Examples

HTTPower.Test.json(conn, %{success: true})
HTTPower.Test.json(conn, %{error: "not found"}, status: 404)

setup()

Sets up HTTPower.Test for the current test.

Call this in your test setup to enable HTTP mocking for that test.

Example

setup do
  HTTPower.Test.setup()
end

stub(fun)

Registers a stub function to handle HTTP requests in tests.

The stub function receives a Plug.Conn struct and should return a Plug.Conn with the response set using helper functions like json/2, html/2, or text/2.

Example

HTTPower.Test.stub(fn conn ->
  case {conn.method, conn.request_path} do
    {"GET", "/weather"} ->
      HTTPower.Test.json(conn, %{temp: 25, condition: "sunny"})

    {"POST", "/users"} ->
      HTTPower.Test.json(conn, %{created: true}, status: 201)

    _ ->
      HTTPower.Test.json(conn, %{error: "not found"}, status: 404)
  end
end)

text(conn, data, opts \\ [])

Sends a text response with the given data.

Options

  • :status - HTTP status code (default: 200)

Examples

HTTPower.Test.text(conn, "Hello, World!")
HTTPower.Test.text(conn, "Not Found", status: 404)

transport_error(conn, reason)

Simulates a network transport error.

This function allows you to test how your application handles various network failures like timeouts, connection errors, and protocol issues.

Supported error reasons

  • :timeout - Request timeout
  • :closed - Connection closed
  • :econnrefused - Connection refused
  • :nxdomain - DNS resolution failed

Examples

HTTPower.Test.stub(fn conn ->
  HTTPower.Test.transport_error(conn, :timeout)
end)

{:error, error} = HTTPower.get("https://api.example.com/slow")
assert error.reason == :test_transport_error
assert error.message =~ "timeout"