rest_handler_SUITE.erl 13 KB

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