compress_SUITE.erl 13 KB

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