From eedc9798bc1651cd21e6673116615e67f34ac9e5 Mon Sep 17 00:00:00 2001 From: Alex S Date: Fri, 28 Jun 2019 19:01:03 +0300 Subject: [PATCH] more changes --- lib/httparrot/cookies_handler.ex | 2 +- lib/httparrot/delete_cookies_handler.ex | 6 +-- lib/httparrot/redirect_handler.ex | 4 +- lib/httparrot/response_headers_handler.ex | 2 +- lib/httparrot/set_cookies_handler.ex | 10 ++-- lib/httparrot/status_code_handler.ex | 4 +- lib/httparrot/store_request_handler.ex | 4 +- lib/httparrot/stream_bytes_handler.ex | 6 +-- test/cookies_handler_test.exs | 4 +- test/delete_cookies_handler_test.exs | 19 ++++--- test/redirect_handler_test.exs | 20 ++++---- test/relative_redirect_handler_test.exs | 23 +++++---- test/response_headers_handler_test.exs | 8 +-- test/set_cookies_handler_test.exs | 38 +++++++------- test/status_code_handler_test.exs | 12 ++--- test/stream_bytes_handler_test.exs | 60 +++++++++++------------ 16 files changed, 113 insertions(+), 109 deletions(-) diff --git a/lib/httparrot/cookies_handler.ex b/lib/httparrot/cookies_handler.ex index eea78c8..e482c5f 100644 --- a/lib/httparrot/cookies_handler.ex +++ b/lib/httparrot/cookies_handler.ex @@ -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 diff --git a/lib/httparrot/delete_cookies_handler.ex b/lib/httparrot/delete_cookies_handler.ex index e5cf6f6..5847f36 100644 --- a/lib/httparrot/delete_cookies_handler.ex +++ b/lib/httparrot/delete_cookies_handler.ex @@ -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 diff --git a/lib/httparrot/redirect_handler.ex b/lib/httparrot/redirect_handler.ex index 09655f9..538b08c 100644 --- a/lib/httparrot/redirect_handler.ex +++ b/lib/httparrot/redirect_handler.ex @@ -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 diff --git a/lib/httparrot/response_headers_handler.ex b/lib/httparrot/response_headers_handler.ex index 2897718..374e4cb 100644 --- a/lib/httparrot/response_headers_handler.ex +++ b/lib/httparrot/response_headers_handler.ex @@ -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} diff --git a/lib/httparrot/set_cookies_handler.ex b/lib/httparrot/set_cookies_handler.ex index b7f9584..df7566c 100644 --- a/lib/httparrot/set_cookies_handler.ex +++ b/lib/httparrot/set_cookies_handler.ex @@ -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 diff --git a/lib/httparrot/status_code_handler.ex b/lib/httparrot/status_code_handler.ex index 72c1636..2c87cd2 100644 --- a/lib/httparrot/status_code_handler.ex +++ b/lib/httparrot/status_code_handler.ex @@ -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 diff --git a/lib/httparrot/store_request_handler.ex b/lib/httparrot/store_request_handler.ex index 05e0790..a33f80d 100644 --- a/lib/httparrot/store_request_handler.ex +++ b/lib/httparrot/store_request_handler.ex @@ -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 diff --git a/lib/httparrot/stream_bytes_handler.ex b/lib/httparrot/stream_bytes_handler.ex index e5e3554..2f97aed 100644 --- a/lib/httparrot/stream_bytes_handler.ex +++ b/lib/httparrot/stream_bytes_handler.ex @@ -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() diff --git a/test/cookies_handler_test.exs b/test/cookies_handler_test.exs index fb1f97c..c06c42c 100644 --- a/test/cookies_handler_test.exs +++ b/test/cookies_handler_test.exs @@ -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) diff --git a/test/delete_cookies_handler_test.exs b/test/delete_cookies_handler_test.exs index b94581e..e7f49d6 100644 --- a/test/delete_cookies_handler_test.exs +++ b/test/delete_cookies_handler_test.exs @@ -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 diff --git a/test/redirect_handler_test.exs b/test/redirect_handler_test.exs index 5cc4287..6c23507 100644 --- a/test/redirect_handler_test.exs +++ b/test/redirect_handler_test.exs @@ -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 diff --git a/test/relative_redirect_handler_test.exs b/test/relative_redirect_handler_test.exs index 5c445f3..a62ad1c 100644 --- a/test/relative_redirect_handler_test.exs +++ b/test/relative_redirect_handler_test.exs @@ -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 diff --git a/test/response_headers_handler_test.exs b/test/response_headers_handler_test.exs index 2155a8f..f3d9d6c 100644 --- a/test/response_headers_handler_test.exs +++ b/test/response_headers_handler_test.exs @@ -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 diff --git a/test/set_cookies_handler_test.exs b/test/set_cookies_handler_test.exs index 1839bf0..fb5dc12 100644 --- a/test/set_cookies_handler_test.exs +++ b/test/set_cookies_handler_test.exs @@ -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]} diff --git a/test/status_code_handler_test.exs b/test/status_code_handler_test.exs index 0c9a4a1..b114000 100644 --- a/test/status_code_handler_test.exs +++ b/test/status_code_handler_test.exs @@ -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 diff --git a/test/stream_bytes_handler_test.exs b/test/stream_bytes_handler_test.exs index 673a9a4..4eeb7d6 100644 --- a/test/stream_bytes_handler_test.exs +++ b/test/stream_bytes_handler_test.exs @@ -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