rest_handler_SUITE.erl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(rest_handler_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. cowboy_test:common_all().
  22. groups() ->
  23. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  24. init_per_group(Name, Config) ->
  25. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  26. end_per_group(Name, _) ->
  27. cowboy:stop_listener(Name).
  28. %% Dispatch configuration.
  29. init_dispatch(_) ->
  30. cowboy_router:compile([{'_', [
  31. {"/switch_handler", switch_handler_h, run},
  32. {"/switch_handler_opts", switch_handler_h, hibernate}
  33. ]}]).
  34. %% Internal.
  35. do_decode(Headers, Body) ->
  36. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  37. {_, <<"gzip">>} -> zlib:gunzip(Body);
  38. _ -> Body
  39. end.
  40. %% Tests.
  41. switch_handler(Config) ->
  42. doc("Switch REST to loop handler for streaming the response body."),
  43. ConnPid = gun_open(Config),
  44. Ref = gun:get(ConnPid, "/switch_handler", [{<<"accept-encoding">>, <<"gzip">>}]),
  45. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  46. {ok, Body} = gun:await_body(ConnPid, Ref),
  47. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  48. ok.
  49. switch_handler_opts(Config) ->
  50. doc("Switch REST to loop handler for streaming the response body; with options."),
  51. ConnPid = gun_open(Config),
  52. Ref = gun:get(ConnPid, "/switch_handler_opts", [{<<"accept-encoding">>, <<"gzip">>}]),
  53. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  54. {ok, Body} = gun:await_body(ConnPid, Ref),
  55. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  56. ok.