Skip to content
Browse files

Allow setting the resp status and body individually

  • Loading branch information...
1 parent 17bd67f commit 4d131fa5a64f00a81b73f9648597f521d2d890f9 @devinus committed Sep 28, 2012
Showing with 36 additions and 0 deletions.
  1. +10 −0 lib/dynamo/http.ex
  2. +8 −0 lib/dynamo/http/response.ex
  3. +18 −0 test/dynamo/http/test_test.exs
View
10 lib/dynamo/http.ex
@@ -129,11 +129,21 @@ defmodule Dynamo.HTTP do
defcallback status(conn)
@doc """
+ Sets the response status and changes the state to `:set`.
+ """
+ defcallback status(status, conn)
+
+ @doc """
Returns the response body if one was set.
"""
defcallback resp_body(conn)
@doc """
+ Sets the response body and changes the state to `:set`.
+ """
+ defcallback resp_body(body, conn)
+
+ @doc """
Sets a response to the given status and body. The
response will only be sent when `send` is called.
View
8 lib/dynamo/http/response.ex
@@ -16,10 +16,18 @@ defmodule Dynamo.HTTP.Response do
status
end
+ def status(status, conn) when is_integer(status) do
+ connection(conn, status: status, state: :set)
+ end
+
def resp_body(connection(resp_body: resp_body)) do
resp_body
end
+ def resp_body(body, conn) do
+ connection(conn, resp_body: body, state: :set)
+ end
+
def resp(status, body, conn) when is_integer(status) do
connection(conn,
status: status,
View
18 test/dynamo/http/test_test.exs
@@ -138,6 +138,24 @@ defmodule Dynamo.HTTP.TestTest do
conn
end
+ test :status do
+ conn = conn(:GET, "/")
+ assert conn.state == :unset
+
+ conn = conn.status(201)
+ assert conn.state == :set
+ assert conn.status == 201
+ end
+
+ test :resp_body do
+ conn = conn(:GET, "/")
+ assert conn.state == :unset
+
+ conn = conn.resp_body("OK")
+ assert conn.state == :set
+ assert conn.resp_body == "OK"
+ end
+
test :resp do
conn = conn(:GET, "/")
assert conn.state == :unset

0 comments on commit 4d131fa

Please sign in to comment.
Something went wrong with that request. Please try again.