Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ressurect conn.resp(status, body)

  • Loading branch information...
commit cd1c01d8271ab42bb5d30c258034fabf693e3d41 1 parent f9d8947
@devinus authored
View
9 lib/dynamo/http.ex
@@ -210,6 +210,15 @@ defmodule Dynamo.HTTP do
defcallback resp_content_type(content_type, conn)
@doc """
+ Sets a response to the given status and body. The
+ response will only be sent when `send` is called.
+
+ After calling this function, the state changes to `:set`,
+ both `status` and `resp_body` are set.
+ """
+ defcallback resp(status, body, conn)
+
+ @doc """
A shortcut to `conn.send(conn.status, conn.resp_body)`.
"""
defcallback send(conn)
View
8 lib/dynamo/http/behaviour.ex
@@ -191,6 +191,14 @@ defmodule Dynamo.HTTP.Behaviour do
connection(conn, resp_charset: resp_charset)
end
+ def resp(status, body, conn) when is_integer(status) do
+ connection(conn,
+ status: status,
+ resp_body: body,
+ state: :set
+ )
+ end
+
def send(connection(status: status, resp_body: body) = conn) do
send(status, body, conn)
end
View
20 test/dynamo/cowboy/http_test.exs
@@ -334,6 +334,26 @@ defmodule Dynamo.Cowboy.HTTPTest do
assert { 500, _, _ } = request :get, "/invalid_sendfile"
end
+ def resp(conn) do
+ assert conn.state == :unset
+
+ conn = conn.resp(201, "OK")
+ assert conn.state == :set
+ assert conn.status == 201
+ assert conn.resp_body == "OK"
+
+ conn = conn.resp(302, "Redirected")
+ assert conn.state == :set
+ assert conn.status == 302
+ assert conn.resp_body == "Redirected"
+
+ conn
+ end
+
+ test :resp do
+ assert { 302, _, "Redirected" } = request :get, "/resp"
+ end
+
## Misc
def forward_to(conn) do
Please sign in to comment.
Something went wrong with that request. Please try again.