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

more changes

This commit is contained in:
Alex S 2019-06-28 19:01:03 +03:00
parent 9821d9ce61
commit eedc9798bc
16 changed files with 113 additions and 109 deletions

View file

@ -9,7 +9,7 @@ defmodule HTTParrot.CookiesHandler do
end
def get_json(req, state) do
{cookies, req} = :cowboy_req.parse_cookies(req)
cookies = :cowboy_req.parse_cookies(req)
cookies = if cookies == [], do: [{}], else: cookies
{response(cookies), req, state}
end

View file

@ -5,7 +5,7 @@ defmodule HTTParrot.DeleteCookiesHandler do
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
def malformed_request(req, state) do
{qs_vals, req} = :cowboy_req.parse_qs(req)
qs_vals = :cowboy_req.parse_qs(req)
if Enum.empty?(qs_vals), do: {true, req, state}, else: {false, req, qs_vals}
end
@ -19,13 +19,13 @@ defmodule HTTParrot.DeleteCookiesHandler do
delete_cookie(name, value, req)
end)
{:ok, req} = :cowboy_req.reply(302, [{"location", "/cookies"}], "Redirecting...", req)
req = :cowboy_req.reply(302, %{"location" => "/cookies"}, "Redirecting...", req)
{:halt, req, qs_vals}
end
defp delete_cookie(name, true, req), do: delete_cookie(name, "", req)
defp delete_cookie(name, value, req) do
:cowboy_req.set_resp_cookie(name, value, [path: "/", max_age: 0], req)
:cowboy_req.set_resp_cookie(name, value, req, path: "/", max_age: 0)
end
end

View file

@ -5,7 +5,7 @@ defmodule HTTParrot.RedirectHandler do
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
def malformed_request(req, state) do
{n, req} = :cowboy_req.binding(:n, req)
n = :cowboy_req.binding(:n, req)
try do
n = n |> String.to_integer() |> max(1)
@ -19,7 +19,7 @@ defmodule HTTParrot.RedirectHandler do
def previously_existed(req, state), do: {true, req, state}
def moved_permanently(req, n) do
{host_url, req} = :cowboy_req.uri(req)
host_url = :cowboy_req.uri(req)
url = if n > 1, do: "/redirect/#{n - 1}", else: "/get"
{{true, host_url <> url}, req, nil}
end

View file

@ -5,7 +5,7 @@ defmodule HTTParrot.ResponseHeadersHandler do
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
def malformed_request(req, state) do
{qs_vals, req} = :cowboy_req.parse_qs(req)
qs_vals = :cowboy_req.parse_qs(req)
if not Enum.empty?(qs_vals) do
{false, req, qs_vals}

View file

@ -5,11 +5,11 @@ defmodule HTTParrot.SetCookiesHandler do
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
def malformed_request(req, state) do
{qs_vals, req} = :cowboy_req.parse_qs(req)
{name, req} = :cowboy_req.binding(:name, req, nil)
qs_vals = :cowboy_req.parse_qs(req)
name = :cowboy_req.binding(:name, req, nil)
if name do
{value, req} = :cowboy_req.binding(:value, req, nil)
value = :cowboy_req.binding(:value, req, nil)
{false, req, List.keystore(qs_vals, name, 0, {name, value})}
else
if Enum.empty?(qs_vals), do: {true, req, state}, else: {false, req, qs_vals}
@ -26,11 +26,11 @@ defmodule HTTParrot.SetCookiesHandler do
set_cookie(name, value, req)
end)
{:ok, req} = :cowboy_req.reply(302, [{"location", "/cookies"}], "Redirecting...", req)
req = :cowboy_req.reply(302, %{"location" => "/cookies"}, "Redirecting...", req)
{:halt, req, qs_vals}
end
defp set_cookie(name, value, req) do
:cowboy_req.set_resp_cookie(name, value, [path: "/"], req)
:cowboy_req.set_resp_cookie(name, value, req, path: "/")
end
end

View file

