annon_api v0.16.2 Annon.ConnCase
This module defines the test case to be used by tests that require setting up a connection.
Credentials
Most of source code is a copy-paste from Phoenix.ConnTest
,
it’s already contains great tests suite, but we don’t
want to depend on Phoenix.
Endpoint testing
Annon.ConnCase
typically works against routers. That’s
the preferred way to test anything that your router dispatches
to.
conn = get build_conn(), "/"
assert conn.resp_body =~ "Welcome!"
conn = post build_conn(), "/login", [username: "john", password: "doe"]
assert conn.resp_body =~ "Logged in!"
As in your application, the connection is also the main abstraction
in testing. build_conn()
returns a new connection and functions in this
module can be used to manipulate the connection before dispatching
to the router.
For example, one could set the accepts header for json requests as follows:
build_conn()
|> put_req_header("accept", "application/json")
|> get("/")
The router being tested is accessed via the @router
module
attribute.
Controller testing
The functions in this module can also be used for controller testing. While router testing is preferred over controller testing as a controller often depends on the pipelines invoked in the router and before, unit testing controllers may be helpful in some situations.
For such cases, just pass an atom representing the action to dispatch:
conn = get build_conn(), :index
assert conn.resp_body =~ "Welcome!"
Link to this section Summary
Functions
Asserts an error was wrapped and sent with the given status
Creates a connection to be used in upcoming requests
Creates a connection to be used in upcoming requests with a preset method, path and body
Deprecated version of conn/0. Use build_conn/0 instead
Dispatches to the current router
Dispatches to the current router
Deletes a request cookie
Dispatches the connection to the given router
Dispatches to the current router
Dispatches to the current router
Asserts the given status code, that we have an html response and returns the response body if one was set or sent
Asserts the given status code, that we have an json response and returns the decoded JSON response if one was set or sent
Dispatches to the current router
Dispatches to the current router
Dispatches to the current router with JSON-encoded params
Dispatches to the current router
Dispatches to the current router with JSON-encoded params
Dispatches to the current router
Dispatches to the current router with JSON-encoded params
Puts a request cookie
Recycles the connection
Returns the location header from the given redirect response
Asserts the given status code and returns the response body if one was set or sent
Returns the content type as long as it matches the given format
Asserts the given status code, that we have an text response and returns the response body if one was set or sent
Dispatches to the current router
Link to this section Functions
Asserts an error was wrapped and sent with the given status.
Useful for testing actions that you expect raise an error and have the response wrapped in an HTTP status, with content usually rendered by your MyApp.ErrorView.
The function accepts a status either as an integer HTTP status or
atom, such as 404
or :not_found
. If an error is raised, a
3-tuple of the wrapped response is returned matching the
status, headers, and body of the response:
{404, [{"content-type", "text/html"} | _], "Page not found"}
Examples
assert_error_sent :not_found, fn ->
get build_conn(), "/users/not-found"
end
response = assert_error_sent 404, fn ->
get build_conn(), "/users/not-found"
end
assert {404, [_h | _t], "Page not found"} = response
Creates a connection to be used in upcoming requests.
Creates a connection to be used in upcoming requests with a preset method, path and body.
This is useful when a specific connection is required for testing a plug or a particular function.
Deprecated version of conn/0. Use build_conn/0 instead
Dispatches to the current router.
See dispatch/5
for more information.
Dispatches to the current router.
See dispatch/5
for more information.
delete_req_cookie(Plug.Conn.t(), binary()) :: Plug.Conn.t()
Deletes a request cookie.
Dispatches the connection to the given router.
When invoked via get/3
, post/3
and friends, the router
is automatically retrieved from the @router
module
attribute, otherwise it must be given as an argument.
The connection will be configured with the given method
,
path_or_action
and params_or_body
.
If path_or_action
is a string, it is considered to be the
request path and stored as so in the connection. If an atom,
it is assumed to be an action and the connection is dispatched
to the given action.
Parameters and body
This function, as well as get/3
, post/3
and friends, accepts the
request body or parameters as last argument:
get build_conn(), "/", some: "param"
get build_conn(), "/", "some=param&url=encoded"
The allowed values are:
nil
- meaning there is no bodya binary - containing a request body. For such cases,
:headers
must be given as option with a content-typea 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
a struct - unlike other maps, a struct will be passed through as-is without normalizing its entries
Dispatches to the current router.
See dispatch/5
for more information.
Dispatches to the current router.
See dispatch/5
for more information.
html_response(Plug.Conn.t(), status :: integer() | atom()) :: String.t() | no_return()
Asserts the given status code, that we have an html response and returns the response body if one was set or sent.
Examples
assert html_response(conn, 200) =~ "<html>"
Asserts the given status code, that we have an json response and returns the decoded JSON response if one was set or sent.
Examples
body = json_response(conn, 200)
assert "can't be blank" in body["errors"]
Dispatches to the current router.
See dispatch/5
for more information.
Dispatches to the current router.
See dispatch/5
for more information.
Dispatches to the current router with JSON-encoded params.
See dispatch/5
for more information.
Dispatches to the current router.
See dispatch/5
for more information.
Dispatches to the current router with JSON-encoded params.
See dispatch/5
for more information.
Dispatches to the current router.
See dispatch/5
for more information.
Dispatches to the current router with JSON-encoded params.
See dispatch/5
for more information.
put_req_cookie(Plug.Conn.t(), binary(), binary()) :: Plug.Conn.t()
Puts a request cookie.
Recycles the connection.
Recycling receives a connection and returns a new connection, containing cookies and relevant information from the given one.
This emulates behaviour performed by browsers where cookies returned in the response are available in following requests.
Note recycle/1
is automatically invoked when dispatching
to the router, unless the connection has already been
recycled.
redirected_to(Plug.Conn.t(), status :: non_neg_integer()) :: Plug.Conn.t()
Returns the location header from the given redirect response.
Raises if the response does not match the redirect status code (defaults to 302).
Examples
assert redirected_to(conn) =~ "/foo/bar"
assert redirected_to(conn, 301) =~ "/foo/bar"
assert redirected_to(conn, :moved_permanently) =~ "/foo/bar"
response(Plug.Conn.t(), status :: integer() | atom()) :: binary() | no_return()
Asserts the given status code and returns the response body if one was set or sent.
Examples
conn = get build_conn(), "/"
assert response(conn, 200) =~ "hello world"
response_content_type(Plug.Conn.t(), atom()) :: String.t() | no_return()
Returns the content type as long as it matches the given format.
Examples
# Assert we have an html response with utf-8 charset
assert response_content_type(conn, :html) =~ "charset=utf-8"
text_response(Plug.Conn.t(), status :: integer() | atom()) :: String.t() | no_return()
Asserts the given status code, that we have an text response and returns the response body if one was set or sent.
Examples
assert text_response(conn, 200) =~ "hello"
Dispatches to the current router.
See dispatch/5
for more information.