rest_handler_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. {"/charsets_provided", charsets_provided_h, []},
  34. {"/charsets_provided_empty", charsets_provided_empty_h, []},
  35. {"/charset_in_content_types_provided",
  36. charset_in_content_types_provided_h, []},
  37. {"/charset_in_content_types_provided_implicit",
  38. charset_in_content_types_provided_implicit_h, []},
  39. {"/charset_in_content_types_provided_implicit_no_callback",
  40. charset_in_content_types_provided_implicit_no_callback_h, []},
  41. {"/provide_callback_missing", provide_callback_missing_h, []},
  42. {"/switch_handler", switch_handler_h, run},
  43. {"/switch_handler_opts", switch_handler_h, hibernate}
  44. ]}]).
  45. %% Internal.
  46. do_decode(Headers, Body) ->
  47. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  48. {_, <<"gzip">>} -> zlib:gunzip(Body);
  49. _ -> Body
  50. end.
  51. %% Tests.
  52. error_on_malformed_if_match(Config) ->
  53. doc("A malformed If-Match header must result in a 400 response."),
  54. ConnPid = gun_open(Config),
  55. Ref = gun:get(ConnPid, "/", [
  56. {<<"accept-encoding">>, <<"gzip">>},
  57. {<<"if-match">>, <<"bad">>}
  58. ]),
  59. {response, _, 400, _} = gun:await(ConnPid, Ref),
  60. ok.
  61. error_on_malformed_if_none_match(Config) ->
  62. doc("A malformed If-None-Match header must result in a 400 response."),
  63. ConnPid = gun_open(Config),
  64. Ref = gun:get(ConnPid, "/", [
  65. {<<"accept-encoding">>, <<"gzip">>},
  66. {<<"if-none-match">>, <<"bad">>}
  67. ]),
  68. {response, _, 400, _} = gun:await(ConnPid, Ref),
  69. ok.
  70. charset_in_content_types_provided(Config) ->
  71. doc("When a charset is matched explictly in content_types_provided, "
  72. "that charset is used and the charsets_provided callback is ignored."),
  73. ConnPid = gun_open(Config),
  74. Ref = gun:get(ConnPid, "/charset_in_content_types_provided", [
  75. {<<"accept">>, <<"text/plain;charset=utf-8">>},
  76. {<<"accept-charset">>, <<"utf-16, utf-8;q=0.5">>},
  77. {<<"accept-encoding">>, <<"gzip">>}
  78. ]),
  79. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  80. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  81. ok.
  82. charset_in_content_types_provided_implicit_match(Config) ->
  83. doc("When a charset is matched implicitly in content_types_provided, "
  84. "the charsets_provided callback is used to determine if the media "
  85. "type will match."),
  86. ConnPid = gun_open(Config),
  87. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  88. {<<"accept">>, <<"text/plain;charset=utf-16">>},
  89. {<<"accept-encoding">>, <<"gzip">>}
  90. ]),
  91. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  92. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  93. ok.
  94. charset_in_content_types_provided_implicit_nomatch(Config) ->
  95. doc("When a charset is matched implicitly in content_types_provided, "
  96. "the charsets_provided callback is used to determine if the media "
  97. "type will match. If it doesn't, try the next media type."),
  98. ConnPid = gun_open(Config),
  99. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  100. {<<"accept">>, <<"text/plain;charset=utf-32, text/plain">>},
  101. {<<"accept-encoding">>, <<"gzip">>}
  102. ]),
  103. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  104. %% We end up with the first charset listed in charsets_provided.
  105. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  106. ok.
  107. charset_in_content_types_provided_implicit_nomatch_error(Config) ->
  108. doc("When a charset is matched implicitly in content_types_provided, "
  109. "the charsets_provided callback is used to determine if the media "
  110. "type will match. If it doesn't, and there's no other media type, "
  111. "a 406 is returned."),
  112. ConnPid = gun_open(Config),
  113. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  114. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  115. {<<"accept-encoding">>, <<"gzip">>}
  116. ]),
  117. {response, _, 406, _} = gun:await(ConnPid, Ref),
  118. ok.
  119. charset_in_content_types_provided_implicit_no_callback(Config) ->
  120. doc("When a charset is matched implicitly in content_types_provided, "
  121. "and the charsets_provided callback is not exported, the media "
  122. "type will match but the charset will be ignored like all other "
  123. "parameters."),
  124. ConnPid = gun_open(Config),
  125. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit_no_callback", [
  126. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  127. {<<"accept-encoding">>, <<"gzip">>}
  128. ]),
  129. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  130. %% The charset is ignored as if it was any other parameter.
  131. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  132. ok.
  133. charsets_provided_match_text(Config) ->
  134. doc("When the media type is text and the charsets_provided callback exists "
  135. "and the accept-charset header was sent, the selected charset is sent "
  136. "back in the content-type of the response."),
  137. ConnPid = gun_open(Config),
  138. Ref = gun:get(ConnPid, "/charsets_provided", [
  139. {<<"accept">>, <<"text/plain">>},
  140. {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
  141. {<<"accept-encoding">>, <<"gzip">>}
  142. ]),
  143. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  144. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  145. ok.
  146. charsets_provided_match_other(Config) ->
  147. doc("When the media type is not text and the charsets_provided callback exists "
  148. "and the accept-charset header was sent, the selected charset is not sent "
  149. "back in the content-type of the response."),
  150. ConnPid = gun_open(Config),
  151. Ref = gun:get(ConnPid, "/charsets_provided", [
  152. {<<"accept">>, <<"application/json">>},
  153. {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
  154. {<<"accept-encoding">>, <<"gzip">>}
  155. ]),
  156. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  157. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  158. ok.
  159. charsets_provided_wildcard_text(Config) ->
  160. doc("When the media type is text and the charsets_provided callback exists "
  161. "and a wildcard accept-charset header was sent, the selected charset is sent "
  162. "back in the content-type of the response."),
  163. ConnPid = gun_open(Config),
  164. Ref = gun:get(ConnPid, "/charsets_provided", [
  165. {<<"accept">>, <<"text/plain">>},
  166. {<<"accept-charset">>, <<"*">>},
  167. {<<"accept-encoding">>, <<"gzip">>}
  168. ]),
  169. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  170. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  171. ok.
  172. charsets_provided_wildcard_other(Config) ->
  173. doc("When the media type is not text and the charsets_provided callback exists "
  174. "and a wildcard accept-charset header was sent, the selected charset is not sent "
  175. "back in the content-type of the response."),
  176. ConnPid = gun_open(Config),
  177. Ref = gun:get(ConnPid, "/charsets_provided", [
  178. {<<"accept">>, <<"application/json">>},
  179. {<<"accept-charset">>, <<"*">>},
  180. {<<"accept-encoding">>, <<"gzip">>}
  181. ]),
  182. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  183. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  184. ok.
  185. charsets_provided_nomatch(Config) ->
  186. doc("Regardless of the media type negotiated, if no charset is found in the "
  187. "accept-charset header match a charset configured in charsets_provided, "
  188. "then a 406 not acceptable response is sent back."),
  189. ConnPid = gun_open(Config),
  190. Ref = gun:get(ConnPid, "/charsets_provided", [
  191. {<<"accept">>, <<"text/plain">>},
  192. {<<"accept-charset">>, <<"utf-8;q=0, iso-8859-1">>},
  193. {<<"accept-encoding">>, <<"gzip">>}
  194. ]),
  195. {response, _, 406, _} = gun:await(ConnPid, Ref),
  196. ok.
  197. charsets_provided_noheader_text(Config) ->
  198. doc("When the media type is text and the charsets_provided callback exists "
  199. "but the accept-charset header was not sent, the first charset in the "
  200. "list is selected and sent back in the content-type of the response."),
  201. ConnPid = gun_open(Config),
  202. Ref = gun:get(ConnPid, "/charsets_provided", [
  203. {<<"accept">>, <<"text/plain">>},
  204. {<<"accept-encoding">>, <<"gzip">>}
  205. ]),
  206. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  207. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  208. ok.
  209. charsets_provided_noheader_other(Config) ->
  210. doc("When the media type is not text and the charsets_provided callback exists "
  211. "but the accept-charset header was not sent, the first charset in the "
  212. "list is selected but is not sent back in the content-type of the response."),
  213. ConnPid = gun_open(Config),
  214. Ref = gun:get(ConnPid, "/charsets_provided", [
  215. {<<"accept">>, <<"application/json">>},
  216. {<<"accept-encoding">>, <<"gzip">>}
  217. ]),
  218. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  219. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  220. ok.
  221. charsets_provided_empty(Config) ->
  222. doc("Regardless of the media type negotiated, if the charsets_provided "
  223. "callback returns an empty list a 406 not acceptable response is sent back."),
  224. ConnPid = gun_open(Config),
  225. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  226. {<<"accept">>, <<"text/plain">>},
  227. {<<"accept-charset">>, <<"utf-8q=0.5, utf-16">>},
  228. {<<"accept-encoding">>, <<"gzip">>}
  229. ]),
  230. {response, _, 406, _} = gun:await(ConnPid, Ref),
  231. ok.
  232. charsets_provided_empty_wildcard(Config) ->
  233. doc("Regardless of the media type negotiated, if the charsets_provided "
  234. "callback returns an empty list a 406 not acceptable response is sent back."),
  235. ConnPid = gun_open(Config),
  236. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  237. {<<"accept">>, <<"text/plain">>},
  238. {<<"accept-charset">>, <<"*">>},
  239. {<<"accept-encoding">>, <<"gzip">>}
  240. ]),
  241. {response, _, 406, _} = gun:await(ConnPid, Ref),
  242. ok.
  243. charsets_provided_empty_noheader(Config) ->
  244. doc("Regardless of the media type negotiated, if the charsets_provided "
  245. "callback returns an empty list a 406 not acceptable response is sent back."),
  246. ConnPid = gun_open(Config),
  247. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  248. {<<"accept">>, <<"text/plain">>},
  249. {<<"accept-encoding">>, <<"gzip">>}
  250. ]),
  251. {response, _, 406, _} = gun:await(ConnPid, Ref),
  252. ok.
  253. provide_callback_missing(Config) ->
  254. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  255. ConnPid = gun_open(Config),
  256. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  257. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  258. ok.
  259. switch_handler(Config) ->
  260. doc("Switch REST to loop handler for streaming the response body."),
  261. ConnPid = gun_open(Config),
  262. Ref = gun:get(ConnPid, "/switch_handler", [{<<"accept-encoding">>, <<"gzip">>}]),
  263. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  264. {ok, Body} = gun:await_body(ConnPid, Ref),
  265. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  266. ok.
  267. switch_handler_opts(Config) ->
  268. doc("Switch REST to loop handler for streaming the response body; with options."),
  269. ConnPid = gun_open(Config),
  270. Ref = gun:get(ConnPid, "/switch_handler_opts", [{<<"accept-encoding">>, <<"gzip">>}]),
  271. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  272. {ok, Body} = gun:await_body(ConnPid, Ref),
  273. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  274. ok.