diff options
author | Loïc Hoguin <[email protected]> | 2017-01-22 10:50:39 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2017-01-22 10:50:39 +0100 |
commit | 3e05ab8f82ac69da7c55d1b28284c32ea63186c4 (patch) | |
tree | cc13546e9579d572d7d926aeab73847a4fc56ca5 /test/handlers/compress_h.erl | |
parent | 3b91523a3c84a882dcc83f0591b919a939cb72c2 (diff) | |
download | cowboy-3e05ab8f82ac69da7c55d1b28284c32ea63186c4.tar.gz cowboy-3e05ab8f82ac69da7c55d1b28284c32ea63186c4.tar.bz2 cowboy-3e05ab8f82ac69da7c55d1b28284c32ea63186c4.zip |
Add experimental cowboy_compress_h stream handler
Currently marked as experimental because it can't be tweaked
(just enabled/disabled) and because it is not documented yet.
Diffstat (limited to 'test/handlers/compress_h.erl')
-rw-r--r-- | test/handlers/compress_h.erl | 37 |
1 files changed, 37 insertions, 0 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. |