@ -9,8 +9,8 @@ defmodule HTTParrot.StatusCodeHandler do
end
def get_json(req, state) do
{code, req} = :cowboy_req.binding(:code, req)
{:ok, req} = :cowboy_req.reply(code, [], "", req)
code = :cowboy_req.binding(:code, req)
req = :cowboy_req.reply(code, %{}, "", req)
{:halt, req, state}
end
end

View file

@ -63,9 +63,9 @@ defmodule HTTParrot.StoreRequestHandler do
def save_post(req, body) do
{info, req} = GeneralRequestInfo.retrieve(req)
{key, req} = :cowboy_req.binding(:key, req)
key = :cowboy_req.binding(:key, req)
HTTParrot.RequestStore.store(key, info ++ body)
{:ok, req} = :cowboy_req.reply(200, [], '{"saved": "true"}', req)
req = :cowboy_req.reply(200, %{}, '{"saved": "true"}', req)
{:halt, req, nil}
end
end

View file

@ -9,9 +9,9 @@ defmodule HTTParrot.StreamBytesHandler do
end
def malformed_request(req, state) do
{n, req} = :cowboy_req.binding(:n, req)
{seed, req} = :cowboy_req.match_qs(["seed", req], "1234")
{chunk_size, req} = :cowboy_req.match_qs(["chunk_size", req], "1024")
n = :cowboy_req.binding(:n, req)
%{seed: seed} = :cowboy_req.match_qs([{:seed, [], "1234"}], req)
%{chunk_size: chunk_size} = :cowboy_req.match_qs([{:chunk_size, [], "1024"}], req)
try do
n = n |> String.to_integer()

View file

@ -11,10 +11,10 @@ defmodule HTTParrot.CookiesHandlerTest do
end
test "returns a JSON with all the cookies o/" do
expect(:cowboy_req, :parse_cookies, 1, {[k1: :v1], :req2})
expect(:cowboy_req, :parse_cookies, 1, k1: :v1)
expect(JSX, :encode!, [{[[cookies: [k1: :v1]]], :json}])
assert get_json(:req1, :state) == {:json, :req2, :state}
assert get_json(:req1, :state) == {:json, :req1, :state}
assert validate(:cowboy_req)
assert validate(JSX)

View file

@ -4,18 +4,23 @@ defmodule HTTParrot.DeleteCookiesHandlerTest do
import HTTParrot.DeleteCookiesHandler
setup do
new :cowboy_req
on_exit fn -> unload() end
new(:cowboy_req)
on_exit(fn -> unload() end)
:ok
end
test "delete cookies and redirect to /cookies " do
expect(:cowboy_req, :set_resp_cookie, [{[:k1, :v1, [path: "/", max_age: 0], :req1], :req2},
{[:k2, :v2, [path: "/", max_age: 0], :req2], :req3}])
expect(:cowboy_req, :reply, [{[302, [{"location", "/cookies"}], "Redirecting...", :req3], {:ok, :req4}}])
expect(:cowboy_req, :set_resp_cookie, [
{[:k1, :v1, :req1, [path: "/", max_age: 0]], :req2},
{[:k2, :v2, :req2, [path: "/", max_age: 0]], :req3}
])
assert get_json(:req1, [k1: :v1, k2: :v2]) == {:halt, :req4, [k1: :v1, k2: :v2]}
expect(:cowboy_req, :reply, [
{[302, %{"location" => "/cookies"}, "Redirecting...", :req3], :req4}
])
assert validate :cowboy_req
assert get_json(:req1, k1: :v1, k2: :v2) == {:halt, :req4, [k1: :v1, k2: :v2]}
assert validate(:cowboy_req)
end
end

View file

