compress_SUITE.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. {group, h3_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_test:stop_group(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_malformed(Config) ->
  54. doc("Send malformed accept-encoding; get an uncompressed response."),
  55. {200, Headers, _} = do_get("/reply/large",
  56. [{<<"accept-encoding">>, <<";">>}], Config),
  57. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  58. false = lists:keyfind(<<"vary">>, 1, Headers),
  59. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  60. ok.
  61. gzip_accept_encoding_missing(Config) ->
  62. doc("Don't send accept-encoding; get an uncompressed response."),
  63. {200, Headers, _} = do_get("/reply/large",
  64. [], Config),
  65. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  66. false = lists:keyfind(<<"vary">>, 1, Headers),
  67. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  68. ok.
  69. gzip_accept_encoding_no_gzip(Config) ->
  70. doc("Send accept-encoding: compress (unsupported by Cowboy); get an uncompressed response."),
  71. {200, Headers, _} = do_get("/reply/large",
  72. [{<<"accept-encoding">>, <<"compress">>}], Config),
  73. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  74. false = lists:keyfind(<<"vary">>, 1, Headers),
  75. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  76. ok.
  77. gzip_accept_encoding_not_supported(Config) ->
  78. doc("Send unsupported accept-encoding; get an uncompressed response."),
  79. {200, Headers, _} = do_get("/reply/large",
  80. [{<<"accept-encoding">>, <<"application/gzip">>}], Config),
  81. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  82. false = lists:keyfind(<<"vary">>, 1, Headers),
  83. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  84. ok.
  85. gzip_reply_content_encoding(Config) ->
  86. doc("Reply with content-encoding header; get an uncompressed response."),
  87. {200, Headers, _} = do_get("/reply/content-encoding",
  88. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  89. %% We set the content-encoding to compress; without actually compressing.
  90. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  91. %% The reply didn't include a vary header.
  92. false = lists:keyfind(<<"vary">>, 1, Headers),
  93. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  94. ok.
  95. gzip_reply_large_body(Config) ->
  96. doc("Reply a large body; get a gzipped response."),
  97. {200, Headers, GzBody} = do_get("/reply/large",
  98. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  99. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  100. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  101. {_, Length} = lists:keyfind(<<"content-length">>, 1, Headers),
  102. ct:log("Original length: 100000; compressed: ~s.", [Length]),
  103. _ = zlib:gunzip(GzBody),
  104. ok.
  105. gzip_reply_sendfile(Config) ->
  106. doc("Reply using sendfile; get an uncompressed response."),
  107. {200, Headers, Body} = do_get("/reply/sendfile",
  108. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  109. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  110. false = lists:keyfind(<<"vary">>, 1, Headers),
  111. ct:log("Body received:~n~p~n", [Body]),
  112. ok.
  113. gzip_reply_small_body(Config) ->
  114. doc("Reply a small body; get an uncompressed response."),
  115. {200, Headers, _} = do_get("/reply/small",
  116. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  117. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  118. false = lists:keyfind(<<"vary">>, 1, Headers),
  119. {_, <<"100">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  120. ok.
  121. gzip_stream_reply(Config) ->
  122. doc("Stream reply; get a gzipped response."),
  123. {200, Headers, GzBody} = do_get("/stream_reply/large",
  124. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  125. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  126. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  127. _ = zlib:gunzip(GzBody),
  128. ok.
  129. gzip_stream_reply_sendfile(Config) ->
  130. doc("Stream reply using sendfile for some chunks; get a gzipped response."),
  131. {200, Headers, GzBody} = do_get("/stream_reply/sendfile",
  132. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  133. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  134. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  135. _ = zlib:gunzip(GzBody),
  136. ok.
  137. gzip_stream_reply_sendfile_fin(Config) ->
  138. doc("Stream reply using sendfile for some chunks; get a gzipped response."),
  139. {200, Headers, GzBody} = do_get("/stream_reply/sendfile_fin",
  140. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  141. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  142. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  143. _ = zlib:gunzip(GzBody),
  144. ok.
  145. gzip_stream_reply_content_encoding(Config) ->
  146. doc("Stream reply with content-encoding header; get an uncompressed response."),
  147. {200, Headers, Body} = do_get("/stream_reply/content-encoding",
  148. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  149. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  150. false = lists:keyfind(<<"vary">>, 1, Headers),
  151. 100000 = iolist_size(Body),
  152. ok.
  153. opts_compress_buffering_false(Config0) ->
  154. doc("Confirm that the compress_buffering option can be set to false, "
  155. "which is the default."),
  156. Fun = case config(ref, Config0) of
  157. https_compress -> init_https;
  158. h2_compress -> init_http2;
  159. _ -> init_http
  160. end,
  161. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  162. env => #{dispatch => init_dispatch(Config0)},
  163. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  164. compress_buffering => false
  165. }, Config0),
  166. try
  167. ConnPid = gun_open(Config),
  168. Ref = gun:get(ConnPid, "/stream_reply/delayed",
  169. [{<<"accept-encoding">>, <<"gzip">>}]),
  170. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  171. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  172. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  173. Z = zlib:open(),
  174. zlib:inflateInit(Z, 31),
  175. {data, nofin, Data1} = gun:await(ConnPid, Ref, 500),
  176. <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)),
  177. timer:sleep(1000),
  178. {data, nofin, Data2} = gun:await(ConnPid, Ref, 500),
  179. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  180. gun:close(ConnPid)
  181. after
  182. cowboy:stop_listener(?FUNCTION_NAME)
  183. end.
  184. opts_compress_buffering_true(Config0) ->
  185. doc("Confirm that the compress_buffering option can be set to true, "
  186. "and that the data received is buffered."),
  187. Fun = case config(ref, Config0) of
  188. https_compress -> init_https;
  189. h2_compress -> init_http2;
  190. _ -> init_http
  191. end,
  192. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  193. env => #{dispatch => init_dispatch(Config0)},
  194. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  195. compress_buffering => true
  196. }, Config0),
  197. try
  198. ConnPid = gun_open(Config),
  199. Ref = gun:get(ConnPid, "/stream_reply/delayed",
  200. [{<<"accept-encoding">>, <<"gzip">>}]),
  201. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  202. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  203. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  204. Z = zlib:open(),
  205. zlib:inflateInit(Z, 31),
  206. %% The data gets buffered because it is too small.
  207. %% In zlib versions before OTP 20.1 the gzip header was also buffered.
  208. <<>> = case gun:await(ConnPid, Ref, 500) of
  209. {data, nofin, Data1} ->
  210. iolist_to_binary(zlib:inflate(Z, Data1));
  211. {error, timeout} ->
  212. <<>>
  213. end,
  214. gun:close(ConnPid)
  215. after
  216. cowboy:stop_listener(?FUNCTION_NAME)
  217. end.
  218. set_options_compress_buffering_false(Config0) ->
  219. doc("Confirm that the compress_buffering option can be dynamically "
  220. "set to false by a handler and that the data received is not 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 => true
  230. }, Config0),
  231. try
  232. ConnPid = gun_open(Config),
  233. Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_false",
  234. [{<<"accept-encoding">>, <<"gzip">>}]),
  235. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  236. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  237. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  238. Z = zlib:open(),
  239. zlib:inflateInit(Z, 31),
  240. {data, nofin, Data1} = gun:await(ConnPid, Ref, 500),
  241. <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)),
  242. timer:sleep(1000),
  243. {data, nofin, Data2} = gun:await(ConnPid, Ref, 500),
  244. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  245. gun:close(ConnPid)
  246. after
  247. cowboy:stop_listener(?FUNCTION_NAME)
  248. end.
  249. set_options_compress_buffering_true(Config0) ->
  250. doc("Confirm that the compress_buffering option can be dynamically "
  251. "set to true by a handler and that the data received is buffered."),
  252. Fun = case config(ref, Config0) of
  253. https_compress -> init_https;
  254. h2_compress -> init_http2;
  255. _ -> init_http
  256. end,
  257. Config = cowboy_test:Fun(?FUNCTION_NAME, #{
  258. env => #{dispatch => init_dispatch(Config0)},
  259. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  260. compress_buffering => false
  261. }, Config0),
  262. try
  263. ConnPid = gun_open(Config),
  264. Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_true",
  265. [{<<"accept-encoding">>, <<"gzip">>}]),
  266. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  267. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  268. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  269. Z = zlib:open(),
  270. zlib:inflateInit(Z, 31),
  271. %% The data gets buffered because it is too small.
  272. %% In zlib versions before OTP 20.1 the gzip header was also buffered.
  273. <<>> = case gun:await(ConnPid, Ref, 500) of
  274. {data, nofin, Data1} ->
  275. iolist_to_binary(zlib:inflate(Z, Data1));
  276. {error, timeout} ->
  277. <<>>
  278. end,
  279. gun:close(ConnPid)
  280. after
  281. cowboy:stop_listener(?FUNCTION_NAME)
  282. end.
  283. set_options_compress_threshold_0(Config) ->
  284. doc("Confirm that the compress_threshold option can be dynamically "
  285. "set to change how large response bodies must be to be compressed."),
  286. {200, Headers, GzBody} = do_get("/reply/set_options_threshold0",
  287. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  288. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  289. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  290. _ = zlib:gunzip(GzBody),
  291. ok.
  292. vary_accept(Config) ->
  293. doc("Add accept-encoding to vary when the response has a 'vary: accept' header."),
  294. {200, Headers, _} = do_get("/reply/vary", [
  295. {<<"accept-encoding">>, <<"gzip">>},
  296. {<<"x-test-vary">>, <<"accept">>}
  297. ], Config),
  298. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  299. {_, <<"accept, accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  300. ok.
  301. vary_accept_accept_encoding(Config) ->
  302. doc("Don't change the vary value when the response has a 'vary: accept, accept-encoding' header."),
  303. {200, Headers, _} = do_get("/reply/vary", [
  304. {<<"accept-encoding">>, <<"gzip">>},
  305. {<<"x-test-vary">>, <<"accept, accept-encoding">>}
  306. ], Config),
  307. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  308. {_, <<"accept, accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  309. ok.
  310. vary_empty(Config) ->
  311. doc("Add accept-encoding to vary when the response has an empty vary header."),
  312. {200, Headers, _} = do_get("/reply/vary", [
  313. {<<"accept-encoding">>, <<"gzip">>},
  314. {<<"x-test-vary">>, <<>>}
  315. ], Config),
  316. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  317. {_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers),
  318. ok.
  319. vary_wildcard(Config) ->
  320. doc("Don't change the vary value when the response has a 'vary: *' header."),
  321. {200, Headers, _} = do_get("/reply/vary", [
  322. {<<"accept-encoding">>, <<"gzip">>},
  323. {<<"x-test-vary">>, <<"*">>}
  324. ], Config),
  325. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  326. {_, <<"*">>} = lists:keyfind(<<"vary">>, 1, Headers),
  327. ok.