rest_handler_SUITE.erl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. {"/charset_in_content_types_provided",
  34. charset_in_content_types_provided_h, []},
  35. {"/charset_in_content_types_provided_implicit",
  36. charset_in_content_types_provided_implicit_h, []},
  37. {"/charset_in_content_types_provided_implicit_no_callback",
  38. charset_in_content_types_provided_implicit_no_callback_h, []},
  39. {"/provide_callback_missing", provide_callback_missing_h, []},
  40. {"/switch_handler", switch_handler_h, run},
  41. {"/switch_handler_opts", switch_handler_h, hibernate}
  42. ]}]).
  43. %% Internal.
  44. do_decode(Headers, Body) ->
  45. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  46. {_, <<"gzip">>} -> zlib:gunzip(Body);
  47. _ -> Body
  48. end.
  49. %% Tests.
  50. error_on_malformed_if_match(Config) ->
  51. doc("A malformed If-Match header must result in a 400 response."),
  52. ConnPid = gun_open(Config),
  53. Ref = gun:get(ConnPid, "/", [
  54. {<<"accept-encoding">>, <<"gzip">>},
  55. {<<"if-match">>, <<"bad">>}
  56. ]),
  57. {response, _, 400, _} = gun:await(ConnPid, Ref),
  58. ok.
  59. error_on_malformed_if_none_match(Config) ->
  60. doc("A malformed If-None-Match header must result in a 400 response."),
  61. ConnPid = gun_open(Config),
  62. Ref = gun:get(ConnPid, "/", [
  63. {<<"accept-encoding">>, <<"gzip">>},
  64. {<<"if-none-match">>, <<"bad">>}
  65. ]),
  66. {response, _, 400, _} = gun:await(ConnPid, Ref),
  67. ok.
  68. charset_in_content_types_provided(Config) ->
  69. doc("When a charset is matched explictly in content_types_provided, "
  70. "that charset is used and the charsets_provided callback is ignored."),
  71. ConnPid = gun_open(Config),
  72. Ref = gun:get(ConnPid, "/charset_in_content_types_provided", [
  73. {<<"accept">>, <<"text/plain;charset=utf-8">>},
  74. {<<"accept-charset">>, <<"utf-16, utf-8;q=0.5">>},
  75. {<<"accept-encoding">>, <<"gzip">>}
  76. ]),
  77. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  78. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  79. ok.
  80. charset_in_content_types_provided_implicit_match(Config) ->
  81. doc("When a charset is matched implicitly in content_types_provided, "
  82. "the charsets_provided callback is used to determine if the media "
  83. "type will match."),
  84. ConnPid = gun_open(Config),
  85. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  86. {<<"accept">>, <<"text/plain;charset=utf-16">>},
  87. {<<"accept-encoding">>, <<"gzip">>}
  88. ]),
  89. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  90. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  91. ok.
  92. charset_in_content_types_provided_implicit_nomatch(Config) ->
  93. doc("When a charset is matched implicitly in content_types_provided, "
  94. "the charsets_provided callback is used to determine if the media "
  95. "type will match. If it doesn't, try the next media type."),
  96. ConnPid = gun_open(Config),
  97. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  98. {<<"accept">>, <<"text/plain;charset=utf-32, text/plain">>},
  99. {<<"accept-encoding">>, <<"gzip">>}
  100. ]),
  101. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  102. %% We end up with the first charset listed in charsets_provided.
  103. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  104. ok.
  105. charset_in_content_types_provided_implicit_nomatch_error(Config) ->
  106. doc("When a charset is matched implicitly in content_types_provided, "
  107. "the charsets_provided callback is used to determine if the media "
  108. "type will match. If it doesn't, and there's no other media type, "
  109. "a 406 is returned."),
  110. ConnPid = gun_open(Config),
  111. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  112. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  113. {<<"accept-encoding">>, <<"gzip">>}
  114. ]),
  115. {response, _, 406, _} = gun:await(ConnPid, Ref),
  116. ok.
  117. charset_in_content_types_provided_implicit_no_callback(Config) ->
  118. doc("When a charset is matched implicitly in content_types_provided, "
  119. "and the charsets_provided callback is not exported, the media "
  120. "type will match but the charset will be ignored like all other "
  121. "parameters."),
  122. ConnPid = gun_open(Config),
  123. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit_no_callback", [
  124. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  125. {<<"accept-encoding">>, <<"gzip">>}
  126. ]),
  127. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  128. %% The charset is ignored as if it was any other parameter.
  129. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  130. ok.
  131. provide_callback_missing(Config) ->
  132. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  133. ConnPid = gun_open(Config),
  134. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  135. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  136. ok.
  137. switch_handler(Config) ->
  138. doc("Switch REST to loop handler for streaming the response body."),
  139. ConnPid = gun_open(Config),
  140. Ref = gun:get(ConnPid, "/switch_handler", [{<<"accept-encoding">>, <<"gzip">>}]),
  141. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  142. {ok, Body} = gun:await_body(ConnPid, Ref),
  143. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  144. ok.
  145. switch_handler_opts(Config) ->
  146. doc("Switch REST to loop handler for streaming the response body; with options."),
  147. ConnPid = gun_open(Config),
  148. Ref = gun:get(ConnPid, "/switch_handler_opts", [{<<"accept-encoding">>, <<"gzip">>}]),
  149. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  150. {ok, Body} = gun:await_body(ConnPid, Ref),
  151. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  152. ok.