compress_SUITE.erl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. Fun = case config(ref, Config0) of
  130. https_compress -> init_https;
  131. h2_compress -> init_http2;
  132. _ -> init_http
  133. end,
  134. Config = cowboy_test:Fun(name(), #{
  135. env => #{dispatch => init_dispatch(Config0)},
  136. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  137. compress_buffering => false
  138. }, Config0),
  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, 100),
  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, 100),
  150. <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)),
  151. gun:close(ConnPid),
  152. cowboy:stop_listener(name()).
  153. opts_compress_buffering_true(Config0) ->
  154. doc("Confirm that the compress_buffering option can be set to true, "
  155. "and that the data received is buffered."),
  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(name(), #{
  162. env => #{dispatch => init_dispatch(Config0)},
  163. stream_handlers => [cowboy_compress_h, cowboy_stream_h],
  164. compress_buffering => true
  165. }, Config0),
  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. Z = zlib:open(),
  172. zlib:inflateInit(Z, 31),
  173. %% The data gets buffered because it is too small.
  174. {data, nofin, Data1} = gun:await(ConnPid, Ref, 100),
  175. <<>> = iolist_to_binary(zlib:inflate(Z, Data1)),
  176. gun:close(ConnPid),
  177. cowboy:stop_listener(name()).