rest_handler_SUITE.erl 2.3 KB

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