compress_SUITE.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. %% ct.
  20. all() ->
  21. [
  22. {group, http_compress},
  23. {group, https_compress},
  24. {group, h2_compress},
  25. {group, h2c_compress}
  26. ].
  27. groups() ->
  28. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  29. init_per_group(Name, Config) ->
  30. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  31. end_per_group(Name, _) ->
  32. cowboy:stop_listener(Name).
  33. %% Routes.
  34. init_dispatch(Config) ->
  35. cowboy_router:compile([{"[...]", [
  36. {"/reply/:what", compress_h, reply},
  37. {"/stream_reply/:what", compress_h, stream_reply}
  38. ]}]).
  39. %% Internal.
  40. do_get(Path, ReqHeaders, Config) ->
  41. ConnPid = gun_open(Config),
  42. Ref = gun:get(ConnPid, Path, ReqHeaders),
  43. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  44. {ok, Body} = case IsFin of
  45. nofin -> gun:await_body(ConnPid, Ref);
  46. fin -> {ok, <<>>}
  47. end,
  48. gun:close(ConnPid),
  49. {Status, RespHeaders, Body}.
  50. %% Tests.
  51. gzip_accept_encoding_missing(Config) ->
  52. doc("Don't send accept-encoding; get an uncompressed response."),
  53. {200, Headers, _} = do_get("/reply/large",
  54. [], Config),
  55. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  56. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  57. ok.
  58. gzip_accept_encoding_no_gzip(Config) ->
  59. doc("Send accept-encoding: compress (unsupported by Cowboy); get an uncompressed response."),
  60. {200, Headers, _} = do_get("/reply/large",
  61. [{<<"accept-encoding">>, <<"compress">>}], Config),
  62. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  63. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  64. ok.
  65. gzip_reply_content_encoding(Config) ->
  66. doc("Reply with content-encoding header; get an uncompressed response."),
  67. {200, Headers, _} = do_get("/reply/content-encoding",
  68. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  69. %% We set the content-encoding to compress; without actually compressing.
  70. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  71. {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  72. ok.
  73. gzip_reply_large_body(Config) ->
  74. doc("Reply a large body; get a gzipped response."),
  75. {200, Headers, GzBody} = do_get("/reply/large",
  76. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  77. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  78. {_, Length} = lists:keyfind(<<"content-length">>, 1, Headers),
  79. ct:log("Original length: 100000; compressed: ~s.", [Length]),
  80. _ = zlib:gunzip(GzBody),
  81. ok.
  82. gzip_reply_sendfile(Config) ->
  83. doc("Reply using sendfile; get an uncompressed response."),
  84. {200, Headers, Body} = do_get("/reply/sendfile",
  85. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  86. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  87. ct:log("Body received:~n~p~n", [Body]),
  88. ok.
  89. gzip_reply_small_body(Config) ->
  90. doc("Reply a small body; get an uncompressed response."),
  91. {200, Headers, _} = do_get("/reply/small",
  92. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  93. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  94. {_, <<"100">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  95. ok.
  96. gzip_stream_reply(Config) ->
  97. doc("Stream reply; get a gzipped response."),
  98. {200, Headers, GzBody} = do_get("/stream_reply/large",
  99. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  100. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  101. _ = zlib:gunzip(GzBody),
  102. ok.
  103. gzip_stream_reply_content_encoding(Config) ->
  104. doc("Stream reply with content-encoding header; get an uncompressed response."),
  105. {200, Headers, Body} = do_get("/stream_reply/content-encoding",
  106. [{<<"accept-encoding">>, <<"gzip">>}], Config),
  107. {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers),
  108. 100000 = iolist_size(Body),
  109. ok.