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

Fix Cowboy 2.5 upgrade issues

This commit is contained in:
Eduardo Gurgel 2019-06-30 14:44:33 +12:00
parent 545101d50c
commit 1f4f716124
20 changed files with 59 additions and 64 deletions

View file

@ -9,7 +9,7 @@ defmodule HTTParrot.Base64Handler do
end end
def malformed_request(req, state) do def malformed_request(req, state) do
{value, req} = :cowboy_req.binding(:value, req) value = :cowboy_req.binding(:value, req)
case decode(value) do case decode(value) do
{:ok, result} -> {false, req, result} {:ok, result} -> {false, req, result}

View file

@ -5,12 +5,12 @@ defmodule HTTParrot.BasicAuthHandler do
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS) use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
def is_authorized(req, state) do def is_authorized(req, state) do
{user, req} = :cowboy_req.binding(:user, req) user = :cowboy_req.binding(:user, req)
{passwd, req} = :cowboy_req.binding(:passwd, req) passwd = :cowboy_req.binding(:passwd, req)
{:ok, auth, req} = :cowboy_req.parse_header("authorization", req) auth = :cowboy_req.parse_header("authorization", req)
case auth do case auth do
{"basic", {^user, ^passwd}} -> {true, req, user} {:basic, ^user, ^passwd} -> {true, req, user}
_ -> {{false, "Basic realm=\"Fake Realm\""}, req, state} _ -> {{false, "Basic realm=\"Fake Realm\""}, req, state}
end end
end end

View file

@ -20,7 +20,7 @@ defmodule HTTParrot.DeleteCookiesHandler do
end) end)
req = :cowboy_req.reply(302, %{"location" => "/cookies"}, "Redirecting...", req) req = :cowboy_req.reply(302, %{"location" => "/cookies"}, "Redirecting...", req)
{:halt, req, qs_vals} {:stop, req, qs_vals}
end end
defp delete_cookie(name, true, req), do: delete_cookie(name, "", req) defp delete_cookie(name, true, req), do: delete_cookie(name, "", req)

View file

@ -8,6 +8,7 @@ defmodule HTTParrot.GeneralRequestInfo do
ip = ip =
case ip do case ip do
{127, 0, 0, 1} -> "" {127, 0, 0, 1} -> ""
:local -> ""
_ -> :inet_parse.ntoa(ip) |> to_string _ -> :inet_parse.ntoa(ip) |> to_string
end end

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 = IO.iodata_to_binary(:cowboy_req.uri(req)) host_url = IO.iodata_to_binary(:cowboy_req.uri(req, %{path: :undefined, qs: :undefined, fragment: :undefined}))
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

@ -27,7 +27,7 @@ defmodule HTTParrot.SetCookiesHandler do
end) end)
req = :cowboy_req.reply(302, %{"location" => "/cookies"}, "Redirecting...", req) req = :cowboy_req.reply(302, %{"location" => "/cookies"}, "Redirecting...", req)
{:halt, req, qs_vals} {:stop, req, qs_vals}
end end
defp set_cookie(name, value, req) do defp set_cookie(name, value, req) do

View file

@ -11,6 +11,6 @@ defmodule HTTParrot.StatusCodeHandler do
def get_json(req, state) do def get_json(req, state) do
code = :cowboy_req.binding(:code, req) code = :cowboy_req.binding(:code, req)
req = :cowboy_req.reply(code, %{}, "", req) req = :cowboy_req.reply(code, %{}, "", req)
{:halt, req, state} {:stop, req, state}
end end
end end

View file

@ -66,6 +66,6 @@ defmodule HTTParrot.StoreRequestHandler do
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} {:stop, req, nil}
end end
end end

View file

@ -26,17 +26,19 @@ defmodule HTTParrot.StreamBytesHandler do
def get_bytes(req, state) do def get_bytes(req, state) do
{n, seed, chunk_size} = state {n, seed, chunk_size} = state
:rand.seed(:exs64, {seed, seed, seed}) :rand.seed(:exs64, {seed, seed, seed})
{{:chunked, stream_response(n, chunk_size)}, req, nil} req = stream_response!(n, chunk_size, req)
{:stop, req, nil}
end end
defp stream_response(n, chunk_size) do defp stream_response!(n, chunk_size, req) do
fn send_func -> req = :cowboy_req.stream_reply(200, %{ "content-type" => "application/octet-stream" }, req)
Stream.repeatedly(fn -> :rand.uniform(255) end) Stream.repeatedly(fn -> :rand.uniform(255) end)
|> Stream.take(n) |> Stream.take(n)
|> Enum.chunk_every(chunk_size, chunk_size, []) |> Enum.chunk_every(chunk_size, chunk_size, [])
|> Enum.each(fn chunk -> |> Enum.each(fn chunk ->
send_func.(List.to_string(chunk)) :cowboy_req.stream_body(List.to_string(chunk), :nofin, req)
end) end)
end :cowboy_req.stream_body("", :fin, req)
req
end end
end end

