compress_SUITE.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. %% Copyright (c) 2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(compress_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. %% ct.
  21. all() ->
  22. [
  23. {group, http_compress},
  24. {group, https_compress},
  25. {group, h2_compress},
  26. {group, h2c_compress}
  27. ].
  28. groups() ->
  29. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  30. init_per_group(Name, Config) ->
  31. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  32. end_per_group(Name, _) ->
  33. cowboy:stop_listener(Name).
  34. %% Routes.
  35. init_dispatch(_Config) ->
  36. cowboy_router:compile([{"[...]", [
  37. {"/reply/:what", compress_h, reply},
  38. {"/stream_reply/:what", compress_h, stream_reply}
  39. ]}]).
  40. %% Internal.
  41. do_get(Path, ReqHeaders, Config) ->
  42. ConnPid = gun_open(Config),
  43. Ref = gun:get(ConnPid, Path, ReqHeaders),
  44. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  45. {ok, Body} = case IsFin of
  46. nofin -> gun:await_body(ConnPid, Ref);
  47. fin -> {ok, <<>>}
  48. end,
  49. gun:close(ConnPid),
  50. {Status, RespHeaders, Body}.
  51. %% Tests.
  52. gzip_accept_encoding_missing(Config) ->
  53. doc("Don't send accept-encoding; get an uncompressed response."),
  54. {200, Headers, _} = do_get("/reply/large",
  55. [], Config),
  56. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  57. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  58. ok.
  59. gzip_accept_encoding_no_gzip(Config) ->
  60. doc("Send accept-encoding: compress (unsupported by Cowboy); get an uncompressed response."),
  61. {200, Headers, _} = do_get("/reply/large",
  62. [{<<"accept-encoding">>, <<"compress">>}], Config),
  63. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  64. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  65. ok.
  66. gzip_reply_content_encoding(Config) ->
  67. doc("Reply with content-encoding header; get an uncompressed response."),
  68. {200, Headers, _} = do_get("/reply/content-encoding",
  69. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  70. %% We set the content-encoding to compress; without actually compressing.
  71. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  72. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  73. ok.
  74. gzip_reply_large_body(Config) ->
  75. doc("Reply a large body; get a gzipped response."),
  76. {200, Headers, GzBody} = do_get("/reply/large",
  77. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  78. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  79. {_, Length} = lists:keyfind(<<"content-length">>, 1, Headers),
  80. ct:log("Original length: 100000; compressed: ~s.", [Length]),
  81. _ = zlib:gunzip(GzBody),
  82. ok.
  83. gzip_reply_sendfile(Config) ->
  84. doc("Reply using sendfile; get an uncompressed response."),
  85. {200, Headers, Body} = do_get("/reply/sendfile",
  86. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  87. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  88. ct:log("Body received:~n~p~n", [Body]),
  89. ok.
  90. gzip_reply_small_body(Config) ->
  91. doc("Reply a small body; get an uncompressed response."),
  92. {200, Headers, _} = do_get("/reply/small",
  93. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  94. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  95. {_, <<"100">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  96. ok.
  97. gzip_stream_reply(Config) ->
  98. doc("Stream reply; get a gzipped response."),
  99. {200, Headers, GzBody} = do_get("/stream_reply/large",
  100. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  101. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  102. _ = zlib:gunzip(GzBody),
  103. ok.
  104. gzip_stream_reply_sendfile(Config) ->
  105. doc("Stream reply using sendfile for some chunks; get a gzipped response."),
  106. {200, Headers, GzBody} = do_get("/stream_reply/sendfile",
  107. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  108. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  109. _ = zlib:gunzip(GzBody),
  110. ok.
  111. gzip_stream_reply_sendfile_fin(Config) ->
  112. doc("Stream reply using sendfile for some chunks; get a gzipped response."),
  113. {200, Headers, GzBody} = do_get("/stream_reply/sendfile_fin",
  114. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  115. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  116. _ = zlib:gunzip(GzBody),
  117. ok.
  118. gzip_stream_reply_content_encoding(Config) ->
  119. doc("Stream reply with content-encoding header; get an uncompressed response."),
  120. {200, Headers, Body} = do_get("/stream_reply/content-encoding",
  121. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  122. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  123. 100000 = iolist_size(Body),
  124. ok.
  125. opts_compress_buffering_false(Config0) ->
  126. doc("Confirm that the compress_buffering option can be set to false, "
  127. "which is the default."),
  128. Fun = case config(ref, Config0) of
  129. https_compress -> init_https;
  130. h2_compress -> init_http2;
  131. _ -> init_http
  132. end,
  133. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  134. env => #{dispatch => init_dispatch(Config0)},
  135. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  136. compress_buffering => false
  137. }, Config0),
  138. try
  139. ConnPid = gun_open(Config),
  140. Ref = gun:get(ConnPid, "/stream_reply/delayed",
  141. [{<<"accept-encoding">>, <<"gzip">>}]),
  142. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  143. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  144. Z = zlib:open(),
  145. zlib:inflateInit(Z, 31),
  146. {data, nofin, Data1} = gun:await(ConnPid, Ref, 500),
  147. <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)),
  148. timer:sleep(1000),
  149. {data, nofin, Data2} = gun:await(ConnPid, Ref, 500),
  150. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  151. gun:close(ConnPid)
  152. after
  153. cowboy:stop_listener(?FUNCTION_NAME)
  154. end.
  155. opts_compress_buffering_true(Config0) ->
  156. doc("Confirm that the compress_buffering option can be set to true, "
  157. "and that the data received is buffered."),
  158. Fun = case config(ref, Config0) of
  159. https_compress -> init_https;
  160. h2_compress -> init_http2;
  161. _ -> init_http
  162. end,
  163. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  164. env => #{dispatch => init_dispatch(Config0)},
  165. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  166. compress_buffering => true
  167. }, Config0),
  168. try
  169. ConnPid = gun_open(Config),
  170. Ref = gun:get(ConnPid, "/stream_reply/delayed",
  171. [{<<"accept-encoding">>, <<"gzip">>}]),
  172. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  173. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  174. Z = zlib:open(),
  175. zlib:inflateInit(Z, 31),
  176. %% The data gets buffered because it is too small.
  177. %% In zlib versions before OTP 20.1 the gzip header was also buffered.
  178. <<>> = case gun:await(ConnPid, Ref, 500) of
  179. {data, nofin, Data1} ->
  180. iolist_to_binary(zlib:inflate(Z, Data1));
  181. {error, timeout} ->
  182. <<>>
  183. end,
  184. gun:close(ConnPid)
  185. after
  186. cowboy:stop_listener(?FUNCTION_NAME)
  187. end.
  188. set_options_compress_buffering_false(Config0) ->
  189. doc("Confirm that the compress_buffering option can be dynamically "
  190. "set to false by a handler and that the data received is not buffered."),
  191. Fun = case config(ref, Config0) of
  192. https_compress -> init_https;
  193. h2_compress -> init_http2;
  194. _ -> init_http
  195. end,
  196. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  197. env => #{dispatch => init_dispatch(Config0)},
  198. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  199. compress_buffering => true
  200. }, Config0),
  201. try
  202. ConnPid = gun_open(Config),
  203. Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_false",
  204. [{<<"accept-encoding">>, <<"gzip">>}]),
  205. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  206. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  207. Z = zlib:open(),
  208. zlib:inflateInit(Z, 31),
  209. {data, nofin, Data1} = gun:await(ConnPid, Ref, 500),
  210. <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)),
  211. timer:sleep(1000),
  212. {data, nofin, Data2} = gun:await(ConnPid, Ref, 500),
  213. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  214. gun:close(ConnPid)
  215. after
  216. cowboy:stop_listener(?FUNCTION_NAME)
  217. end.
  218. set_options_compress_buffering_true(Config0) ->
  219. doc("Confirm that the compress_buffering option can be dynamically "
  220. "set to true by a handler and that the data received is buffered."),
  221. Fun = case config(ref, Config0) of
  222. https_compress -> init_https;
  223. h2_compress -> init_http2;
  224. _ -> init_http
  225. end,
  226. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  227. env => #{dispatch => init_dispatch(Config0)},
  228. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  229. compress_buffering => false
  230. }, Config0),
  231. try
  232. ConnPid = gun_open(Config),
  233. Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_true",
  234. [{<<"accept-encoding">>, <<"gzip">>}]),
  235. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  236. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  237. Z = zlib:open(),
  238. zlib:inflateInit(Z, 31),
  239. %% The data gets buffered because it is too small.
  240. %% In zlib versions before OTP 20.1 the gzip header was also buffered.
  241. <<>> = case gun:await(ConnPid, Ref, 500) of
  242. {data, nofin, Data1} ->
  243. iolist_to_binary(zlib:inflate(Z, Data1));
  244. {error, timeout} ->
  245. <<>>
  246. end,
  247. gun:close(ConnPid)
  248. after
  249. cowboy:stop_listener(?FUNCTION_NAME)
  250. end.
  251. set_options_compress_threshold_0(Config) ->
  252. doc("Confirm that the compress_threshold option can be dynamically "
  253. "set to change how large response bodies must be to be compressed."),
  254. {200, Headers, GzBody} = do_get("/reply/set_options_threshold0",
  255. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  256. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  257. _ = zlib:gunzip(GzBody),
  258. ok.