Plug.Test
Conveniences for testing plugs.
This module can be used in your test cases, like this:
use ExUnit.Case, async: true
use Plug.Test
Using this module will:
- import all the functions from this module
- import all the functions from the
Plug.Conn
module
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_cookies(new_conn, old_conn) | Moves cookies from a connection into a new connection for subsequent requests |
Functions
Specs:
- conn(String.Chars.t, binary, params, [{:headers, Plug.Conn.headers}]) :: Plug.Conn.t
Creates a test connection.
The request method
and path
are required arguments. method
may be any
value that implements to_string/1
and it will properly converted and
normalized (e.g., :get
or "post"
).
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 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. However, this option is now deprecated in favour of using
put_req_header/3
instead.
Examples
conn(:get, "/foo", "bar=10")
conn(:post, "/")
conn("patch", "/", "", headers: [{"content-type", "application/json"}])
Specs:
- delete_req_cookie(Plug.Conn.t, binary) :: Plug.Conn.t
Deletes a request cookie.
Specs:
- delete_req_header(Plug.Conn.t, binary) :: Plug.Conn.t
Deletes a request header.
Specs:
- put_req_cookie(Plug.Conn.t, binary, binary) :: Plug.Conn.t
Puts a request cookie.
Specs:
- put_req_header(Plug.Conn.t, binary, binary) :: Plug.Conn.t
Puts a new request header.
Previous entries of the same header are overridden.
Specs:
- recycle_cookies(Plug.Conn.t, Plug.Conn.t) :: Plug.Conn.t
Moves cookies from a 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 where cookies are always passed
forward, and returns the new version of new_conn
.