mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 08:12:31 -04:00
Reuse PostHandler to receive patch and put requests too
PHandler will take care of /put, /post and /patch
This commit is contained in:
parent
15ad5d5e65
commit
6ba2efb47d
3 changed files with 29 additions and 7 deletions
|
@ -7,7 +7,9 @@ defmodule HTTParrot do
|
|||
{'/user-agent', HTTParrot.UserAgentHandler, []},
|
||||
{'/headers', HTTParrot.HeadersHandler, []},
|
||||
{'/get', HTTParrot.GetHandler, []},
|
||||
{'/post', HTTParrot.PostHandler, []},
|
||||
{'/post', HTTParrot.PHandler, []},
|
||||
{'/put', HTTParrot.PHandler, []},
|
||||
{'/patch', HTTParrot.PHandler, []},
|
||||
{'/status/:code', HTTParrot.StatusCodeHandler, []},
|
||||
{'/redirect-to', HTTParrot.RedirectToHandler, []},
|
||||
{'/cookies', HTTParrot.CookiesHandler, []},
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
defmodule HTTParrot.PostHandler do
|
||||
defmodule HTTParrot.PHandler do
|
||||
@moduledoc """
|
||||
This REST handler will respond to POST, PATCH and PUT requests.
|
||||
"""
|
||||
alias HTTParrot.GeneralRequestInfo
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
@doc """
|
||||
When a request is made to /post, allowed_methods will return POST for example
|
||||
"""
|
||||
def allowed_methods(req, state) do
|
||||
{["POST"], req, state}
|
||||
{path, req} = :cowboy_req.path(req)
|
||||
path = String.slice(path, 1..-1)
|
||||
{[String.upcase(path)], req, state}
|
||||
end
|
||||
|
||||
def content_types_accepted(req, state) do
|
|
@ -1,7 +1,7 @@
|
|||
defmodule HTTParrot.PostHandlerTest do
|
||||
defmodule HTTParrot.PHandlerTest do
|
||||
use ExUnit.Case
|
||||
import :meck
|
||||
import HTTParrot.PostHandler
|
||||
import HTTParrot.PHandler
|
||||
|
||||
setup do
|
||||
new HTTParrot.GeneralRequestInfo
|
||||
|
@ -15,7 +15,19 @@ defmodule HTTParrot.PostHandlerTest do
|
|||
unload :cowboy_req
|
||||
end
|
||||
|
||||
test "returns json with general info and POST form data" do
|
||||
Enum.each [{"/post", "POST"},
|
||||
{"/patch", "PATCH"},
|
||||
{"/put", "PUT"}], fn {path, verb} ->
|
||||
test "allowed_methods return verb related to #{path}" do
|
||||
expect(:cowboy_req, :path, 1, {unquote(path), :req2})
|
||||
|
||||
assert allowed_methods(:req1, :state) == {[unquote(verb)], :req2, :state}
|
||||
|
||||
assert validate :cowboy_req
|
||||
end
|
||||
end
|
||||
|
||||
test "returns json with general info and P[OST, ATCH, UT] form data" do
|
||||
expect(:cowboy_req, :body_qs, 1, {:ok, :body_qs, :req2})
|
||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
||||
|
@ -26,7 +38,7 @@ defmodule HTTParrot.PostHandlerTest do
|
|||
assert validate HTTParrot.GeneralRequestInfo
|
||||
end
|
||||
|
||||
test "returns json with general info and POST JSON body data" do
|
||||
test "returns json with general info and P[OST, ATCH, UT] JSON body data" do
|
||||
expect(:cowboy_req, :body, 1, {:ok, :body, :req2})
|
||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
Loading…
Add table
Reference in a new issue