diff options
Diffstat (limited to 'test/handlers/echo_h.erl')
-rw-r--r-- | test/handlers/echo_h.erl | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/test/handlers/echo_h.erl b/test/handlers/echo_h.erl index fd45c5f..98594dc 100644 --- a/test/handlers/echo_h.erl +++ b/test/handlers/echo_h.erl @@ -12,10 +12,32 @@ init(Req, Opts) -> echo_arg(Arg, Req, Opts) end. -echo(<<"body">>, Req0, Opts) -> - {ok, Body, Req} = cowboy_req:read_body(Req0), +echo(<<"read_body">>, Req0, Opts) -> + case Opts of + #{crash := true} -> ct_helper:ignore(cowboy_req, read_body, 2); + _ -> ok + end, + {_, Body, Req} = case cowboy_req:path(Req0) of + <<"/full", _/bits>> -> read_body(Req0, <<>>); + <<"/opts", _/bits>> -> cowboy_req:read_body(Req0, Opts); + _ -> cowboy_req:read_body(Req0) + end, cowboy_req:reply(200, #{}, Body, Req), {ok, Req, Opts}; +echo(<<"read_urlencoded_body">>, Req0, Opts) -> + Path = cowboy_req:path(Req0), + case {Path, Opts} of + {<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2); + {_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2); + _ -> ok + end, + {ok, Body, Req} = case Path of + <<"/opts", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts); + <<"/crash", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts); + _ -> cowboy_req:read_urlencoded_body(Req0) + end, + cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), + {ok, Req, Opts}; echo(<<"uri">>, Req, Opts) -> Value = case cowboy_req:path_info(Req) of [<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined}); @@ -55,6 +77,12 @@ echo_arg(Arg0, Req, Opts) -> cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), {ok, Req, Opts}. +read_body(Req0, Acc) -> + case cowboy_req:read_body(Req0) of + {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req}; + {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>) + end. + value_to_iodata(V) when is_integer(V) -> integer_to_binary(V); value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1); value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~p", [V]); |