compress_SUITE.erl 13 KB

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