compress_SUITE.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  58. ok.
  59. gzip_accept_encoding_no_gzip(Config) ->
  60. doc("Send accept-encoding: compress (unsupported by Cowboy); get an uncompressed response."),
  61. {200, Headers, _} = do_get("/reply/large",
  62. [{<<"accept-encoding">>, <<"compress">>}], Config),
  63. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  64. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  65. ok.
  66. gzip_reply_content_encoding(Config) ->
  67. doc("Reply with content-encoding header; get an uncompressed response."),
  68. {200, Headers, _} = do_get("/reply/content-encoding",
  69. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  70. %% We set the content-encoding to compress; without actually compressing.
  71. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  72. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  73. ok.
  74. gzip_reply_large_body(Config) ->
  75. doc("Reply a large body; get a gzipped response."),
  76. {200, Headers, GzBody} = do_get("/reply/large",
  77. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  78. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  79. {_, Length} = lists:keyfind(<<"content-length">>, 1, Headers),
  80. ct:log("Original length: 100000; compressed: ~s.", [Length]),
  81. _ = zlib:gunzip(GzBody),
  82. ok.
  83. gzip_reply_sendfile(Config) ->
  84. doc("Reply using sendfile; get an uncompressed response."),
  85. {200, Headers, Body} = do_get("/reply/sendfile",
  86. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  87. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  88. ct:log("Body received:~n~p~n", [Body]),
  89. ok.
  90. gzip_reply_small_body(Config) ->
  91. doc("Reply a small body; get an uncompressed response."),
  92. {200, Headers, _} = do_get("/reply/small",
  93. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  94. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  95. {_, <<"100">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  96. ok.
  97. gzip_stream_reply(Config) ->
  98. doc("Stream reply; get a gzipped response."),
  99. {200, Headers, GzBody} = do_get("/stream_reply/large",
  100. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  101. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  102. _ = zlib:gunzip(GzBody),
  103. ok.
  104. gzip_stream_reply_content_encoding(Config) ->
  105. doc("Stream reply with content-encoding header; get an uncompressed response."),
  106. {200, Headers, Body} = do_get("/stream_reply/content-encoding",
  107. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  108. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  109. 100000 = iolist_size(Body),
  110. ok.