1
0
Fork 0
mirror of https://github.com/edgurgel/httparrot synced 2025-04-05 08:12:31 -04:00

Add '/cookies/set/:name/:value' reusing the same handler

This commit is contained in:
Eduardo Gurgel 2014-01-31 22:06:41 -03:00
parent 36f62941a7
commit 2008fab385
3 changed files with 46 additions and 2 deletions

View file

@ -17,7 +17,7 @@ defmodule HTTParrot do
{'/redirect-to', HTTParrot.RedirectToHandler, []},
{'/relative-redirect/:n', HTTParrot.RelativeRedirectHandler, []},
{'/cookies', HTTParrot.CookiesHandler, []},
{'/cookies/set', HTTParrot.SetCookiesHandler, []},
{'/cookies/set[/:name/:value]', HTTParrot.SetCookiesHandler, []},
{'/cookies/delete', HTTParrot.DeleteCookiesHandler, []},
{'/basic-auth/:user/:passwd', HTTParrot.BasicAuthHandler, []},
{'/hidden-basic-auth/:user/:passwd', HTTParrot.HiddenBasicAuthHandler, []},

View file

@ -13,7 +13,13 @@ defmodule HTTParrot.SetCookiesHandler do
def malformed_request(req, state) do
{qs_vals, req} = :cowboy_req.qs_vals(req)
if Enum.empty?(qs_vals), do: {true, req, state}, else: {false, req, qs_vals}
{name, req} = :cowboy_req.binding(:name, req, nil)
if name do
{value, req} = :cowboy_req.binding(:value, req, nil)
{false, req, Dict.merge([{name, value}], qs_vals)}
else
if Enum.empty?(qs_vals), do: {true, req, state}, else: {false, req, qs_vals}
end
end
def content_types_provided(req, state) do

View file

@ -11,6 +11,44 @@ defmodule HTTParrot.SetCookiesHandlerTest do
unload :cowboy_req
end
test "malformed_request returns false if /name/value is sent" do
expect(:cowboy_req, :qs_vals, 1, {[], :req2})
expect(:cowboy_req, :binding, [{[:name, :req2, nil], {"name", :req3}},
{[:value, :req3, nil], {"value", :req4}}])
assert malformed_request(:req1, :state) == {false, :req4, [{"name", "value"}]}
assert validate :cowboy_req
end
test "malformed_request returns query string values too if /name/value is sent" do
expect(:cowboy_req, :qs_vals, 1, {[{"name", "value2"}], :req2})
expect(:cowboy_req, :binding, [{[:name, :req2, nil], {"name", :req3}},
{[:value, :req3, nil], {"value", :req4}}])
assert malformed_request(:req1, :state) == {false, :req4, [{"name", "value2"}]}
assert validate :cowboy_req
end
test "malformed_request returns false if query string values are sent" do
expect(:cowboy_req, :qs_vals, 1, {[{"name", "value"}], :req2})
expect(:cowboy_req, :binding, [{[:name, :req2, nil], {nil, :req3}}])
assert malformed_request(:req1, :state) == {false, :req3, [{"name", "value"}]}
assert validate :cowboy_req
end
test "malformed_request returns true if query string values are not sent" do
expect(:cowboy_req, :qs_vals, 1, {[], :req2})
expect(:cowboy_req, :binding, [{[:name, :req2, nil], {nil, :req3}}])
assert malformed_request(:req1, :state) == {true, :req3, :state}
assert validate :cowboy_req
end
test "redirect to /cookies " do
expect(:cowboy_req, :set_resp_cookie, [{[:k1, :v1, [path: "/"], :req1], :req2},
{[:k2, :v2, [path: "/"], :req2], :req3}])