@ -10,41 +10,41 @@ defmodule HTTParrot.RedirectHandlerTest do
end
test "malformed_request returns true if it's not an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"a2B=", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "a2B="}])
assert malformed_request(:req1, :state) == {true, :req2, :state}
assert malformed_request(:req1, :state) == {true, :req1, :state}
assert validate(:cowboy_req)
end
test "malformed_request returns false if it's an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
assert malformed_request(:req1, :state) == {false, :req2, 2}
assert malformed_request(:req1, :state) == {false, :req1, 2}
assert validate(:cowboy_req)
end
test "malformed_request returns 1 if 'n' is less than 1" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"0", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "0"}])
assert malformed_request(:req1, :state) == {false, :req2, 1}
assert malformed_request(:req1, :state) == {false, :req1, 1}
assert validate(:cowboy_req)
end
test "moved_permanently returns 'redirect/n-1' if n > 1" do
expect(:cowboy_req, :uri, 1, {"host", :req2})
expect(:cowboy_req, :uri, 1, "host")
assert moved_permanently(:req1, 4) == {{true, "host/redirect/3"}, :req2, nil}
assert moved_permanently(:req1, 4) == {{true, "host/redirect/3"}, :req1, nil}
assert validate(:cowboy_req)
end
test "moved_permanently returns '/get' if n = 1" do
expect(:cowboy_req, :uri, 1, {"host", :req2})
expect(:cowboy_req, :uri, 1, "host")
assert moved_permanently(:req1, 1) == {{true, "host/get"}, :req2, nil}
assert moved_permanently(:req1, 1) == {{true, "host/get"}, :req1, nil}
assert validate(:cowboy_req)
end

View file

@ -4,33 +4,33 @@ defmodule HTTParrot.RelativeRedirectHandlerTest do
import HTTParrot.RelativeRedirectHandler
setup do
new :cowboy_req
on_exit fn -> unload() end
new(:cowboy_req)
on_exit(fn -> unload() end)
:ok
end
test "malformed_request returns false if it's not an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"a2B=", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "a2B="}])
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
test "malformed_request returns false if it's an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
assert malformed_request(:req1, :state) == {false, :req2, 2}
assert malformed_request(:req1, :state) == {false, :req1, 2}
assert validate :cowboy_req
assert validate(:cowboy_req)
end
test "malformed_request returns 1 if 'n' is less than 1" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"0", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "0"}])
assert malformed_request(:req1, :state) == {false, :req2, 1}
assert malformed_request(:req1, :state) == {false, :req1, 1}
assert validate :cowboy_req
assert validate(:cowboy_req)
end
test "moved_permanently returns 'redirect/n-1' if n > 1" do
@ -40,5 +40,4 @@ defmodule HTTParrot.RelativeRedirectHandlerTest do
test "moved_permanently returns '/get' if n = 1" do
assert moved_permanently(:req1, 1) == {{true, "/get"}, :req1, nil}
end
end

View file

@ -11,17 +11,17 @@ defmodule HTTParrot.ResponseHeadersHandlerTest do
end
test "malformed_request returns true if query string is empty" do
expect(:cowboy_req, :parse_qs, 1, {[], :req2})
expect(:cowboy_req, :parse_qs, 1, [])
assert malformed_request(:req1, :state) == {true, :req2, :state}
assert malformed_request(:req1, :state) == {true, :req1, :state}
assert validate(:cowboy_req)
end
test "malformed_request returns false if query string is not empty" do
expect(:cowboy_req, :parse_qs, 1, {[{"foo", "bar"}], :req2})
expect(:cowboy_req, :parse_qs, 1, [{"foo", "bar"}])
assert malformed_request(:req1, :state) == {false, :req2, [{"foo", "bar"}]}
assert malformed_request(:req1, :state) == {false, :req1, [{"foo", "bar"}]}
assert validate(:cowboy_req)
end

View file

