Plug.Test

Conveniences for testing plugs

Examples

This module can be used in your test cases:

use ExUnit.Case, async: true
use Plug.Test

and it will:

* import all functions from this module
* import all functions from [`Plug.Conn`](Plug.Conn.html)
* alias [`Plug.Conn`](Plug.Conn.html) to `Conn`

Summary

conn(method, path, params_or_body \\ nil, opts \\ [])

Creates a test connection

delete_req_cookie(conn, key)

Deletes a request cookie

delete_req_header(conn, key)

Deletes a request header

put_req_cookie(conn, key, value)

Puts a request cookie

put_req_header(conn, key, value)

Puts a new request header

recycle(new_conn, old_conn)

Recycles data from old connection into a new connection for subsequent requests

Functions

conn(method, path, params_or_body \\ nil, opts \\ [])

Specs:

Creates a test connection.

The request method and path must be given as required arguments. method may be any value that implements to_string/1 and it will properly converted and normalized.

The params_or_body field must be one of:

  • nil - meaning there is no body;
  • a binary - containing a request body. For such cases, :headers must be given as option with a content-type;
  • a map or list - containing the parameters which will automatically set the content-type to multipart. The map or list may be contain other lists or maps and all entries will be normalized to string keys;

The only option supported so far is :headers which expects a list of headers.

delete_req_cookie(conn, key)

Specs:

Deletes a request cookie.

delete_req_header(conn, key)

Specs:

Deletes a request header.

put_req_cookie(conn, key, value)

Specs:

Puts a request cookie.

put_req_header(conn, key, value)

Specs:

Puts a new request header.

Previous entries of the same headers are removed.

recycle(new_conn, old_conn)

Specs:

Recycles data from old connection into a new connection for subsequent requests.

This function copies the cookie information in old_conn into new_conn, emulating multiple requests done by clients were cookies are always passed forward.