rest_handler_SUITE.erl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. {"/stop_handler", stop_handler_h, []},
  44. {"/switch_handler", switch_handler_h, run},
  45. {"/switch_handler_opts", switch_handler_h, hibernate}
  46. ]}]).
  47. %% Internal.
  48. do_decode(Headers, Body) ->
  49. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  50. {_, <<"gzip">>} -> zlib:gunzip(Body);
  51. _ -> Body
  52. end.
  53. %% Tests.
  54. error_on_malformed_if_match(Config) ->
  55. doc("A malformed If-Match header must result in a 400 response."),
  56. ConnPid = gun_open(Config),
  57. Ref = gun:get(ConnPid, "/", [
  58. {<<"accept-encoding">>, <<"gzip">>},
  59. {<<"if-match">>, <<"bad">>}
  60. ]),
  61. {response, _, 400, _} = gun:await(ConnPid, Ref),
  62. ok.
  63. error_on_malformed_if_none_match(Config) ->
  64. doc("A malformed If-None-Match header must result in a 400 response."),
  65. ConnPid = gun_open(Config),
  66. Ref = gun:get(ConnPid, "/", [
  67. {<<"accept-encoding">>, <<"gzip">>},
  68. {<<"if-none-match">>, <<"bad">>}
  69. ]),
  70. {response, _, 400, _} = gun:await(ConnPid, Ref),
  71. ok.
  72. charset_in_content_types_provided(Config) ->
  73. doc("When a charset is matched explictly in content_types_provided, "
  74. "that charset is used and the charsets_provided callback is ignored."),
  75. ConnPid = gun_open(Config),
  76. Ref = gun:get(ConnPid, "/charset_in_content_types_provided", [
  77. {<<"accept">>, <<"text/plain;charset=utf-8">>},
  78. {<<"accept-charset">>, <<"utf-16, utf-8;q=0.5">>},
  79. {<<"accept-encoding">>, <<"gzip">>}
  80. ]),
  81. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  82. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  83. ok.
  84. charset_in_content_types_provided_implicit_match(Config) ->
  85. doc("When a charset is matched implicitly in content_types_provided, "
  86. "the charsets_provided callback is used to determine if the media "
  87. "type will match."),
  88. ConnPid = gun_open(Config),
  89. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  90. {<<"accept">>, <<"text/plain;charset=utf-16">>},
  91. {<<"accept-encoding">>, <<"gzip">>}
  92. ]),
  93. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  94. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  95. ok.
  96. charset_in_content_types_provided_implicit_nomatch(Config) ->
  97. doc("When a charset is matched implicitly in content_types_provided, "
  98. "the charsets_provided callback is used to determine if the media "
  99. "type will match. If it doesn't, try the next media type."),
  100. ConnPid = gun_open(Config),
  101. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  102. {<<"accept">>, <<"text/plain;charset=utf-32, text/plain">>},
  103. {<<"accept-encoding">>, <<"gzip">>}
  104. ]),
  105. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  106. %% We end up with the first charset listed in charsets_provided.
  107. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  108. ok.
  109. charset_in_content_types_provided_implicit_nomatch_error(Config) ->
  110. doc("When a charset is matched implicitly in content_types_provided, "
  111. "the charsets_provided callback is used to determine if the media "
  112. "type will match. If it doesn't, and there's no other media type, "
  113. "a 406 is returned."),
  114. ConnPid = gun_open(Config),
  115. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  116. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  117. {<<"accept-encoding">>, <<"gzip">>}
  118. ]),
  119. {response, _, 406, _} = gun:await(ConnPid, Ref),
  120. ok.
  121. charset_in_content_types_provided_implicit_no_callback(Config) ->
  122. doc("When a charset is matched implicitly in content_types_provided, "
  123. "and the charsets_provided callback is not exported, the media "
  124. "type will match but the charset will be ignored like all other "
  125. "parameters."),
  126. ConnPid = gun_open(Config),
  127. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit_no_callback", [
  128. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  129. {<<"accept-encoding">>, <<"gzip">>}
  130. ]),
  131. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  132. %% The charset is ignored as if it was any other parameter.
  133. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  134. ok.
  135. charsets_provided_match_text(Config) ->
  136. doc("When the media type is text and the charsets_provided callback exists "
  137. "and the accept-charset header was sent, the selected charset is sent "
  138. "back in the content-type of the response."),
  139. ConnPid = gun_open(Config),
  140. Ref = gun:get(ConnPid, "/charsets_provided", [
  141. {<<"accept">>, <<"text/plain">>},
  142. {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
  143. {<<"accept-encoding">>, <<"gzip">>}
  144. ]),
  145. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  146. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  147. ok.
  148. charsets_provided_match_other(Config) ->
  149. doc("When the media type is not text and the charsets_provided callback exists "
  150. "and the accept-charset header was sent, the selected charset is not sent "
  151. "back in the content-type of the response."),
  152. ConnPid = gun_open(Config),
  153. Ref = gun:get(ConnPid, "/charsets_provided", [
  154. {<<"accept">>, <<"application/json">>},
  155. {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
  156. {<<"accept-encoding">>, <<"gzip">>}
  157. ]),
  158. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  159. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  160. ok.
  161. charsets_provided_wildcard_text(Config) ->
  162. doc("When the media type is text and the charsets_provided callback exists "
  163. "and a wildcard accept-charset header was sent, the selected charset is sent "
  164. "back in the content-type of the response."),
  165. ConnPid = gun_open(Config),
  166. Ref = gun:get(ConnPid, "/charsets_provided", [
  167. {<<"accept">>, <<"text/plain">>},
  168. {<<"accept-charset">>, <<"*">>},
  169. {<<"accept-encoding">>, <<"gzip">>}
  170. ]),
  171. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  172. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  173. ok.
  174. charsets_provided_wildcard_other(Config) ->
  175. doc("When the media type is not text and the charsets_provided callback exists "
  176. "and a wildcard accept-charset header was sent, the selected charset is not sent "
  177. "back in the content-type of the response."),
  178. ConnPid = gun_open(Config),
  179. Ref = gun:get(ConnPid, "/charsets_provided", [
  180. {<<"accept">>, <<"application/json">>},
  181. {<<"accept-charset">>, <<"*">>},
  182. {<<"accept-encoding">>, <<"gzip">>}
  183. ]),
  184. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  185. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  186. ok.
  187. charsets_provided_nomatch(Config) ->
  188. doc("Regardless of the media type negotiated, if no charset is found in the "
  189. "accept-charset header match a charset configured in charsets_provided, "
  190. "then a 406 not acceptable response is sent back."),
  191. ConnPid = gun_open(Config),
  192. Ref = gun:get(ConnPid, "/charsets_provided", [
  193. {<<"accept">>, <<"text/plain">>},
  194. {<<"accept-charset">>, <<"utf-8;q=0, iso-8859-1">>},
  195. {<<"accept-encoding">>, <<"gzip">>}
  196. ]),
  197. {response, _, 406, _} = gun:await(ConnPid, Ref),
  198. ok.
  199. charsets_provided_noheader_text(Config) ->
  200. doc("When the media type is text and the charsets_provided callback exists "
  201. "but the accept-charset header was not sent, the first charset in the "
  202. "list is selected and sent back in the content-type of the response."),
  203. ConnPid = gun_open(Config),
  204. Ref = gun:get(ConnPid, "/charsets_provided", [
  205. {<<"accept">>, <<"text/plain">>},
  206. {<<"accept-encoding">>, <<"gzip">>}
  207. ]),
  208. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  209. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  210. ok.
  211. charsets_provided_noheader_other(Config) ->
  212. doc("When the media type is not text and the charsets_provided callback exists "
  213. "but the accept-charset header was not sent, the first charset in the "
  214. "list is selected but is not sent back in the content-type of the response."),
  215. ConnPid = gun_open(Config),
  216. Ref = gun:get(ConnPid, "/charsets_provided", [
  217. {<<"accept">>, <<"application/json">>},
  218. {<<"accept-encoding">>, <<"gzip">>}
  219. ]),
  220. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  221. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  222. ok.
  223. charsets_provided_empty(Config) ->
  224. doc("Regardless of the media type negotiated, if the charsets_provided "
  225. "callback returns an empty list a 406 not acceptable response is sent back."),
  226. ConnPid = gun_open(Config),
  227. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  228. {<<"accept">>, <<"text/plain">>},
  229. {<<"accept-charset">>, <<"utf-8q=0.5, utf-16">>},
  230. {<<"accept-encoding">>, <<"gzip">>}
  231. ]),
  232. {response, _, 406, _} = gun:await(ConnPid, Ref),
  233. ok.
  234. charsets_provided_empty_wildcard(Config) ->
  235. doc("Regardless of the media type negotiated, if the charsets_provided "
  236. "callback returns an empty list a 406 not acceptable response is sent back."),
  237. ConnPid = gun_open(Config),
  238. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  239. {<<"accept">>, <<"text/plain">>},
  240. {<<"accept-charset">>, <<"*">>},
  241. {<<"accept-encoding">>, <<"gzip">>}
  242. ]),
  243. {response, _, 406, _} = gun:await(ConnPid, Ref),
  244. ok.
  245. charsets_provided_empty_noheader(Config) ->
  246. doc("Regardless of the media type negotiated, if the charsets_provided "
  247. "callback returns an empty list a 406 not acceptable response is sent back."),
  248. ConnPid = gun_open(Config),
  249. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  250. {<<"accept">>, <<"text/plain">>},
  251. {<<"accept-encoding">>, <<"gzip">>}
  252. ]),
  253. {response, _, 406, _} = gun:await(ConnPid, Ref),
  254. ok.
  255. provide_callback_missing(Config) ->
  256. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  257. ConnPid = gun_open(Config),
  258. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  259. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  260. ok.
  261. rate_limited(Config) ->
  262. doc("A 429 response must be sent when the rate_limited callback returns true. "
  263. "The retry-after header is specified as an integer."),
  264. ConnPid = gun_open(Config),
  265. Ref = gun:get(ConnPid, "/rate_limited?true", [{<<"accept-encoding">>, <<"gzip">>}]),
  266. {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
  267. {_, <<"3600">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
  268. ok.
  269. rate_limited_datetime(Config) ->
  270. doc("A 429 response must be sent when the rate_limited callback returns true. "
  271. "The retry-after header is specified as a date/time tuple."),
  272. ConnPid = gun_open(Config),
  273. Ref = gun:get(ConnPid, "/rate_limited?true-date", [{<<"accept-encoding">>, <<"gzip">>}]),
  274. {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
  275. {_, <<"Fri, 22 Feb 2222 11:11:11 GMT">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
  276. ok.
  277. rate_not_limited(Config) ->
  278. doc("A success response must be sent when the rate_limited callback returns false."),
  279. ConnPid = gun_open(Config),
  280. Ref = gun:get(ConnPid, "/rate_limited?false", [{<<"accept-encoding">>, <<"gzip">>}]),
  281. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  282. ok.
  283. stop_handler_allowed_methods(Config) ->
  284. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  285. stop_handler_allow_missing_post(Config) ->
  286. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  287. stop_handler_charsets_provided(Config) ->
  288. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  289. stop_handler_content_types_accepted(Config) ->
  290. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  291. stop_handler_content_types_provided(Config) ->
  292. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  293. stop_handler_delete_completed(Config) ->
  294. do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
  295. stop_handler_delete_resource(Config) ->
  296. do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
  297. stop_handler_forbidden(Config) ->
  298. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  299. stop_handler_is_authorized(Config) ->
  300. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  301. stop_handler_is_conflict(Config) ->
  302. do_req_body_stop_handler(Config, put, ?FUNCTION_NAME).
  303. stop_handler_known_methods(Config) ->
  304. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  305. stop_handler_languages_provided(Config) ->
  306. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  307. stop_handler_malformed_request(Config) ->
  308. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  309. stop_handler_moved_permanently(Config) ->
  310. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  311. stop_handler_moved_temporarily(Config) ->
  312. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  313. stop_handler_multiple_choices(Config) ->
  314. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  315. stop_handler_options(Config) ->
  316. do_no_body_stop_handler(Config, options, ?FUNCTION_NAME).
  317. stop_handler_previously_existed(Config) ->
  318. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  319. stop_handler_rate_limited(Config) ->
  320. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  321. stop_handler_resource_exists(Config) ->
  322. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  323. stop_handler_service_available(Config) ->
  324. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  325. stop_handler_uri_too_long(Config) ->
  326. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  327. stop_handler_valid_content_headers(Config) ->
  328. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  329. stop_handler_valid_entity_length(Config) ->
  330. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  331. stop_handler_accept(Config) ->
  332. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  333. stop_handler_provide(Config) ->
  334. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  335. do_no_body_stop_handler(Config, Method, StateName0) ->
  336. doc("Send a response manually and stop the REST handler."),
  337. ConnPid = gun_open(Config),
  338. "stop_handler_" ++ StateName = atom_to_list(StateName0),
  339. Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName,
  340. [{<<"accept-encoding">>, <<"gzip">>}]),
  341. {response, fin, 248, _} = gun:await(ConnPid, Ref),
  342. ok.
  343. do_req_body_stop_handler(Config, Method, StateName0) ->
  344. doc("Send a response manually and stop the REST handler."),
  345. ConnPid = gun_open(Config),
  346. "stop_handler_" ++ StateName = atom_to_list(StateName0),
  347. Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
  348. {<<"accept-encoding">>, <<"gzip">>},
  349. {<<"content-type">>, <<"text/plain">>}
  350. ], <<"Hocus PocuSwitch!">>),
  351. {response, fin, 248, _} = gun:await(ConnPid, Ref),
  352. ok.
  353. switch_handler_allowed_methods(Config) ->
  354. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  355. switch_handler_allow_missing_post(Config) ->
  356. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  357. switch_handler_charsets_provided(Config) ->
  358. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  359. switch_handler_content_types_accepted(Config) ->
  360. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  361. switch_handler_content_types_provided(Config) ->
  362. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  363. switch_handler_delete_completed(Config) ->
  364. do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
  365. switch_handler_delete_resource(Config) ->
  366. do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
  367. switch_handler_forbidden(Config) ->
  368. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  369. switch_handler_is_authorized(Config) ->
  370. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  371. switch_handler_is_conflict(Config) ->
  372. do_req_body_switch_handler(Config, put, ?FUNCTION_NAME).
  373. switch_handler_known_methods(Config) ->
  374. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  375. switch_handler_languages_provided(Config) ->
  376. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  377. switch_handler_malformed_request(Config) ->
  378. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  379. switch_handler_moved_permanently(Config) ->
  380. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  381. switch_handler_moved_temporarily(Config) ->
  382. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  383. switch_handler_multiple_choices(Config) ->
  384. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  385. switch_handler_options(Config) ->
  386. do_no_body_switch_handler(Config, options, ?FUNCTION_NAME).
  387. switch_handler_previously_existed(Config) ->
  388. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  389. switch_handler_rate_limited(Config) ->
  390. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  391. switch_handler_resource_exists(Config) ->
  392. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  393. switch_handler_service_available(Config) ->
  394. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  395. switch_handler_uri_too_long(Config) ->
  396. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  397. switch_handler_valid_content_headers(Config) ->
  398. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  399. switch_handler_valid_entity_length(Config) ->
  400. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  401. switch_handler_accept(Config) ->
  402. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  403. switch_handler_provide(Config) ->
  404. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  405. do_no_body_switch_handler(Config, Method, StateName0) ->
  406. doc("Switch REST to loop handler for streaming the response body, "
  407. "with and without options."),
  408. "switch_handler_" ++ StateName = atom_to_list(StateName0),
  409. do_no_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
  410. do_no_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
  411. do_no_body_switch_handler1(Config, Method, Path) ->
  412. ConnPid = gun_open(Config),
  413. Ref = gun:Method(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  414. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  415. {ok, Body} = gun:await_body(ConnPid, Ref),
  416. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  417. ok.
  418. do_req_body_switch_handler(Config, Method, StateName0) ->
  419. doc("Switch REST to loop handler for streaming the response body, "
  420. "with and without options."),
  421. "switch_handler_" ++ StateName = atom_to_list(StateName0),
  422. do_req_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
  423. do_req_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
  424. do_req_body_switch_handler1(Config, Method, Path) ->
  425. ConnPid = gun_open(Config),
  426. Ref = gun:Method(ConnPid, Path, [
  427. {<<"accept-encoding">>, <<"gzip">>},
  428. {<<"content-type">>, <<"text/plain">>}
  429. ], <<"Hocus PocuSwitch!">>),
  430. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  431. {ok, Body} = gun:await_body(ConnPid, Ref),
  432. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  433. ok.