@ -10,62 +10,62 @@ defmodule HTTParrot.SetCookiesHandlerTest do
end
test "malformed_request returns false if /name/value is sent" do
expect(:cowboy_req, :parse_qs, 1, {[], :req2})
expect(:cowboy_req, :parse_qs, 1, [])
expect(:cowboy_req, :binding, [
{[:name, :req2, nil], {"name", :req3}},
{[:value, :req3, nil], {"value", :req4}}
{[:name, :req1, nil], "name"},
{[:value, :req1, nil], "value"}
])
assert malformed_request(:req1, :state) == {false, :req4, [{"name", "value"}]}
assert malformed_request(:req1, :state) == {false, :req1, [{"name", "value"}]}
assert validate(:cowboy_req)
end
test "malformed_request returns query string values too if /name/value is sent" do
expect(:cowboy_req, :parse_qs, 1, {[{"name", "value2"}], :req2})
expect(:cowboy_req, :parse_qs, 1, [{"name", "value2"}])
expect(:cowboy_req, :binding, [
{[:name, :req2, nil], {"name", :req3}},
{[:value, :req3, nil], {"value", :req4}}
{[:name, :req1, nil], "name"},
{[:value, :req1, nil], "value"}
])
expect(:cowboy_req, :binding, [
{[:name, :req2, nil], {"name", :req3}},
{[:value, :req3, nil], {"value", :req4}}
{[:name, :req1, nil], "name"},
{[:value, :req1, nil], "value"}
])
assert malformed_request(:req1, :state) == {false, :req4, [{"name", "value"}]}
assert malformed_request(:req1, :state) == {false, :req1, [{"name", "value"}]}
assert validate(:cowboy_req)
end
test "malformed_request returns false if query string values are sent" do
expect(:cowboy_req, :parse_qs, 1, {[{"name", "value"}], :req2})
expect(:cowboy_req, :binding, [{[:name, :req2, nil], {nil, :req3}}])
expect(:cowboy_req, :parse_qs, 1, [{"name", "value"}])
expect(:cowboy_req, :binding, [{[:name, :req1, nil], nil}])
assert malformed_request(:req1, :state) == {false, :req3, [{"name", "value"}]}
assert malformed_request(:req1, :state) == {false, :req1, [{"name", "value"}]}
assert validate(:cowboy_req)
end
test "malformed_request returns true if query string values are not sent" do
expect(:cowboy_req, :parse_qs, 1, {[], :req2})
expect(:cowboy_req, :binding, [{[:name, :req2, nil], {nil, :req3}}])
expect(:cowboy_req, :parse_qs, 1, [])
expect(:cowboy_req, :binding, [{[:name, :req1, nil], nil}])
assert malformed_request(:req1, :state) == {true, :req3, :state}
assert malformed_request(:req1, :state) == {true, :req1, :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}
{[:k1, :v1, :req1, [path: "/"]], :req2},
{[:k2, :v2, :req2, [path: "/"]], :req3}
])
expect(:cowboy_req, :reply, [
{[302, [{"location", "/cookies"}], "Redirecting...", :req3], {:ok, :req4}}
{[302, %{"location" => "/cookies"}, "Redirecting...", :req3], :req4}
])
assert get_json(:req1, k1: :v1, k2: :v2) == {:halt, :req4, [k1: :v1, k2: :v2]}

View file

@ -4,16 +4,16 @@ defmodule HTTParrot.StatusCodeHandlerTest do
import HTTParrot.StatusCodeHandler
setup do
new :cowboy_req
on_exit fn -> unload() end
new(:cowboy_req)
on_exit(fn -> unload() end)
:ok
end
test "reply with 'code' as status code" do
code = 123
expect(:cowboy_req, :binding, [{[:code, :req1], {code, :req2}}])
expect(:cowboy_req, :reply, [{[code, [], "", :req2], {:ok, :req3}}])
assert get_json(:req1, :state) == {:halt, :req3, :state}
assert validate :cowboy_req
expect(:cowboy_req, :binding, [{[:code, :req1], code}])
expect(:cowboy_req, :reply, [{[code, %{}, "", :req1], :req2}])
assert get_json(:req1, :state) == {:halt, :req2, :state}
assert validate(:cowboy_req)
end
end

View file

