compress_SUITE.erl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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(ct_helper, [name/0]).
  20. -import(cowboy_test, [gun_open/1]).
  21. %% ct.
  22. all() ->
  23. [
  24. {group, http_compress},
  25. {group, https_compress},
  26. {group, h2_compress},
  27. {group, h2c_compress}
  28. ].
  29. groups() ->
  30. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  31. init_per_group(Name, Config) ->
  32. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  33. end_per_group(Name, _) ->
  34. cowboy:stop_listener(Name).
  35. %% Routes.
  36. init_dispatch(_Config) ->
  37. cowboy_router:compile([{"[...]", [
  38. {"/reply/:what", compress_h, reply},
  39. {"/stream_reply/:what", compress_h, stream_reply}
  40. ]}]).
  41. %% Internal.
  42. do_get(Path, ReqHeaders, Config) ->
  43. ConnPid = gun_open(Config),
  44. Ref = gun:get(ConnPid, Path, ReqHeaders),
  45. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  46. {ok, Body} = case IsFin of
  47. nofin -> gun:await_body(ConnPid, Ref);
  48. fin -> {ok, <<>>}
  49. end,
  50. gun:close(ConnPid),
  51. {Status, RespHeaders, Body}.
  52. %% Tests.
  53. gzip_accept_encoding_missing(Config) ->
  54. doc("Don't send accept-encoding; get an uncompressed response."),
  55. {200, Headers, _} = do_get("/reply/large",
  56. [], Config),
  57. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  58. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  59. ok.
  60. gzip_accept_encoding_no_gzip(Config) ->
  61. doc("Send accept-encoding: compress (unsupported by Cowboy); get an uncompressed response."),
  62. {200, Headers, _} = do_get("/reply/large",
  63. [{<<"accept-encoding">>, <<"compress">>}], Config),
  64. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  65. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  66. ok.
  67. gzip_reply_content_encoding(Config) ->
  68. doc("Reply with content-encoding header; get an uncompressed response."),
  69. {200, Headers, _} = do_get("/reply/content-encoding",
  70. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  71. %% We set the content-encoding to compress; without actually compressing.
  72. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  73. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  74. ok.
  75. gzip_reply_large_body(Config) ->
  76. doc("Reply a large body; get a gzipped response."),
  77. {200, Headers, GzBody} = do_get("/reply/large",
  78. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  79. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  80. {_, Length} = lists:keyfind(<<"content-length">>, 1, Headers),
  81. ct:log("Original length: 100000; compressed: ~s.", [Length]),
  82. _ = zlib:gunzip(GzBody),
  83. ok.
  84. gzip_reply_sendfile(Config) ->
  85. doc("Reply using sendfile; get an uncompressed response."),
  86. {200, Headers, Body} = do_get("/reply/sendfile",
  87. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  88. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  89. ct:log("Body received:~n~p~n", [Body]),
  90. ok.
  91. gzip_reply_small_body(Config) ->
  92. doc("Reply a small body; get an uncompressed response."),
  93. {200, Headers, _} = do_get("/reply/small",
  94. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  95. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  96. {_, <<"100">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  97. ok.
  98. gzip_stream_reply(Config) ->
  99. doc("Stream reply; get a gzipped response."),
  100. {200, Headers, GzBody} = do_get("/stream_reply/large",
  101. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  102. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  103. _ = zlib:gunzip(GzBody),
  104. ok.
  105. gzip_stream_reply_sendfile(Config) ->
  106. doc("Stream reply using sendfile for some chunks; get a gzipped response."),
  107. {200, Headers, GzBody} = do_get("/stream_reply/sendfile",
  108. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  109. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  110. _ = zlib:gunzip(GzBody),
  111. ok.
  112. gzip_stream_reply_sendfile_fin(Config) ->
  113. doc("Stream reply using sendfile for some chunks; get a gzipped response."),
  114. {200, Headers, GzBody} = do_get("/stream_reply/sendfile_fin",
  115. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  116. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  117. _ = zlib:gunzip(GzBody),
  118. ok.
  119. gzip_stream_reply_content_encoding(Config) ->
  120. doc("Stream reply with content-encoding header; get an uncompressed response."),
  121. {200, Headers, Body} = do_get("/stream_reply/content-encoding",
  122. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  123. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  124. 100000 = iolist_size(Body),
  125. ok.
  126. opts_compress_buffering_false(Config0) ->
  127. doc("Confirm that the compress_buffering option can be set to false, "
  128. "which is the default."),
  129. Name = name(),
  130. Fun = case config(ref, Config0) of
  131. https_compress -> init_https;
  132. h2_compress -> init_http2;
  133. _ -> init_http
  134. end,
  135. Config = cowboy_test:Fun(Name, #{
  136. env => #{dispatch => init_dispatch(Config0)},
  137. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  138. compress_buffering => false
  139. }, Config0),
  140. try
  141. ConnPid = gun_open(Config),
  142. Ref = gun:get(ConnPid, "/stream_reply/delayed",
  143. [{<<"accept-encoding">>, <<"gzip">>}]),
  144. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  145. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  146. Z = zlib:open(),
  147. zlib:inflateInit(Z, 31),
  148. {data, nofin, Data1} = gun:await(ConnPid, Ref, 100),
  149. <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)),
  150. timer:sleep(1000),
  151. {data, nofin, Data2} = gun:await(ConnPid, Ref, 100),
  152. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  153. gun:close(ConnPid)
  154. after
  155. cowboy:stop_listener(Name)
  156. end.
  157. opts_compress_buffering_true(Config0) ->
  158. doc("Confirm that the compress_buffering option can be set to true, "
  159. "and that the data received is buffered."),
  160. Name = name(),
  161. Fun = case config(ref, Config0) of
  162. https_compress -> init_https;
  163. h2_compress -> init_http2;
  164. _ -> init_http
  165. end,
  166. Config = cowboy_test:Fun(Name, #{
  167. env => #{dispatch => init_dispatch(Config0)},
  168. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  169. compress_buffering => true
  170. }, Config0),
  171. try
  172. ConnPid = gun_open(Config),
  173. Ref = gun:get(ConnPid, "/stream_reply/delayed",
  174. [{<<"accept-encoding">>, <<"gzip">>}]),
  175. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  176. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  177. Z = zlib:open(),
  178. zlib:inflateInit(Z, 31),
  179. %% The data gets buffered because it is too small.
  180. {data, nofin, Data1} = gun:await(ConnPid, Ref, 100),
  181. <<>> = iolist_to_binary(zlib:inflate(Z, Data1)),
  182. gun:close(ConnPid)
  183. after
  184. cowboy:stop_listener(Name)
  185. end.
  186. set_options_compress_buffering_false(Config0) ->
  187. doc("Confirm that the compress_buffering option can be dynamically "
  188. "set to false by a handler and that the data received is not buffered."),
  189. Name = name(),
  190. Fun = case config(ref, Config0) of
  191. https_compress -> init_https;
  192. h2_compress -> init_http2;
  193. _ -> init_http
  194. end,
  195. Config = cowboy_test:Fun(Name, #{
  196. env => #{dispatch => init_dispatch(Config0)},
  197. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  198. compress_buffering => true
  199. }, Config0),
  200. try
  201. ConnPid = gun_open(Config),
  202. Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_false",
  203. [{<<"accept-encoding">>, <<"gzip">>}]),
  204. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  205. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  206. Z = zlib:open(),
  207. zlib:inflateInit(Z, 31),
  208. {data, nofin, Data1} = gun:await(ConnPid, Ref, 100),
  209. <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)),
  210. timer:sleep(1000),
  211. {data, nofin, Data2} = gun:await(ConnPid, Ref, 100),
  212. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  213. gun:close(ConnPid)
  214. after
  215. cowboy:stop_listener(Name)
  216. end.
  217. set_options_compress_buffering_true(Config0) ->
  218. doc("Confirm that the compress_buffering option can be dynamically "
  219. "set to true by a handler and that the data received is buffered."),
  220. Name = name(),
  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(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. {data, nofin, Data1} = gun:await(ConnPid, Ref, 100),
  241. <<>> = iolist_to_binary(zlib:inflate(Z, Data1)),
  242. gun:close(ConnPid)
  243. after
  244. cowboy:stop_listener(Name)
  245. end.
  246. set_options_compress_threshold_0(Config) ->
  247. doc("Confirm that the compress_threshold option can be dynamically "
  248. "set to change how large response bodies must be to be compressed."),
  249. {200, Headers, GzBody} = do_get("/reply/set_options_threshold0",
  250. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  251. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  252. _ = zlib:gunzip(GzBody),
  253. ok.