Dynamo.HTTP.Case
A bunch of helpers to make it easy to test Dynamos and routers.
By default, these helpers are macros that dispatch directly to the registered endpoint. Here is an example:
defmodule MyAppTest do
use ExUnit.Case
use Dynamo.HTTP.Case
test :root_route do
conn = get("/")
assert conn.sent_body =~ %r/somevalue/
end
end
The default dynamo used in tests is Dynamo.under_Test
.
This can be changed in a specific test case using @endpoint
:
defmodule CustomRouterTest do
use ExUnit.Case
use Dynamo.HTTP.Case
@endpoint CustomRouter
test :route do
conn = get("/route")
assert conn.sent_body =~ %r/somevalue/
end
end
The connection used in such tests is the Dynamo.Connection.Test
which provides some test specific function.
Testing with sequential requests
In some cases, the same test may request different endpoints:
test :session do
conn = get("/put_session")
assert conn.sent_body =~ %r/somevalue/
conn = get(conn, "/set_session")
assert conn.sent_body =~ %r/othervalue/
end
The example above will automatically work, since
get
/post
/put
/patch
/delete
/options
recycles the connection before
each request.
When recycled, all response information previously set in the connection is cleaned and all cookies are moved from the response to the request. This allows state to be passed in between the different requests.
Notice though that recycling will clean up any information set in the connection:
test :session do
conn = get("/put_session")
assert conn.sent_body =~ %r/somevalue/
conn = conn.assign(:foo, :bar)
conn = get(conn, "/set_session")
assert conn.sent_body =~ %r/othervalue/
end
In the example above, the assign :foo
set before the request
won't be visible in the endpoint since it will be cleaned up.
This can be fixed by explicitly cleaning up the request:
conn = conn.recycle.assign(:foo, :bar)
If the connection was already recycled, it won't be recycled once again.
Finally, notice that all get
/post
/put
/patch
/delete
/options
macros
are simply a proxy to process/4
. So in case you want to dispatch
to different dynamos at the same time, process/4
may be useful.
Functions summary
conn(method, path, body \\ "") | Returns a connection built with the given method, path and body. |
process(endpoint, conn, method, path \\ nil) | Requests the given |
put_session_cookie(conn, session) | Writes a session cookie according to the current store to be used in the next request. This is the preferred way to set the session before a request. |
Macros summary
delete(arg1, arg2 \\ nil) | Does a DELETE request to the given path: |
get(arg1, arg2 \\ nil) | Does a GET request to the given path: |
options(arg1, arg2 \\ nil) | Does a OPTIONS request to the given path: |
patch(arg1, arg2 \\ nil) | Does a PATCH request to the given path: |
post(arg1, arg2 \\ nil) | Does a POST request to the given path and optionally body: |
put(arg1, arg2 \\ nil) | Does a PUT request to the given path: |
Functions
conn(method, path, body \\ "")
Returns a connection built with the given method, path and body.
process(endpoint, conn, method, path \\ nil)
Requests the given endpoint
with the given method
and path
.
And verifies if the endpoint returned a valid connection.
Examples
process MyDynamo, :get, "/foo"
process MyDynamo, conn, :get, "/foo"
put_session_cookie(conn, session)
Writes a session cookie according to the current store to be used in the next request. This is the preferred way to set the session before a request.
Macros
delete(arg1, arg2 \\ nil)
Does a DELETE request to the given path:
delete("/foo")
delete(conn, "/foo")
options(arg1, arg2 \\ nil)
Does a OPTIONS request to the given path:
options("/foo")
options(conn, "/foo")
patch(arg1, arg2 \\ nil)
Does a PATCH request to the given path:
patch("/foo")
patch(conn, "/foo")
post(arg1, arg2 \\ nil)
Does a POST request to the given path and optionally body:
post("/foo")
post(conn, "/foo")
post(conn, "/foo", "test body") # POSTs to `/foo` with `test body` body
post(conn, "/foo", [{"foo", "bar"}]) # POSTs to `/foo` with `foo=bar` body