Skip to content

Commit

Permalink
Support fetching request body
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Torres committed Sep 28, 2012
1 parent 17bd67f commit e94fcda
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/dynamo/cowboy/http.ex
Expand Up @@ -12,7 +12,7 @@ defmodule Dynamo.Cowboy.HTTP do
Record.defmacros __ENV__, :connection, Record.defmacros __ENV__, :connection,
[ :req, :path_info_segments, :script_name_segments, :req_headers, [ :req, :path_info_segments, :script_name_segments, :req_headers,
:params, :cookies, :resp_headers, :resp_cookies, :assigns, :status, :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 ] :resp_body, :state ]


use Dynamo.HTTP.Paths use Dynamo.HTTP.Paths
Expand All @@ -26,7 +26,7 @@ defmodule Dynamo.Cowboy.HTTP do
{ verb, req } = R.method(req) { verb, req } = R.method(req)


{ binary, _ } = R.path 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) fn(bin) -> :cowboy_http.urldecode(bin, :crash) end)


connection( connection(
Expand Down Expand Up @@ -60,7 +60,7 @@ defmodule Dynamo.Cowboy.HTTP do


def path_segments(connection(req: req)) do def path_segments(connection(req: req)) do
{ binary, _ } = R.path 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) fn(bin) -> :cowboy_http.urldecode(bin, :crash) end)
segments segments
end end
Expand Down Expand Up @@ -127,8 +127,13 @@ defmodule Dynamo.Cowboy.HTTP do
connection(conn, req: req, req_headers: Binary.Dict.new(headers)) connection(conn, req: req, req_headers: Binary.Dict.new(headers))
end 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. # 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 conn
end end


Expand Down
5 changes: 5 additions & 0 deletions lib/dynamo/http.ex
Expand Up @@ -33,6 +33,11 @@ defmodule Dynamo.HTTP do
""" """
defcallback req_headers(conn) defcallback req_headers(conn)


@doc """
Returns the request body as a binary.
"""
defcallback req_body(conn)

@doc """ @doc """
Returns the HTTP method as an atom. Returns the HTTP method as an atom.
Expand Down
8 changes: 8 additions & 0 deletions lib/dynamo/http/request.ex
Expand Up @@ -40,6 +40,14 @@ defmodule Dynamo.HTTP.Request do
def req_headers(connection(req_headers: req_headers)) do def req_headers(connection(req_headers: req_headers)) do
req_headers req_headers
end 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 end
end end
12 changes: 12 additions & 0 deletions lib/dynamo/http/test.ex
Expand Up @@ -96,6 +96,10 @@ defmodule Dynamo.HTTP.Test do
connection(conn, cookies: raw_cookies, fetched: [:cookies|fetched]) connection(conn, cookies: raw_cookies, fetched: [:cookies|fetched])
end 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 ## Test only API


@doc """ @doc """
Expand All @@ -118,6 +122,7 @@ defmodule Dynamo.HTTP.Test do
script_name_segments: [], script_name_segments: [],
params: nil, params: nil,
req_headers: nil, req_headers: nil,
req_body: nil,
method: method, method: method,
original_method: method) original_method: method)


Expand All @@ -142,6 +147,13 @@ defmodule Dynamo.HTTP.Test do
connection(conn, raw_cookies: Binary.Dict.new(cookies)) connection(conn, raw_cookies: Binary.Dict.new(cookies))
end end


@doc """
Sets the body to be read by the request.
"""
def req_body(body, conn) do
connection(conn, req_body: body)
end

@doc """ @doc """
Sets a request header, overriding any previous value. Sets a request header, overriding any previous value.
Both `key` and `value` are converted to binary. Both `key` and `value` are converted to binary.
Expand Down
10 changes: 10 additions & 0 deletions test/dynamo/cowboy/http_test.exs
Expand Up @@ -255,6 +255,16 @@ defmodule Dynamo.Cowboy.HTTPTest do


## Request Body API ## 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 def send(conn) do
assert conn.state == :unset assert conn.state == :unset


Expand Down
11 changes: 11 additions & 0 deletions test/dynamo/http/test_test.exs
Expand Up @@ -63,6 +63,17 @@ defmodule Dynamo.HTTP.TestTest do
assert conn.fetch(:headers).req_headers["X-Code"] == nil assert conn.fetch(:headers).req_headers["X-Code"] == nil
end 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 test :host do
conn = conn(:GET, "/foo/bar").fetch(:headers) conn = conn(:GET, "/foo/bar").fetch(:headers)
assert conn.req_headers["Host"] == "127.0.0.1" assert conn.req_headers["Host"] == "127.0.0.1"
Expand Down

0 comments on commit e94fcda

Please sign in to comment.