@ -10,91 +10,91 @@ defmodule HTTParrot.StreamBytesHandlerTest do
end
test "malformed_request returns true if n is not an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"a2B=", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "a2B="}])
expect(:cowboy_req, :match_qs, fn [name, req], default ->
expect(:cowboy_req, :match_qs, fn [{name, [], default}], req ->
case {name, req, default} do
{"seed", :req2, "1234"} -> {"1234", :req3}
{"chunk_size", :req3, "1024"} -> {"1024", :req4}
{:seed, :req1, "1234"} -> %{seed: "1234"}
{:chunk_size, :req1, "1024"} -> %{chunk_size: "1024"}
end
end)
assert malformed_request(:req1, :state) == {true, :req4, :state}
assert malformed_request(:req1, :state) == {true, :req1, :state}
assert validate(:cowboy_req)
end
test "malformed_request returns false if n is an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
expect(:cowboy_req, :match_qs, fn [name, req], default ->
expect(:cowboy_req, :match_qs, fn [{name, [], default}], req ->
case {name, req, default} do
{"seed", :req2, "1234"} -> {"1234", :req3}
{"chunk_size", :req3, "1024"} -> {"1024", :req4}
{:seed, :req1, "1234"} -> %{seed: "1234"}
{:chunk_size, :req1, "1024"} -> %{chunk_size: "1024"}
end
end)
assert malformed_request(:req1, :state) == {false, :req4, {2, 1234, 1024}}
assert malformed_request(:req1, :state) == {false, :req1, {2, 1234, 1024}}
assert validate(:cowboy_req)
end
test "malformed_request returns true if seed is not an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
expect(:cowboy_req, :match_qs, fn [name, req], default ->
expect(:cowboy_req, :match_qs, fn [{name, [], default}], req ->
case {name, req, default} do
{"seed", :req2, "1234"} -> {"a2B=", :req3}
{"chunk_size", :req3, "1024"} -> {"1024", :req4}
{:seed, :req1, "1234"} -> %{seed: "a2B="}
{:chunk_size, :req1, "1024"} -> %{chunk_size: "1024"}
end
end)
assert malformed_request(:req1, :state) == {true, :req4, :state}
assert malformed_request(:req1, :state) == {true, :req1, :state}
assert validate(:cowboy_req)
end
test "malformed_request returns false if seed is an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
expect(:cowboy_req, :match_qs, fn [name, req], default ->
expect(:cowboy_req, :match_qs, fn [{name, [], default}], req ->
case {name, req, default} do
{"seed", :req2, "1234"} -> {"7", :req3}
{"chunk_size", :req3, "1024"} -> {"1024", :req4}
{:seed, :req1, "1234"} -> %{seed: "7"}
{:chunk_size, :req1, "1024"} -> %{chunk_size: "1024"}
end
end)
assert malformed_request(:req1, :state) == {false, :req4, {2, 7, 1024}}
assert malformed_request(:req1, :state) == {false, :req1, {2, 7, 1024}}
assert validate(:cowboy_req)
end
test "malformed_request returns true if chunk_size is not an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
expect(:cowboy_req, :match_qs, fn [name, req], default ->
expect(:cowboy_req, :match_qs, fn [{name, [], default}], req ->
case {name, req, default} do
{"seed", :req2, "1234"} -> {"1234", :req3}
{"chunk_size", :req3, "1024"} -> {"a2B=", :req4}
{:seed, :req1, "1234"} -> %{seed: "1234"}
{:chunk_size, :req1, "1024"} -> %{chunk_size: "a2B="}
end
end)
assert malformed_request(:req1, :state) == {true, :req4, :state}
assert malformed_request(:req1, :state) == {true, :req1, :state}
assert validate(:cowboy_req)
end
test "malformed_request returns false if chunk_size is an integer" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
expect(:cowboy_req, :binding, [{[:n, :req1], "2"}])
expect(:cowboy_req, :match_qs, fn [name, req], default ->
expect(:cowboy_req, :match_qs, fn [{name, [], default}], req ->
case {name, req, default} do
{"seed", :req2, "1234"} -> {"1234", :req3}
{"chunk_size", :req3, "1024"} -> {"13", :req4}
{:seed, :req1, "1234"} -> %{seed: "1234"}
{:chunk_size, :req1, "1024"} -> %{chunk_size: "13"}
end
end)
assert malformed_request(:req1, :state) == {false, :req4, {2, 1234, 13}}
assert malformed_request(:req1, :state) == {false, :req1, {2, 1234, 13}}
assert validate(:cowboy_req)
end