diff options
Diffstat (limited to 'test/handlers')
-rw-r--r-- | test/handlers/compress_h.erl | 37 | ||||
-rw-r--r-- | test/handlers/echo_h.erl | 2 | ||||
-rw-r--r-- | test/handlers/resp_h.erl | 4 |
3 files changed, 40 insertions, 3 deletions
diff --git a/test/handlers/compress_h.erl b/test/handlers/compress_h.erl new file mode 100644 index 0000000..5787b66 --- /dev/null +++ b/test/handlers/compress_h.erl @@ -0,0 +1,37 @@ +%% This module sends a response body of varying sizes to test +%% the cowboy_compress_h stream handler. + +-module(compress_h). + +-export([init/2]). + +init(Req0, State=reply) -> + Req = case cowboy_req:binding(what, Req0) of + <<"small">> -> + cowboy_req:reply(200, #{}, lists:duplicate(100, $a), Req0); + <<"large">> -> + cowboy_req:reply(200, #{}, lists:duplicate(100000, $a), Req0); + <<"content-encoding">> -> + cowboy_req:reply(200, #{<<"content-encoding">> => <<"compress">>}, + lists:duplicate(100000, $a), Req0); + <<"sendfile">> -> + AppFile = code:where_is_file("cowboy.app"), + Size = filelib:file_size(AppFile), + cowboy_req:reply(200, #{}, {sendfile, 0, Size, AppFile}, Req0) + end, + {ok, Req, State}; +init(Req0, State=stream_reply) -> + Req = case cowboy_req:binding(what, Req0) of + <<"large">> -> + stream_reply(#{}, Req0); + <<"content-encoding">> -> + stream_reply(#{<<"content-encoding">> => <<"compress">>}, Req0) + end, + {ok, Req, State}. + +stream_reply(Headers, Req0) -> + Data = lists:duplicate(10000, $a), + Req = cowboy_req:stream_reply(200, Headers, Req0), + _ = [cowboy_req:stream_body(Data, nofin, Req) || _ <- lists:seq(1,9)], + cowboy_req:stream_body(Data, fin, Req), + Req. diff --git a/test/handlers/echo_h.erl b/test/handlers/echo_h.erl index fb7d8a8..5042ca8 100644 --- a/test/handlers/echo_h.erl +++ b/test/handlers/echo_h.erl @@ -79,5 +79,5 @@ read_body(Req0, Acc) -> 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]); +value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~999999p", [V]); value_to_iodata(V) -> V. diff --git a/test/handlers/resp_h.erl b/test/handlers/resp_h.erl index 658c5f8..5e6696e 100644 --- a/test/handlers/resp_h.erl +++ b/test/handlers/resp_h.erl @@ -28,7 +28,7 @@ do(<<"set_resp_header">>, Req0, Opts) -> do(<<"set_resp_headers">>, Req0, Opts) -> Req = cowboy_req:set_resp_headers(#{ <<"content-type">> => <<"text/plain">>, - <<"content-encoding">> => <<"gzip">> + <<"content-encoding">> => <<"compress">> }, Req0), {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts}; do(<<"resp_header_defined">>, Req0, Opts) -> @@ -44,7 +44,7 @@ do(<<"resp_headers">>, Req0, Opts) -> Req1 = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0), Req = cowboy_req:set_resp_headers(#{ <<"content-type">> => <<"text/plain">>, - <<"content-encoding">> => <<"gzip">> + <<"content-encoding">> => <<"compress">> }, Req1), Headers = cowboy_req:resp_headers(Req), true = maps:is_key(<<"server">>, Headers), |