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

more fixes

This commit is contained in:
Alex S 2019-06-29 14:41:16 +03:00
parent eedc9798bc
commit 3f2bbe78df
9 changed files with 18 additions and 17 deletions

View file

@ -26,6 +26,6 @@ defmodule HTTParrot.DeleteCookiesHandler do
defp delete_cookie(name, true, req), do: delete_cookie(name, "", req) defp delete_cookie(name, true, req), do: delete_cookie(name, "", req)
defp delete_cookie(name, value, req) do defp delete_cookie(name, value, req) do
:cowboy_req.set_resp_cookie(name, value, req, path: "/", max_age: 0) :cowboy_req.set_resp_cookie(name, value, req, %{path: "/", max_age: 0})
end end
end end

View file

@ -2,7 +2,7 @@ defmodule HTTParrot.GeneralRequestInfo do
def retrieve(req) do def retrieve(req) do
args = :cowboy_req.parse_qs(req) args = :cowboy_req.parse_qs(req)
headers = :cowboy_req.headers(req) headers = :cowboy_req.headers(req)
url = :cowboy_req.uri(req) url = IO.iodata_to_binary(:cowboy_req.uri(req))
{ip, _port} = :cowboy_req.peer(req) {ip, _port} = :cowboy_req.peer(req)
ip = ip =
@ -19,8 +19,8 @@ defmodule HTTParrot.GeneralRequestInfo do
@doc """ @doc """
Group by keys and if duplicated keys, aggregate them as a list Group by keys and if duplicated keys, aggregate them as a list
iex> group_by_keys([a: "v1", a: "v2", b: "v3", a: "v4"]) iex> group_by_keys([{"a", "v1"}, {"a", "v2"}, {"b", "v3"}, {"a", "v4"}])
%{a: ["v1", "v2", "v4"], b: "v3"} %{"a" => ["v1", "v2", "v4"], "b" => "v3"}
""" """
@spec group_by_keys(list) :: map @spec group_by_keys(list) :: map
def group_by_keys([]), do: %{} def group_by_keys([]), do: %{}

View file

@ -19,7 +19,7 @@ defmodule HTTParrot.RedirectHandler do
def previously_existed(req, state), do: {true, req, state} def previously_existed(req, state), do: {true, req, state}
def moved_permanently(req, n) do def moved_permanently(req, n) do
host_url = :cowboy_req.uri(req) host_url = IO.iodata_to_binary(:cowboy_req.uri(req))
url = if n > 1, do: "/redirect/#{n - 1}", else: "/get" url = if n > 1, do: "/redirect/#{n - 1}", else: "/get"
{{true, host_url <> url}, req, nil} {{true, host_url <> url}, req, nil}
end end

View file

@ -31,6 +31,6 @@ defmodule HTTParrot.SetCookiesHandler do
end end
defp set_cookie(name, value, req) do defp set_cookie(name, value, req) do
:cowboy_req.set_resp_cookie(name, value, req, path: "/") :cowboy_req.set_resp_cookie(name, value, req, %{path: "/"})
end end
end end

View file

@ -21,7 +21,7 @@ defmodule HTTParrot.StoreRequestHandler do
def get(req, state) do def get(req, state) do
{info, req} = GeneralRequestInfo.retrieve(req) {info, req} = GeneralRequestInfo.retrieve(req)
{key, _} = :cowboy_req.binding(:key, req) key = :cowboy_req.binding(:key, req)
HTTParrot.RequestStore.store(key, info) HTTParrot.RequestStore.store(key, info)
{'{"saved": "true"}', req, state} {'{"saved": "true"}', req, state}
end end
@ -65,7 +65,7 @@ defmodule HTTParrot.StoreRequestHandler do
{info, req} = GeneralRequestInfo.retrieve(req) {info, req} = GeneralRequestInfo.retrieve(req)
key = :cowboy_req.binding(:key, req) key = :cowboy_req.binding(:key, req)
HTTParrot.RequestStore.store(key, info ++ body) HTTParrot.RequestStore.store(key, info ++ body)
req = :cowboy_req.reply(200, %{}, '{"saved": "true"}', req) req = :cowboy_req.reply(200, %{}, "{\"saved\": \"true\"}", req)
{:halt, req, nil} {:halt, req, nil}
end end
end end

View file

@ -11,15 +11,16 @@ defmodule HTTParrot.DeleteCookiesHandlerTest do
test "delete cookies and redirect to /cookies " do test "delete cookies and redirect to /cookies " do
expect(:cowboy_req, :set_resp_cookie, [ expect(:cowboy_req, :set_resp_cookie, [
{[:k1, :v1, :req1, [path: "/", max_age: 0]], :req2}, {["k1", "v1", :req1, %{path: "/", max_age: 0}], :req2},
{[:k2, :v2, :req2, [path: "/", max_age: 0]], :req3} {["k2", "v2", :req2, %{path: "/", max_age: 0}], :req3}
]) ])
expect(:cowboy_req, :reply, [ expect(:cowboy_req, :reply, [
{[302, %{"location" => "/cookies"}, "Redirecting...", :req3], :req4} {[302, %{"location" => "/cookies"}, "Redirecting...", :req3], :req4}
]) ])
assert get_json(:req1, k1: :v1, k2: :v2) == {:halt, :req4, [k1: :v1, k2: :v2]} assert get_json(:req1, [{"k1", "v1"}, {"k2", "v2"}]) ==
{:halt, :req4, [{"k1", "v1"}, {"k2", "v2"}]}
assert validate(:cowboy_req) assert validate(:cowboy_req)
end end

View file

@ -60,8 +60,8 @@ defmodule HTTParrot.SetCookiesHandlerTest do
test "redirect to /cookies " do test "redirect to /cookies " do
expect(:cowboy_req, :set_resp_cookie, [ expect(:cowboy_req, :set_resp_cookie, [
{[:k1, :v1, :req1, [path: "/"]], :req2}, {[:k1, :v1, :req1, %{path: "/"}], :req2},
{[:k2, :v2, :req2, [path: "/"]], :req3} {[:k2, :v2, :req2, %{path: "/"}], :req3}
]) ])
expect(:cowboy_req, :reply, [ expect(:cowboy_req, :reply, [

View file

@ -5,19 +5,19 @@ defmodule HTTParrot.StoreRequestHandlerTests do
setup do setup do
HTTParrot.RequestStore.clear(:test) HTTParrot.RequestStore.clear(:test)
on_exit fn -> unload() end on_exit(fn -> unload() end)
:ok :ok
end end
test "store a request" do test "store a request" do
expect(:cowboy_req, :binding, [:key, :req1], {:test, :req1}) expect(:cowboy_req, :binding, [:key, :req1], :test)
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req1}) expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req1})
assert get(:req1, :state) == {'{"saved": "true"}', :req1, :state} assert get(:req1, :state) == {'{"saved": "true"}', :req1, :state}
assert HTTParrot.RequestStore.retrieve(:test) == [:info] assert HTTParrot.RequestStore.retrieve(:test) == [:info]
end end
test "store multiple requests" do test "store multiple requests" do
expect(:cowboy_req, :binding, [:key, :req1], {:test, :req1}) expect(:cowboy_req, :binding, [:key, :req1], :test)
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req1}) expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req1})
assert get(:req1, :state) == {'{"saved": "true"}', :req1, :state} assert get(:req1, :state) == {'{"saved": "true"}', :req1, :state}
assert get(:req2, :state) == {'{"saved": "true"}', :req1, :state} assert get(:req2, :state) == {'{"saved": "true"}', :req1, :state}

View file

@ -1 +1 @@
ExUnit.start ExUnit.start()