Skip to content
Browse files

Support fetching request body

  • Loading branch information...
1 parent 17bd67f commit e94fcda6b7cfadb3ba0b0de45f90a93a1c1635f8 @devinus committed Sep 28, 2012
View
13 lib/dynamo/cowboy/http.ex
@@ -12,7 +12,7 @@ defmodule Dynamo.Cowboy.HTTP do
Record.defmacros __ENV__, :connection,
[ :req, :path_info_segments, :script_name_segments, :req_headers,
:params, :cookies, :resp_headers, :resp_cookies, :assigns, :status,
- :method, :original_method, # :res_charset, :res_type, :session, :req_body,
+ :method, :original_method, :req_body, # :res_charset, :res_type, :session,
:resp_body, :state ]
use Dynamo.HTTP.Paths
@@ -26,7 +26,7 @@ defmodule Dynamo.Cowboy.HTTP do
{ verb, req } = R.method(req)
{ binary, _ } = R.path req
- { segments, _, _} = :cowboy_dispatcher.split_path(binary,
+ { segments, _, _} = :cowboy_dispatcher.split_path(binary,
fn(bin) -> :cowboy_http.urldecode(bin, :crash) end)
connection(
@@ -60,7 +60,7 @@ defmodule Dynamo.Cowboy.HTTP do
def path_segments(connection(req: req)) do
{ binary, _ } = R.path req
- { segments, _, _} = :cowboy_dispatcher.split_path(binary,
+ { segments, _, _} = :cowboy_dispatcher.split_path(binary,
fn(bin) -> :cowboy_http.urldecode(bin, :crash) end)
segments
end
@@ -127,8 +127,13 @@ defmodule Dynamo.Cowboy.HTTP do
connection(conn, req: req, req_headers: Binary.Dict.new(headers))
end
+ def fetch(:body, connection(req: req, req_body: nil) = conn) do
+ { :ok, body, req } = R.body req
+ connection(conn, req: req, req_body: body)
+ end
+
# The given aspect was already loaded.
- def fetch(aspect, conn) when aspect in [:params, :cookies, :headers] do
+ def fetch(aspect, conn) when aspect in [:params, :cookies, :headers, :body] do
conn
end
View
5 lib/dynamo/http.ex
@@ -34,6 +34,11 @@ defmodule Dynamo.HTTP do
defcallback req_headers(conn)
@doc """
+ Returns the request body as a binary.
+ """
+ defcallback req_body(conn)
+
+ @doc """
Returns the HTTP method as an atom.
## Examples
View
8 lib/dynamo/http/request.ex
@@ -40,6 +40,14 @@ defmodule Dynamo.HTTP.Request do
def req_headers(connection(req_headers: req_headers)) do
req_headers
end
+
+ def req_body(connection(req_body: nil)) do
+ raise Dynamo.HTTP.UnfetchedError, aspect: :req_body
+ end
+
+ def req_body(connection(req_body: req_body)) do
+ req_body
+ end
end
end
end
View
12 lib/dynamo/http/test.ex
@@ -96,6 +96,10 @@ defmodule Dynamo.HTTP.Test do
connection(conn, cookies: raw_cookies, fetched: [:cookies|fetched])
end
+ def fetch(:body, connection(req_body: req_body, fetched: fetched) = conn) do
+ connection(conn, req_body: req_body, fetched: [:body|fetched])
+ end
+
## Test only API
@doc """
@@ -118,6 +122,7 @@ defmodule Dynamo.HTTP.Test do
script_name_segments: [],
params: nil,
req_headers: nil,
+ req_body: nil,
method: method,
original_method: method)
@@ -143,6 +148,13 @@ defmodule Dynamo.HTTP.Test do
end
@doc """
+ Sets the body to be read by the request.
+ """
+ def req_body(body, conn) do
+ connection(conn, req_body: body)
+ end
+
+ @doc """
Sets a request header, overriding any previous value.
Both `key` and `value` are converted to binary.
"""
View
10 test/dynamo/cowboy/http_test.exs
@@ -255,6 +255,16 @@ defmodule Dynamo.Cowboy.HTTPTest do
## Request Body API
+ def req_body(conn) do
+ conn = conn.fetch(:body)
+ assert conn.req_body == "foobar"
+ conn
+ end
+
+ test :req_body do
+ assert_success request :post, "/req_body", [{ "Content-Type", "application/x-foobar" }], "foobar"
+ end
+
def send(conn) do
assert conn.state == :unset
View
11 test/dynamo/http/test_test.exs
@@ -63,6 +63,17 @@ defmodule Dynamo.HTTP.TestTest do
assert conn.fetch(:headers).req_headers["X-Code"] == nil
end
+ test :req_body do
+ conn = conn(:POST, "/foo/bar")
+
+ assert_raise Dynamo.HTTP.UnfetchedError, fn ->
+ conn.req_body
+ end
+
+ conn = conn.req_body("foobar")
+ assert conn.fetch(:body).req_body == "foobar"
+ end
+
test :host do
conn = conn(:GET, "/foo/bar").fetch(:headers)
assert conn.req_headers["Host"] == "127.0.0.1"

0 comments on commit e94fcda

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