View file

@ -25,14 +25,12 @@ defmodule HTTParrot.StreamHandler do
def get_json(req, n) do def get_json(req, n) do
{info, req} = GeneralRequestInfo.retrieve(req) {info, req} = GeneralRequestInfo.retrieve(req)
{{:chunked, stream_response(n, info)}, req, nil} req = :cowboy_req.stream_reply(200, %{ "content-type" => "application/json" }, req)
end Enum.each(0..(n - 1), fn i ->
body = JSX.encode!([id: i] ++ info)
defp stream_response(n, info) do :cowboy_req.stream_body(body, :nofin, req)
fn send_func -> end)
Enum.each(0..(n - 1), fn i -> :cowboy_req.stream_body("", :fin, req)
send_func.(([id: i] ++ info) |> JSX.encode!()) {:stop, req, nil}
end)
end
end end
end end

View file

@ -9,7 +9,7 @@ defmodule HTTParrot.UserAgentHandler do
end end
def get_json(req, state) do def get_json(req, state) do
{user_agent, req} = :cowboy_req.header("user-agent", req, "null") user_agent = :cowboy_req.header("user-agent", req, "null")
{response(user_agent), req, state} {response(user_agent), req, state}
end end

View file

@ -10,16 +10,16 @@ defmodule HTTParrot.Base64HandlerTest do
end end
test "halts with error" do test "halts with error" do
expect(:cowboy_req, :binding, [{[:value, :req1], {"I=", :req2}}]) expect(:cowboy_req, :binding, [{[:value, :req1], "I="}])
assert malformed_request(:req1, :state) == {true, :req2, :state} assert malformed_request(:req1, :state) == {true, :req1, :state}
assert validate(:cowboy_req) assert validate(:cowboy_req)
end end
test "proceeds with decoded base64 urlsafe" do test "proceeds with decoded base64 urlsafe" do
expect(:cowboy_req, :binding, [{[:value, :req1], {"LytiYXNlNjQrLw", :req2}}]) expect(:cowboy_req, :binding, [{[:value, :req1], "LytiYXNlNjQrLw"}])
assert malformed_request(:req1, :state) == {false, :req2, "/+base64+/"} assert malformed_request(:req1, :state) == {false, :req1, "/+base64+/"}
assert validate(:cowboy_req) assert validate(:cowboy_req)
end end

View file

@ -11,22 +11,22 @@ defmodule HTTParrot.BasicAuthHandlerTest do
end end
test "is_authorized returns true if user and passwd match" do test "is_authorized returns true if user and passwd match" do
expect(:cowboy_req, :binding, [{[:user, :req1], {:user, :req2}}, expect(:cowboy_req, :binding, [{[:user, :req1], :user},
{[:passwd, :req2], {:passwd, :req3}}]) {[:passwd, :req1], :passwd}])
expect(:cowboy_req, :parse_header, [{["authorization", :req3], {:ok, {"basic", {:user, :passwd}}, :req4}}]) expect(:cowboy_req, :parse_header, [{["authorization", :req1], {:basic, :user, :passwd}}])
assert is_authorized(:req1, :state) == {true, :req4, :user} assert is_authorized(:req1, :state) == {true, :req1, :user}
assert validate :cowboy_req assert validate :cowboy_req
assert validate JSX assert validate JSX
end end
test "is_authorized returns false if user and passwd doesnt match" do test "is_authorized returns false if user and passwd doesnt match" do
expect(:cowboy_req, :binding, [{[:user, :req1], {:user, :req2}}, expect(:cowboy_req, :binding, [{[:user, :req1], :user},
{[:passwd, :req2], {:passwd, :req3}}]) {[:passwd, :req1], :passwd}])
expect(:cowboy_req, :parse_header, [{["authorization", :req3], {:ok, {"basic", {:not_the_user, :passwd}}, :req4}}]) expect(:cowboy_req, :parse_header, [{["authorization", :req1], {:basic, :not_the_user, :passwd}}])
assert is_authorized(:req1, :state) == {{false, "Basic realm=\"Fake Realm\""}, :req4, :state} assert is_authorized(:req1, :state) == {{false, "Basic realm=\"Fake Realm\""}, :req1, :state}
assert validate :cowboy_req assert validate :cowboy_req
assert validate JSX assert validate JSX

View file

@ -20,7 +20,7 @@ defmodule HTTParrot.DeleteCookiesHandlerTest do
]) ])
assert get_json(:req1, [{"k1", "v1"}, {"k2", "v2"}]) == assert get_json(:req1, [{"k1", "v1"}, {"k2", "v2"}]) ==
{:halt, :req4, [{"k1", "v1"}, {"k2", "v2"}]} {:stop, :req4, [{"k1", "v1"}, {"k2", "v2"}]}
assert validate(:cowboy_req) assert validate(:cowboy_req)
end end

View file

