PlugTestHelpers

Helpers to test your Plugs with ExUnit.

To import the helpers into your test case, simply add use PlugTestHelpers:

defmodule do
  use ExUnit.Case, async: true

  use Plug.Test
  use PlugTestHelpers

  test "home page" do
    conn = conn(:get, "/")
    conn = MyPlug.call(conn, @opts)
    assert_status 200
  end

end

Important: each assert_* expect the current HTTP connection (a Plug.Conn struct) to be bound to a variable named conn.

Source

Summary

assert_body(expected_body)

Passes if the current HTTP response body is equal to the given string

assert_body_match(regex)

Passes if the current HTTP response body matches the given regex

assert_header(key, expected_value)

Passes if the current HTTP response has a header set to the given value

assert_header_match(key, regex)

Passes if the current HTTP response has a header matching the given regex

assert_redirect()

Passes if the current HTTP response is a redirect

assert_redirect(expected_url)

Passes if the current HTTP response is a redirect to the given URL

assert_status(expected_status)

Passes if the current HTTP response code matches the given expected status code

Macros

assert_body(expected_body)

Passes if the current HTTP response body is equal to the given string

Source
assert_body_match(regex)

Passes if the current HTTP response body matches the given regex

Source
assert_header(key, expected_value)

Passes if the current HTTP response has a header set to the given value

Source
assert_header_match(key, regex)

Passes if the current HTTP response has a header matching the given regex

Source
assert_redirect()

Passes if the current HTTP response is a redirect

Source
assert_redirect(expected_url)

Passes if the current HTTP response is a redirect to the given URL

Source
assert_status(expected_status)

Passes if the current HTTP response code matches the given expected status code.

Status code can be either:

  • an integer (200, 404, …)
  • an atom (one of :ok, :success, :created, :pending, :no_content, :partial, :permanent_redirect, :redirect, :bad_request, :authentication_failed, :forbidden, :not_found, :not_acceptable or :internal_server_error)
Source