diff options
Diffstat (limited to 'test/handlers')
-rw-r--r-- | test/handlers/echo_h.erl | 32 | ||||
-rw-r--r-- | test/handlers/multipart_h.erl | 65 | ||||
-rw-r--r-- | test/handlers/resp_h.erl | 158 |
3 files changed, 253 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]); diff --git a/test/handlers/multipart_h.erl b/test/handlers/multipart_h.erl new file mode 100644 index 0000000..289d2ed --- /dev/null +++ b/test/handlers/multipart_h.erl @@ -0,0 +1,65 @@ +%% This module reads a multipart body and echoes it back as an Erlang term. + +-module(multipart_h). + +-export([init/2]). + +init(Req0, State) -> + {Result, Req} = case cowboy_req:binding(key, Req0) of + undefined -> acc_multipart(Req0, []); + <<"skip_body">> -> skip_body_multipart(Req0, []); + <<"read_part2">> -> read_part2_multipart(Req0, []); + <<"read_part_body2">> -> read_part_body2_multipart(Req0, []) + end, + {ok, cowboy_req:reply(200, #{}, term_to_binary(Result), Req), State}. + +acc_multipart(Req0, Acc) -> + case cowboy_req:part(Req0) of + {ok, Headers, Req1} -> + {ok, Body, Req} = stream_body(Req1, <<>>), + acc_multipart(Req, [{Headers, Body}|Acc]); + {done, Req} -> + {lists:reverse(Acc), Req} + end. + +stream_body(Req0, Acc) -> + case cowboy_req:part_body(Req0) of + {more, Data, Req} -> + stream_body(Req, << Acc/binary, Data/binary >>); + {ok, Data, Req} -> + {ok, << Acc/binary, Data/binary >>, Req} + end. + +skip_body_multipart(Req0, Acc) -> + case cowboy_req:part(Req0) of + {ok, Headers, Req} -> + skip_body_multipart(Req, [Headers|Acc]); + {done, Req} -> + {lists:reverse(Acc), Req} + end. + +read_part2_multipart(Req0, Acc) -> + case cowboy_req:part(Req0, #{length => 1, period => 1}) of + {ok, Headers, Req1} -> + {ok, Body, Req} = stream_body(Req1, <<>>), + acc_multipart(Req, [{Headers, Body}|Acc]); + {done, Req} -> + {lists:reverse(Acc), Req} + end. + +read_part_body2_multipart(Req0, Acc) -> + case cowboy_req:part(Req0) of + {ok, Headers, Req1} -> + {ok, Body, Req} = stream_body2(Req1, <<>>), + acc_multipart(Req, [{Headers, Body}|Acc]); + {done, Req} -> + {lists:reverse(Acc), Req} + end. + +stream_body2(Req0, Acc) -> + case cowboy_req:part_body(Req0, #{length => 1, period => 1}) of + {more, Data, Req} -> + stream_body(Req, << Acc/binary, Data/binary >>); + {ok, Data, Req} -> + {ok, << Acc/binary, Data/binary >>, Req} + end. diff --git a/test/handlers/resp_h.erl b/test/handlers/resp_h.erl new file mode 100644 index 0000000..36e6f13 --- /dev/null +++ b/test/handlers/resp_h.erl @@ -0,0 +1,158 @@ +%% This module echoes back the value the test is interested in. + +-module(resp_h). + +-export([init/2]). + +init(Req, Opts) -> + do(cowboy_req:binding(key, Req), Req, Opts). + +do(<<"set_resp_cookie3">>, Req0, Opts) -> + Req = case cowboy_req:binding(arg, Req0) of + undefined -> + cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0); + <<"multiple">> -> + Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0), + cowboy_req:set_resp_cookie(<<"yourcookie">>, <<"yourvalue">>, Req1); + <<"overwrite">> -> + Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0), + cowboy_req:set_resp_cookie(<<"mycookie">>, <<"overwrite">>, Req1) + end, + cowboy_req:reply(200, #{}, "OK", Req), + {ok, Req, Opts}; +do(<<"set_resp_cookie4">>, Req0, Opts) -> + Req = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", #{path => cowboy_req:path(Req0)}, Req0), + cowboy_req:reply(200, #{}, "OK", Req), + {ok, Req, Opts}; +do(<<"set_resp_header">>, Req0, Opts) -> + Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0), + cowboy_req:reply(200, #{}, "OK", Req), + {ok, Req, Opts}; +do(<<"set_resp_body">>, Req0, Opts) -> + Arg = cowboy_req:binding(arg, Req0), + Req = case Arg of + <<"sendfile">> -> + AppFile = code:where_is_file("cowboy.app"), + cowboy_req:set_resp_body({sendfile, 0, filelib:file_size(AppFile), AppFile}, Req0); + _ -> + cowboy_req:set_resp_body(<<"OK">>, Req0) + end, + case Arg of + <<"override">> -> + cowboy_req:reply(200, #{}, <<"OVERRIDE">>, Req); + _ -> + cowboy_req:reply(200, Req) + end, + {ok, Req, Opts}; +do(<<"has_resp_header">>, Req0, Opts) -> + false = cowboy_req:has_resp_header(<<"content-type">>, Req0), + Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0), + true = cowboy_req:has_resp_header(<<"content-type">>, Req), + cowboy_req:reply(200, #{}, "OK", Req), + {ok, Req, Opts}; +do(<<"has_resp_body">>, Req0, Opts) -> + case cowboy_req:binding(arg, Req0) of + <<"sendfile">> -> + %% @todo Cases for sendfile. Note that sendfile 0 is unallowed. + false = cowboy_req:has_resp_body(Req0), + Req = cowboy_req:set_resp_body({sendfile, 0, 10, code:where_is_file("cowboy.app")}, Req0), + true = cowboy_req:has_resp_body(Req), + cowboy_req:reply(200, #{}, <<"OK">>, Req), + {ok, Req, Opts}; + undefined -> + false = cowboy_req:has_resp_body(Req0), + Req = cowboy_req:set_resp_body(<<"OK">>, Req0), + true = cowboy_req:has_resp_body(Req), + cowboy_req:reply(200, #{}, Req), + {ok, Req, Opts} + end; +do(<<"delete_resp_header">>, Req0, Opts) -> + false = cowboy_req:has_resp_header(<<"content-type">>, Req0), + Req1 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0), + true = cowboy_req:has_resp_header(<<"content-type">>, Req1), + Req = cowboy_req:delete_resp_header(<<"content-type">>, Req1), + false = cowboy_req:has_resp_header(<<"content-type">>, Req), + cowboy_req:reply(200, #{}, "OK", Req), + {ok, Req, Opts}; +do(<<"reply2">>, Req, Opts) -> + case cowboy_req:binding(arg, Req) of + <<"binary">> -> + cowboy_req:reply(<<"200 GOOD">>, Req); + <<"error">> -> + ct_helper:ignore(cowboy_req, reply, 4), + cowboy_req:reply(ok, Req); + <<"twice">> -> + cowboy_req:reply(200, Req), + cowboy_req:reply(200, Req); + Status -> + cowboy_req:reply(binary_to_integer(Status), Req) + end, + {ok, Req, Opts}; +do(<<"reply3">>, Req, Opts) -> + case cowboy_req:binding(arg, Req) of + <<"error">> -> + ct_helper:ignore(cowboy_req, reply, 4), + cowboy_req:reply(200, ok, Req); + Status -> + cowboy_req:reply(binary_to_integer(Status), + #{<<"content-type">> => <<"text/plain">>}, Req) + end, + {ok, Req, Opts}; +do(<<"reply4">>, Req, Opts) -> + case cowboy_req:binding(arg, Req) of + <<"error">> -> + ct_helper:ignore(erlang, iolist_size, 1), + cowboy_req:reply(200, #{}, ok, Req); + Status -> + cowboy_req:reply(binary_to_integer(Status), #{}, <<"OK">>, Req) + end, + {ok, Req, Opts}; +do(<<"stream_reply2">>, Req, Opts) -> + case cowboy_req:binding(arg, Req) of + <<"binary">> -> + cowboy_req:stream_reply(<<"200 GOOD">>, Req); + <<"error">> -> + ct_helper:ignore(cowboy_req, stream_reply, 3), + cowboy_req:stream_reply(ok, Req); + Status -> + cowboy_req:stream_reply(binary_to_integer(Status), Req) + end, + stream_body(Req), + {ok, Req, Opts}; +do(<<"stream_reply3">>, Req, Opts) -> + case cowboy_req:binding(arg, Req) of + <<"error">> -> + ct_helper:ignore(cowboy_req, stream_reply, 3), + cowboy_req:stream_reply(200, ok, Req); + Status -> + cowboy_req:stream_reply(binary_to_integer(Status), + #{<<"content-type">> => <<"text/plain">>}, Req) + end, + stream_body(Req), + {ok, Req, Opts}; +do(<<"stream_body">>, Req, Opts) -> + %% Call stream_body without initiating streaming. + cowboy_req:stream_body(<<0:800000>>, fin, Req), + {ok, Req, Opts}; +do(<<"push">>, Req, Opts) -> + case cowboy_req:binding(arg, Req) of + <<"method">> -> + cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req, + #{method => <<"HEAD">>}); + <<"origin">> -> + cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req, + #{scheme => <<"ftp">>, host => <<"127.0.0.1">>, port => 21}); + <<"qs">> -> + cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req, + #{qs => <<"server=cowboy&version=2.0">>}); + _ -> + cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req), + %% The text/plain mime is not defined by default, so a 406 will be returned. + cowboy_req:push("/static/plain.txt", #{<<"accept">> => <<"text/plain">>}, Req) + end, + cowboy_req:reply(200, Req), + {ok, Req, Opts}. + +stream_body(Req) -> + _ = [cowboy_req:stream_body(<<0:800000>>, nofin, Req) || _ <- lists:seq(1,9)], + cowboy_req:stream_body(<<0:800000>>, fin, Req). |