@ -34,7 +34,7 @@ defmodule HTTParrot.RedirectHandlerTest do
end end
test "moved_permanently returns 'redirect/n-1' if n > 1" do test "moved_permanently returns 'redirect/n-1' if n > 1" do
expect(:cowboy_req, :uri, 1, "host") expect(:cowboy_req, :uri, 2, "host")
assert moved_permanently(:req1, 4) == {{true, "host/redirect/3"}, :req1, nil} assert moved_permanently(:req1, 4) == {{true, "host/redirect/3"}, :req1, nil}
@ -42,7 +42,7 @@ defmodule HTTParrot.RedirectHandlerTest do
end end
test "moved_permanently returns '/get' if n = 1" do test "moved_permanently returns '/get' if n = 1" do
expect(:cowboy_req, :uri, 1, "host") expect(:cowboy_req, :uri, 2, "host")
assert moved_permanently(:req1, 1) == {{true, "host/get"}, :req1, nil} assert moved_permanently(:req1, 1) == {{true, "host/get"}, :req1, nil}

View file

@ -68,7 +68,7 @@ defmodule HTTParrot.SetCookiesHandlerTest do
{[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) == {:stop, :req4, [k1: :v1, k2: :v2]}
assert validate(:cowboy_req) assert validate(:cowboy_req)
end end

View file

@ -13,7 +13,7 @@ defmodule HTTParrot.StatusCodeHandlerTest do
code = 123 code = 123
expect(:cowboy_req, :binding, [{[:code, :req1], code}]) expect(:cowboy_req, :binding, [{[:code, :req1], code}])
expect(:cowboy_req, :reply, [{[code, %{}, "", :req1], :req2}]) expect(:cowboy_req, :reply, [{[code, %{}, "", :req1], :req2}])
assert get_json(:req1, :state) == {:halt, :req2, :state} assert get_json(:req1, :state) == {:stop, :req2, :state}
assert validate(:cowboy_req) assert validate(:cowboy_req)
end end
end end

View file

@ -100,15 +100,14 @@ defmodule HTTParrot.StreamBytesHandlerTest do
end end
test "response must stream chunks" do test "response must stream chunks" do
assert {{:chunked, func}, :req1, nil} = get_bytes(:req1, {9, 3, 4}) expect(:cowboy_req, :stream_reply, 3, :req2)
assert is_function(func) expect(:cowboy_req, :stream_body, 3, :ok)
send_func = fn body -> send(self(), {:chunk, body}) end assert {:stop, :req2, nil} = get_bytes(:req1, {9, 3, 4})
func.(send_func)
assert_receive {:chunk, chunk1} chunk1 = :meck.capture(1, :cowboy_req, :stream_body, :_, 1)
assert_receive {:chunk, chunk2} chunk2 = :meck.capture(2, :cowboy_req, :stream_body, :_, 1)
assert_receive {:chunk, chunk3} chunk3 = :meck.capture(3, :cowboy_req, :stream_body, :_, 1)
assert String.length(chunk1) == 4 assert String.length(chunk1) == 4
assert String.length(chunk2) == 4 assert String.length(chunk2) == 4

View file

@ -45,16 +45,11 @@ defmodule HTTParrot.StreamHandlerTest do
test "response must stream chunks" do test "response must stream chunks" do
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req2}) expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req2})
expect(:cowboy_req, :stream_reply, 3, :req2)
expect(:cowboy_req, :stream_body, 3, :req2)
expect(JSX, :encode!, [{[[{:id, 0}, :info]], :json1}, {[[{:id, 1}, :info]], :json2}]) expect(JSX, :encode!, [{[[{:id, 0}, :info]], :json1}, {[[{:id, 1}, :info]], :json2}])
assert {{:chunked, func}, :req2, nil} = get_json(:req1, 2) assert {:stop, :req2, nil} = get_json(:req1, 2)
assert is_function(func)
send_func = fn body -> send(self(), {:chunk, body}) end
func.(send_func)
assert_receive {:chunk, :json1}
assert_receive {:chunk, :json2}
assert validate(HTTParrot.GeneralRequestInfo) assert validate(HTTParrot.GeneralRequestInfo)
assert validate(JSX) assert validate(JSX)

View file

@ -11,10 +11,10 @@ defmodule HTTParrot.UserAgentHandlerTest do
end end
test "returns prettified json with user agent" do test "returns prettified json with user agent" do
expect(:cowboy_req, :header, [{["user-agent", :req1, "null"], {:user_agent, :req2}}]) expect(:cowboy_req, :header, [{["user-agent", :req1, "null"], :user_agent}])
expect(JSX, :encode!, [{[[{"user-agent", :user_agent}]], :json}]) expect(JSX, :encode!, [{[[{"user-agent", :user_agent}]], :json}])
assert get_json(:req1, :state) == {:json, :req2, :state} assert get_json(:req1, :state) == {:json, :req1, :state}
assert validate :cowboy_req assert validate :cowboy_req
assert validate JSX assert validate JSX