rest_handler_SUITE.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. {"/", rest_hello_h, []},
  33. {"/provide_callback_missing", provide_callback_missing_h, []},
  34. {"/switch_handler", switch_handler_h, run},
  35. {"/switch_handler_opts", switch_handler_h, hibernate}
  36. ]}]).
  37. %% Internal.
  38. do_decode(Headers, Body) ->
  39. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  40. {_, <<"gzip">>} -> zlib:gunzip(Body);
  41. _ -> Body
  42. end.
  43. %% Tests.
  44. error_on_malformed_if_match(Config) ->
  45. doc("A malformed If-Match header must result in a 400 response."),
  46. ConnPid = gun_open(Config),
  47. Ref = gun:get(ConnPid, "/", [
  48. {<<"accept-encoding">>, <<"gzip">>},
  49. {<<"if-match">>, <<"bad">>}
  50. ]),
  51. {response, _, 400, _} = gun:await(ConnPid, Ref),
  52. ok.
  53. error_on_malformed_if_none_match(Config) ->
  54. doc("A malformed If-None-Match header must result in a 400 response."),
  55. ConnPid = gun_open(Config),
  56. Ref = gun:get(ConnPid, "/", [
  57. {<<"accept-encoding">>, <<"gzip">>},
  58. {<<"if-none-match">>, <<"bad">>}
  59. ]),
  60. {response, _, 400, _} = gun:await(ConnPid, Ref),
  61. ok.
  62. provide_callback_missing(Config) ->
  63. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  64. ConnPid = gun_open(Config),
  65. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  66. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  67. ok.
  68. switch_handler(Config) ->
  69. doc("Switch REST to loop handler for streaming the response body."),
  70. ConnPid = gun_open(Config),
  71. Ref = gun:get(ConnPid, "/switch_handler", [{<<"accept-encoding">>, <<"gzip">>}]),
  72. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  73. {ok, Body} = gun:await_body(ConnPid, Ref),
  74. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  75. ok.
  76. switch_handler_opts(Config) ->
  77. doc("Switch REST to loop handler for streaming the response body; with options."),
  78. ConnPid = gun_open(Config),
  79. Ref = gun:get(ConnPid, "/switch_handler_opts", [{<<"accept-encoding">>, <<"gzip">>}]),
  80. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  81. {ok, Body} = gun:await_body(ConnPid, Ref),
  82. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  83. ok.