rest_handler_SUITE.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. {"/provide_callback_missing", provide_callback_missing_h, []},
  33. {"/switch_handler", switch_handler_h, run},
  34. {"/switch_handler_opts", switch_handler_h, hibernate}
  35. ]}]).
  36. %% Internal.
  37. do_decode(Headers, Body) ->
  38. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  39. {_, <<"gzip">>} -> zlib:gunzip(Body);
  40. _ -> Body
  41. end.
  42. %% Tests.
  43. provide_callback_missing(Config) ->
  44. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  45. ConnPid = gun_open(Config),
  46. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  47. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  48. ok.
  49. switch_handler(Config) ->
  50. doc("Switch REST to loop handler for streaming the response body."),
  51. ConnPid = gun_open(Config),
  52. Ref = gun:get(ConnPid, "/switch_handler", [{<<"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.
  57. switch_handler_opts(Config) ->
  58. doc("Switch REST to loop handler for streaming the response body; with options."),
  59. ConnPid = gun_open(Config),
  60. Ref = gun:get(ConnPid, "/switch_handler_opts", [{<<"accept-encoding">>, <<"gzip">>}]),
  61. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  62. {ok, Body} = gun:await_body(ConnPid, Ref),
  63. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  64. ok.