diff --git a/lib/httparrot.ex b/lib/httparrot.ex index ba9f750..e36d634 100644 --- a/lib/httparrot.ex +++ b/lib/httparrot.ex @@ -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, []}, diff --git a/lib/httparrot/set_cookies_handler.ex b/lib/httparrot/set_cookies_handler.ex index e407511..60754d5 100644 --- a/lib/httparrot/set_cookies_handler.ex +++ b/lib/httparrot/set_cookies_handler.ex @@ -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 diff --git a/test/set_cookies_handler_test.exs b/test/set_cookies_handler_test.exs index ee4a374..daa1aec 100644 --- a/test/set_cookies_handler_test.exs +++ b/test/set_cookies_handler_test.exs @@ -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}])