rest_handler_SUITE.erl 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. {"/if_range", if_range_h, []},
  42. {"/provide_callback_missing", provide_callback_missing_h, []},
  43. {"/provide_range_callback", provide_range_callback_h, []},
  44. {"/range_satisfiable", range_satisfiable_h, []},
  45. {"/ranges_provided", ranges_provided_h, []},
  46. {"/ranges_provided_auto", ranges_provided_auto_h, []},
  47. {"/rate_limited", rate_limited_h, []},
  48. {"/stop_handler", stop_handler_h, []},
  49. {"/switch_handler", switch_handler_h, run},
  50. {"/switch_handler_opts", switch_handler_h, hibernate}
  51. ]}]).
  52. %% Internal.
  53. do_decode(Headers, Body) ->
  54. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  55. {_, <<"gzip">>} -> zlib:gunzip(Body);
  56. _ -> Body
  57. end.
  58. %% Tests.
  59. error_on_malformed_if_match(Config) ->
  60. doc("A malformed If-Match header must result in a 400 response."),
  61. ConnPid = gun_open(Config),
  62. Ref = gun:get(ConnPid, "/", [
  63. {<<"accept-encoding">>, <<"gzip">>},
  64. {<<"if-match">>, <<"bad">>}
  65. ]),
  66. {response, _, 400, _} = gun:await(ConnPid, Ref),
  67. ok.
  68. error_on_malformed_if_none_match(Config) ->
  69. doc("A malformed If-None-Match header must result in a 400 response."),
  70. ConnPid = gun_open(Config),
  71. Ref = gun:get(ConnPid, "/", [
  72. {<<"accept-encoding">>, <<"gzip">>},
  73. {<<"if-none-match">>, <<"bad">>}
  74. ]),
  75. {response, _, 400, _} = gun:await(ConnPid, Ref),
  76. ok.
  77. charset_in_content_types_provided(Config) ->
  78. doc("When a charset is matched explictly in content_types_provided, "
  79. "that charset is used and the charsets_provided callback is ignored."),
  80. ConnPid = gun_open(Config),
  81. Ref = gun:get(ConnPid, "/charset_in_content_types_provided", [
  82. {<<"accept">>, <<"text/plain;charset=utf-8">>},
  83. {<<"accept-charset">>, <<"utf-16, utf-8;q=0.5">>},
  84. {<<"accept-encoding">>, <<"gzip">>}
  85. ]),
  86. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  87. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  88. ok.
  89. charset_in_content_types_provided_implicit_match(Config) ->
  90. doc("When a charset is matched implicitly in content_types_provided, "
  91. "the charsets_provided callback is used to determine if the media "
  92. "type will match."),
  93. ConnPid = gun_open(Config),
  94. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  95. {<<"accept">>, <<"text/plain;charset=utf-16">>},
  96. {<<"accept-encoding">>, <<"gzip">>}
  97. ]),
  98. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  99. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  100. ok.
  101. charset_in_content_types_provided_implicit_nomatch(Config) ->
  102. doc("When a charset is matched implicitly in content_types_provided, "
  103. "the charsets_provided callback is used to determine if the media "
  104. "type will match. If it doesn't, try the next media type."),
  105. ConnPid = gun_open(Config),
  106. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  107. {<<"accept">>, <<"text/plain;charset=utf-32, text/plain">>},
  108. {<<"accept-encoding">>, <<"gzip">>}
  109. ]),
  110. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  111. %% We end up with the first charset listed in charsets_provided.
  112. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  113. ok.
  114. charset_in_content_types_provided_implicit_nomatch_error(Config) ->
  115. doc("When a charset is matched implicitly in content_types_provided, "
  116. "the charsets_provided callback is used to determine if the media "
  117. "type will match. If it doesn't, and there's no other media type, "
  118. "a 406 is returned."),
  119. ConnPid = gun_open(Config),
  120. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
  121. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  122. {<<"accept-encoding">>, <<"gzip">>}
  123. ]),
  124. {response, _, 406, _} = gun:await(ConnPid, Ref),
  125. ok.
  126. charset_in_content_types_provided_implicit_no_callback(Config) ->
  127. doc("When a charset is matched implicitly in content_types_provided, "
  128. "and the charsets_provided callback is not exported, the media "
  129. "type will match but the charset will be ignored like all other "
  130. "parameters."),
  131. ConnPid = gun_open(Config),
  132. Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit_no_callback", [
  133. {<<"accept">>, <<"text/plain;charset=utf-32">>},
  134. {<<"accept-encoding">>, <<"gzip">>}
  135. ]),
  136. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  137. %% The charset is ignored as if it was any other parameter.
  138. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  139. ok.
  140. charsets_provided_match_text(Config) ->
  141. doc("When the media type is text and the charsets_provided callback exists "
  142. "and the accept-charset header was sent, the selected charset is sent "
  143. "back in the content-type of the response."),
  144. ConnPid = gun_open(Config),
  145. Ref = gun:get(ConnPid, "/charsets_provided", [
  146. {<<"accept">>, <<"text/plain">>},
  147. {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
  148. {<<"accept-encoding">>, <<"gzip">>}
  149. ]),
  150. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  151. {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  152. ok.
  153. charsets_provided_match_other(Config) ->
  154. doc("When the media type is not text and the charsets_provided callback exists "
  155. "and the accept-charset header was sent, the selected charset is not sent "
  156. "back in the content-type of the response."),
  157. ConnPid = gun_open(Config),
  158. Ref = gun:get(ConnPid, "/charsets_provided", [
  159. {<<"accept">>, <<"application/json">>},
  160. {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
  161. {<<"accept-encoding">>, <<"gzip">>}
  162. ]),
  163. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  164. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  165. ok.
  166. charsets_provided_wildcard_text(Config) ->
  167. doc("When the media type is text and the charsets_provided callback exists "
  168. "and a wildcard accept-charset header was sent, the selected charset is sent "
  169. "back in the content-type of the response."),
  170. ConnPid = gun_open(Config),
  171. Ref = gun:get(ConnPid, "/charsets_provided", [
  172. {<<"accept">>, <<"text/plain">>},
  173. {<<"accept-charset">>, <<"*">>},
  174. {<<"accept-encoding">>, <<"gzip">>}
  175. ]),
  176. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  177. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  178. ok.
  179. charsets_provided_wildcard_other(Config) ->
  180. doc("When the media type is not text and the charsets_provided callback exists "
  181. "and a wildcard accept-charset header was sent, the selected charset is not sent "
  182. "back in the content-type of the response."),
  183. ConnPid = gun_open(Config),
  184. Ref = gun:get(ConnPid, "/charsets_provided", [
  185. {<<"accept">>, <<"application/json">>},
  186. {<<"accept-charset">>, <<"*">>},
  187. {<<"accept-encoding">>, <<"gzip">>}
  188. ]),
  189. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  190. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  191. ok.
  192. charsets_provided_nomatch(Config) ->
  193. doc("Regardless of the media type negotiated, if no charset is found in the "
  194. "accept-charset header match a charset configured in charsets_provided, "
  195. "then a 406 not acceptable response is sent back."),
  196. ConnPid = gun_open(Config),
  197. Ref = gun:get(ConnPid, "/charsets_provided", [
  198. {<<"accept">>, <<"text/plain">>},
  199. {<<"accept-charset">>, <<"utf-8;q=0, iso-8859-1">>},
  200. {<<"accept-encoding">>, <<"gzip">>}
  201. ]),
  202. {response, _, 406, _} = gun:await(ConnPid, Ref),
  203. ok.
  204. charsets_provided_noheader_text(Config) ->
  205. doc("When the media type is text and the charsets_provided callback exists "
  206. "but the accept-charset header was not sent, the first charset in the "
  207. "list is selected and sent back in the content-type of the response."),
  208. ConnPid = gun_open(Config),
  209. Ref = gun:get(ConnPid, "/charsets_provided", [
  210. {<<"accept">>, <<"text/plain">>},
  211. {<<"accept-encoding">>, <<"gzip">>}
  212. ]),
  213. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  214. {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  215. ok.
  216. charsets_provided_noheader_other(Config) ->
  217. doc("When the media type is not text and the charsets_provided callback exists "
  218. "but the accept-charset header was not sent, the first charset in the "
  219. "list is selected but is not sent back in the content-type of the response."),
  220. ConnPid = gun_open(Config),
  221. Ref = gun:get(ConnPid, "/charsets_provided", [
  222. {<<"accept">>, <<"application/json">>},
  223. {<<"accept-encoding">>, <<"gzip">>}
  224. ]),
  225. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  226. {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  227. ok.
  228. charsets_provided_empty(Config) ->
  229. doc("Regardless of the media type negotiated, if the charsets_provided "
  230. "callback returns an empty list a 406 not acceptable response is sent back."),
  231. ConnPid = gun_open(Config),
  232. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  233. {<<"accept">>, <<"text/plain">>},
  234. {<<"accept-charset">>, <<"utf-8q=0.5, utf-16">>},
  235. {<<"accept-encoding">>, <<"gzip">>}
  236. ]),
  237. {response, _, 406, _} = gun:await(ConnPid, Ref),
  238. ok.
  239. charsets_provided_empty_wildcard(Config) ->
  240. doc("Regardless of the media type negotiated, if the charsets_provided "
  241. "callback returns an empty list a 406 not acceptable response is sent back."),
  242. ConnPid = gun_open(Config),
  243. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  244. {<<"accept">>, <<"text/plain">>},
  245. {<<"accept-charset">>, <<"*">>},
  246. {<<"accept-encoding">>, <<"gzip">>}
  247. ]),
  248. {response, _, 406, _} = gun:await(ConnPid, Ref),
  249. ok.
  250. charsets_provided_empty_noheader(Config) ->
  251. doc("Regardless of the media type negotiated, if the charsets_provided "
  252. "callback returns an empty list a 406 not acceptable response is sent back."),
  253. ConnPid = gun_open(Config),
  254. Ref = gun:get(ConnPid, "/charsets_provided_empty", [
  255. {<<"accept">>, <<"text/plain">>},
  256. {<<"accept-encoding">>, <<"gzip">>}
  257. ]),
  258. {response, _, 406, _} = gun:await(ConnPid, Ref),
  259. ok.
  260. if_range_etag_equal(Config) ->
  261. doc("When the if-range header matches, a 206 partial content "
  262. "response is expected for an otherwise valid range request. (RFC7233 3.2)"),
  263. ConnPid = gun_open(Config),
  264. Ref = gun:get(ConnPid, "/if_range", [
  265. {<<"accept-encoding">>, <<"gzip">>},
  266. {<<"range">>, <<"bytes=0-">>},
  267. {<<"if-range">>, <<"\"strong-and-match\"">>}
  268. ]),
  269. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  270. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  271. {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  272. ok.
  273. if_range_etag_not_equal(Config) ->
  274. doc("When the if-range header does not match, the range header "
  275. "must be ignored and a 200 OK response is expected for "
  276. "an otherwise valid range request. (RFC7233 3.2)"),
  277. ConnPid = gun_open(Config),
  278. Ref = gun:get(ConnPid, "/if_range", [
  279. {<<"accept-encoding">>, <<"gzip">>},
  280. {<<"range">>, <<"bytes=0-">>},
  281. {<<"if-range">>, <<"\"strong-but-no-match\"">>}
  282. ]),
  283. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  284. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  285. false = lists:keyfind(<<"content-range">>, 1, Headers),
  286. ok.
  287. if_range_ignored_when_no_range_header(Config) ->
  288. doc("When there is no range header the if-range header is ignored "
  289. "and a 200 OK response is expected (RFC7233 3.2)"),
  290. ConnPid = gun_open(Config),
  291. Ref = gun:get(ConnPid, "/if_range", [
  292. {<<"accept-encoding">>, <<"gzip">>},
  293. {<<"if-range">>, <<"\"strong-and-match\"">>}
  294. ]),
  295. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  296. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  297. false = lists:keyfind(<<"content-range">>, 1, Headers),
  298. ok.
  299. if_range_ignored_when_ranges_provided_missing(Config) ->
  300. doc("When the resource does not support range requests "
  301. "the range and if-range headers must be ignored"
  302. "and a 200 OK response is expected (RFC7233 3.2)"),
  303. ConnPid = gun_open(Config),
  304. Ref = gun:get(ConnPid, "/if_range?missing-ranges_provided", [
  305. {<<"accept-encoding">>, <<"gzip">>},
  306. {<<"range">>, <<"bytes=0-">>},
  307. {<<"if-range">>, <<"\"strong-and-match\"">>}
  308. ]),
  309. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  310. false = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  311. false = lists:keyfind(<<"content-range">>, 1, Headers),
  312. ok.
  313. if_range_ignored_when_ranges_provided_empty(Config) ->
  314. doc("When the resource does not support range requests "
  315. "the range and if-range headers must be ignored"
  316. "and a 200 OK response is expected (RFC7233 3.2)"),
  317. ConnPid = gun_open(Config),
  318. Ref = gun:get(ConnPid, "/if_range?empty-ranges_provided", [
  319. {<<"accept-encoding">>, <<"gzip">>},
  320. {<<"range">>, <<"bytes=0-">>},
  321. {<<"if-range">>, <<"\"strong-and-match\"">>}
  322. ]),
  323. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  324. {_, <<"none">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  325. false = lists:keyfind(<<"content-range">>, 1, Headers),
  326. ok.
  327. if_range_weak_etag_not_equal(Config) ->
  328. doc("The if-range header must not match weak etags; the range header "
  329. "must be ignored and a 200 OK response is expected for "
  330. "an otherwise valid range request. (RFC7233 3.2)"),
  331. ConnPid = gun_open(Config),
  332. Ref = gun:get(ConnPid, "/if_range?weak-etag", [
  333. {<<"accept-encoding">>, <<"gzip">>},
  334. {<<"range">>, <<"bytes=0-">>},
  335. {<<"if-range">>, <<"W/\"weak-no-match\"">>}
  336. ]),
  337. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  338. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  339. false = lists:keyfind(<<"content-range">>, 1, Headers),
  340. ok.
  341. if_range_date_not_equal(Config) ->
  342. doc("The if-range header must not match weak dates. Cowboy "
  343. "currently has no way of knowing whether a resource was "
  344. "updated twice within the same second. The range header "
  345. "must be ignored and a 200 OK response is expected for "
  346. "an otherwise valid range request. (RFC7233 3.2)"),
  347. ConnPid = gun_open(Config),
  348. Ref = gun:get(ConnPid, "/if_range", [
  349. {<<"accept-encoding">>, <<"gzip">>},
  350. {<<"range">>, <<"bytes=0-">>},
  351. {<<"if-range">>, <<"Fri, 22 Feb 2222 11:11:11 GMT">>}
  352. ]),
  353. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  354. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  355. false = lists:keyfind(<<"content-range">>, 1, Headers),
  356. ok.
  357. provide_callback_missing(Config) ->
  358. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  359. ConnPid = gun_open(Config),
  360. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  361. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  362. ok.
  363. provide_range_callback(Config) ->
  364. doc("A successful request for a single range results in a "
  365. "206 partial content response with content-range set. (RFC7233 4.1, RFC7233 4.2)"),
  366. ConnPid = gun_open(Config),
  367. Ref = gun:get(ConnPid, "/provide_range_callback", [
  368. {<<"accept-encoding">>, <<"gzip">>},
  369. {<<"range">>, <<"bytes=0-">>}
  370. ]),
  371. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  372. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  373. {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  374. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  375. {ok, <<"This is ranged REST!">>} = gun:await_body(ConnPid, Ref),
  376. ok.
  377. provide_range_callback_multipart(Config) ->
  378. doc("A successful request for multiple ranges results in a "
  379. "206 partial content response using the multipart/byteranges "
  380. "content-type and the content-range not being set. The real "
  381. "content-type and content-range of the parts can be found in "
  382. "the multipart headers. (RFC7233 4.1, RFC7233 A)"),
  383. ConnPid = gun_open(Config),
  384. Ref = gun:get(ConnPid, "/provide_range_callback", [
  385. {<<"accept-encoding">>, <<"gzip">>},
  386. %% This range selects everything except the space characters.
  387. {<<"range">>, <<"bytes=0-3, 5-6, 8-13, 15-">>}
  388. ]),
  389. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  390. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  391. false = lists:keyfind(<<"content-range">>, 1, Headers),
  392. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  393. = lists:keyfind(<<"content-type">>, 1, Headers),
  394. {ok, Body0} = gun:await_body(ConnPid, Ref),
  395. Body = do_decode(Headers, Body0),
  396. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  397. [
  398. {bytes, 0, 3, 20},
  399. {bytes, 5, 6, 20},
  400. {bytes, 8, 13, 20},
  401. {bytes, 15, 19, 20}
  402. ] = ContentRanges,
  403. <<"ThisisrangedREST!">> = BodyAcc,
  404. ok.
  405. do_provide_range_callback_multipart_body(Rest, Boundary, ContentRangesAcc, BodyAcc) ->
  406. case cow_multipart:parse_headers(Rest, Boundary) of
  407. {ok, Headers, Rest1} ->
  408. {_, ContentRange0} = lists:keyfind(<<"content-range">>, 1, Headers),
  409. ContentRange = cow_http_hd:parse_content_range(ContentRange0),
  410. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  411. case cow_multipart:parse_body(Rest1, Boundary) of
  412. {done, Body} ->
  413. do_provide_range_callback_multipart_body(<<>>, Boundary,
  414. [ContentRange|ContentRangesAcc],
  415. <<BodyAcc/binary, Body/binary>>);
  416. {done, Body, Rest2} ->
  417. do_provide_range_callback_multipart_body(Rest2, Boundary,
  418. [ContentRange|ContentRangesAcc],
  419. <<BodyAcc/binary, Body/binary>>)
  420. end;
  421. {done, <<>>} ->
  422. {lists:reverse(ContentRangesAcc), BodyAcc}
  423. end.
  424. provide_range_callback_metadata(Config) ->
  425. doc("A successful request for a single range results in a "
  426. "206 partial content response with the same headers that "
  427. "a normal 200 OK response would, like vary or etag. (RFC7233 4.1)"),
  428. ConnPid = gun_open(Config),
  429. Ref = gun:get(ConnPid, "/provide_range_callback", [
  430. {<<"accept-encoding">>, <<"gzip">>},
  431. {<<"range">>, <<"bytes=0-">>}
  432. ]),
  433. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  434. {_, _} = lists:keyfind(<<"date">>, 1, Headers),
  435. {_, _} = lists:keyfind(<<"etag">>, 1, Headers),
  436. {_, _} = lists:keyfind(<<"expires">>, 1, Headers),
  437. {_, _} = lists:keyfind(<<"last-modified">>, 1, Headers),
  438. {_, _} = lists:keyfind(<<"vary">>, 1, Headers),
  439. %% Also cache-control and content-location but we don't send those.
  440. ok.
  441. provide_range_callback_missing(Config) ->
  442. doc("A 500 response must be sent when the ProvideRangeCallback can't be called."),
  443. ConnPid = gun_open(Config),
  444. Ref = gun:get(ConnPid, "/provide_range_callback?missing", [
  445. {<<"accept-encoding">>, <<"gzip">>},
  446. {<<"range">>, <<"bytes=0-">>}
  447. ]),
  448. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  449. ok.
  450. range_ignore_unknown_unit(Config) ->
  451. doc("The range header must be ignored when the range unit "
  452. "is not found in ranges_provided. (RFC7233 3.1)"),
  453. ConnPid = gun_open(Config),
  454. Ref = gun:get(ConnPid, "/if_range", [
  455. {<<"accept-encoding">>, <<"gzip">>},
  456. {<<"range">>, <<"chapters=1-">>}
  457. ]),
  458. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  459. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  460. false = lists:keyfind(<<"content-range">>, 1, Headers),
  461. ok.
  462. range_ignore_when_not_modified(Config) ->
  463. doc("The range header must be ignored when a conditional "
  464. "GET results in a 304 not modified response. (RFC7233 3.1)"),
  465. ConnPid = gun_open(Config),
  466. Ref = gun:get(ConnPid, "/if_range", [
  467. {<<"accept-encoding">>, <<"gzip">>},
  468. {<<"range">>, <<"bytes=0-">>},
  469. {<<"if-none-match">>, <<"\"strong-and-match\"">>}
  470. ]),
  471. {response, fin, 304, Headers} = gun:await(ConnPid, Ref),
  472. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  473. false = lists:keyfind(<<"content-range">>, 1, Headers),
  474. ok.
  475. range_satisfiable(Config) ->
  476. doc("When the range_satisfiable callback returns true "
  477. "a 206 partial content response is expected for "
  478. "an otherwise valid range request. (RFC7233 4.1)"),
  479. ConnPid = gun_open(Config),
  480. Ref = gun:get(ConnPid, "/range_satisfiable?true", [
  481. {<<"accept-encoding">>, <<"gzip">>},
  482. {<<"range">>, <<"bytes=0-">>}
  483. ]),
  484. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  485. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  486. {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  487. ok.
  488. range_not_satisfiable(Config) ->
  489. doc("When the range_satisfiable callback returns false "
  490. "a 416 range not satisfiable response is expected for "
  491. "an otherwise valid range request. (RFC7233 4.4)"),
  492. ConnPid = gun_open(Config),
  493. Ref = gun:get(ConnPid, "/range_satisfiable?false", [
  494. {<<"accept-encoding">>, <<"gzip">>},
  495. {<<"range">>, <<"bytes=0-">>}
  496. ]),
  497. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  498. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  499. false = lists:keyfind(<<"content-range">>, 1, Headers),
  500. ok.
  501. range_not_satisfiable_int(Config) ->
  502. doc("When the range_satisfiable callback returns false "
  503. "a 416 range not satisfiable response is expected for "
  504. "an otherwise valid range request. If an integer is "
  505. "provided it is used to construct the content-range "
  506. "header. (RFC7233 4.2, RFC7233 4.4)"),
  507. ConnPid = gun_open(Config),
  508. Ref = gun:get(ConnPid, "/range_satisfiable?false-int", [
  509. {<<"accept-encoding">>, <<"gzip">>},
  510. {<<"range">>, <<"bytes=0-">>}
  511. ]),
  512. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  513. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  514. {_, <<"bytes */123">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  515. ok.
  516. range_not_satisfiable_bin(Config) ->
  517. doc("When the range_satisfiable callback returns false "
  518. "a 416 range not satisfiable response is expected for "
  519. "an otherwise valid range request. If a binary is "
  520. "provided it is used to construct the content-range "
  521. "header. (RFC7233 4.2, RFC7233 4.4)"),
  522. ConnPid = gun_open(Config),
  523. Ref = gun:get(ConnPid, "/range_satisfiable?false-bin", [
  524. {<<"accept-encoding">>, <<"gzip">>},
  525. {<<"range">>, <<"bytes=0-">>}
  526. ]),
  527. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  528. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  529. {_, <<"bytes */456">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  530. ok.
  531. range_satisfiable_missing(Config) ->
  532. doc("When the range_satisfiable callback is missing "
  533. "a 206 partial content response is expected for "
  534. "an otherwise valid range request. (RFC7233 4.1)"),
  535. ConnPid = gun_open(Config),
  536. Ref = gun:get(ConnPid, "/range_satisfiable?missing", [
  537. {<<"accept-encoding">>, <<"gzip">>},
  538. {<<"range">>, <<"bytes=0-">>}
  539. ]),
  540. {response, _, 206, Headers} = gun:await(ConnPid, Ref),
  541. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  542. {_, <<"bytes ", _/bits>>} = lists:keyfind(<<"content-range">>, 1, Headers),
  543. ok.
  544. ranges_provided_accept_ranges(Config) ->
  545. doc("When the ranges_provided callback exists the accept-ranges header "
  546. "is sent in the response. (RFC7233 2.3)"),
  547. ConnPid = gun_open(Config),
  548. Ref = gun:get(ConnPid, "/ranges_provided?list", [{<<"accept-encoding">>, <<"gzip">>}]),
  549. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  550. {_, <<"bytes, pages, chapters">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  551. ok.
  552. %% @todo Probably should have options to do this automatically for auto at least.
  553. %%
  554. %% A server that supports range requests MAY ignore or reject a Range
  555. %% header field that consists of more than two overlapping ranges, or a
  556. %% set of many small ranges that are not listed in ascending order,
  557. %% since both are indications of either a broken client or a deliberate
  558. %% denial-of-service attack (Section 6.1).
  559. %% @todo Probably should have options for auto as well to join ranges that
  560. %% are very close from each other.
  561. ranges_provided_auto_data(Config) ->
  562. doc("When the unit range is bytes and the callback is 'auto' "
  563. "Cowboy will call the normal ProvideCallback and perform "
  564. "the range calculations automatically."),
  565. ConnPid = gun_open(Config),
  566. Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
  567. {<<"accept-encoding">>, <<"gzip">>},
  568. {<<"range">>, <<"bytes=8-">>}
  569. ]),
  570. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  571. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  572. {_, <<"bytes 8-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  573. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  574. {ok, <<"ranged REST!">>} = gun:await_body(ConnPid, Ref),
  575. ok.
  576. ranges_provided_auto_sendfile(Config) ->
  577. doc("When the unit range is bytes and the callback is 'auto' "
  578. "Cowboy will call the normal ProvideCallback and perform "
  579. "the range calculations automatically."),
  580. ConnPid = gun_open(Config),
  581. Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
  582. {<<"accept-encoding">>, <<"gzip">>},
  583. {<<"range">>, <<"bytes=8-">>}
  584. ]),
  585. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  586. Size = filelib:file_size(Path),
  587. {ok, <<_:8/binary, Body/bits>>} = file:read_file(Path),
  588. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  589. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  590. {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
  591. ContentRange = iolist_to_binary([
  592. <<"bytes 8-">>,
  593. integer_to_binary(Size - 1),
  594. <<"/">>,
  595. integer_to_binary(Size)
  596. ]),
  597. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  598. {ok, Body} = gun:await_body(ConnPid, Ref),
  599. ok.
  600. ranges_provided_auto_multipart_data(Config) ->
  601. doc("When the unit range is bytes and the callback is 'auto' "
  602. "Cowboy will call the normal ProvideCallback and perform "
  603. "the range calculations automatically."),
  604. ConnPid = gun_open(Config),
  605. Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
  606. {<<"accept-encoding">>, <<"gzip">>},
  607. %% This range selects everything except the space characters.
  608. {<<"range">>, <<"bytes=0-3, 5-6, 8-13, 15-">>}
  609. ]),
  610. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  611. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  612. false = lists:keyfind(<<"content-range">>, 1, Headers),
  613. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  614. = lists:keyfind(<<"content-type">>, 1, Headers),
  615. {ok, Body0} = gun:await_body(ConnPid, Ref),
  616. Body = do_decode(Headers, Body0),
  617. %% We will receive the ranges in the same order as requested.
  618. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  619. [
  620. {bytes, 0, 3, 20},
  621. {bytes, 5, 6, 20},
  622. {bytes, 8, 13, 20},
  623. {bytes, 15, 19, 20}
  624. ] = ContentRanges,
  625. <<"ThisisrangedREST!">> = BodyAcc,
  626. ok.
  627. ranges_provided_auto_multipart_sendfile(Config) ->
  628. doc("When the unit range is bytes and the callback is 'auto' "
  629. "Cowboy will call the normal ProvideCallback and perform "
  630. "the range calculations automatically."),
  631. ConnPid = gun_open(Config),
  632. Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
  633. {<<"accept-encoding">>, <<"gzip">>},
  634. %% This range selects a few random chunks of the file.
  635. {<<"range">>, <<"bytes=50-99, 150-199, 250-299, -99">>}
  636. ]),
  637. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  638. Size = filelib:file_size(Path),
  639. Skip = Size - 399,
  640. {ok, <<
  641. _:50/binary, Body1:50/binary,
  642. _:50/binary, Body2:50/binary,
  643. _:50/binary, Body3:50/binary,
  644. _:Skip/binary, Body4/bits>>} = file:read_file(Path),
  645. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  646. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  647. false = lists:keyfind(<<"content-range">>, 1, Headers),
  648. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  649. = lists:keyfind(<<"content-type">>, 1, Headers),
  650. {ok, Body0} = gun:await_body(ConnPid, Ref),
  651. Body = do_decode(Headers, Body0),
  652. %% We will receive the ranges in the same order as requested.
  653. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  654. LastFrom = 300 + Skip,
  655. LastTo = Size - 1,
  656. [
  657. {bytes, 50, 99, Size},
  658. {bytes, 150, 199, Size},
  659. {bytes, 250, 299, Size},
  660. {bytes, LastFrom, LastTo, Size}
  661. ] = ContentRanges,
  662. BodyAcc = <<Body1/binary, Body2/binary, Body3/binary, Body4/binary>>,
  663. ok.
  664. ranges_provided_auto_not_satisfiable_data(Config) ->
  665. doc("When the unit range is bytes and the callback is 'auto' "
  666. "Cowboy will call the normal ProvideCallback and perform "
  667. "the range calculations automatically. When the requested "
  668. "range is not satisfiable a 416 range not satisfiable response "
  669. "is expected. The content-range header will be set. (RFC7233 4.4)"),
  670. ConnPid = gun_open(Config),
  671. Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
  672. {<<"accept-encoding">>, <<"gzip">>},
  673. {<<"range">>, <<"bytes=1000-">>}
  674. ]),
  675. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  676. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  677. {_, <<"bytes */20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  678. ok.
  679. ranges_provided_auto_not_satisfiable_sendfile(Config) ->
  680. doc("When the unit range is bytes and the callback is 'auto' "
  681. "Cowboy will call the normal ProvideCallback and perform "
  682. "the range calculations automatically. When the requested "
  683. "range is not satisfiable a 416 range not satisfiable response "
  684. "is expected. The content-range header will be set. (RFC7233 4.4)"),
  685. ConnPid = gun_open(Config),
  686. Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
  687. {<<"accept-encoding">>, <<"gzip">>},
  688. {<<"range">>, <<"bytes=1000-">>}
  689. ]),
  690. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  691. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  692. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  693. Size = filelib:file_size(Path),
  694. ContentRange = iolist_to_binary([<<"bytes */">>, integer_to_binary(Size)]),
  695. {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
  696. ok.
  697. ranges_provided_empty_accept_ranges_none(Config) ->
  698. doc("When the ranges_provided callback exists but returns an empty list "
  699. "the accept-ranges header is sent in the response with the value none. (RFC7233 2.3)"),
  700. ConnPid = gun_open(Config),
  701. Ref = gun:get(ConnPid, "/ranges_provided?none", [{<<"accept-encoding">>, <<"gzip">>}]),
  702. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  703. {_, <<"none">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  704. ok.
  705. ranges_provided_missing_no_accept_ranges(Config) ->
  706. doc("When the ranges_provided callback does not exist "
  707. "the accept-ranges header is not sent in the response."),
  708. ConnPid = gun_open(Config),
  709. Ref = gun:get(ConnPid, "/ranges_provided?missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  710. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  711. false = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  712. ok.
  713. rate_limited(Config) ->
  714. doc("A 429 response must be sent when the rate_limited callback returns true. "
  715. "The retry-after header is specified as an integer. (RFC6585 4, RFC7231 7.1.3)"),
  716. ConnPid = gun_open(Config),
  717. Ref = gun:get(ConnPid, "/rate_limited?true", [{<<"accept-encoding">>, <<"gzip">>}]),
  718. {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
  719. {_, <<"3600">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
  720. ok.
  721. rate_limited_datetime(Config) ->
  722. doc("A 429 response must be sent when the rate_limited callback returns true. "
  723. "The retry-after header is specified as a date/time tuple. (RFC6585 4, RFC7231 7.1.3)"),
  724. ConnPid = gun_open(Config),
  725. Ref = gun:get(ConnPid, "/rate_limited?true-date", [{<<"accept-encoding">>, <<"gzip">>}]),
  726. {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
  727. {_, <<"Fri, 22 Feb 2222 11:11:11 GMT">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
  728. ok.
  729. rate_not_limited(Config) ->
  730. doc("A success response must be sent when the rate_limited callback returns false."),
  731. ConnPid = gun_open(Config),
  732. Ref = gun:get(ConnPid, "/rate_limited?false", [{<<"accept-encoding">>, <<"gzip">>}]),
  733. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  734. ok.
  735. stop_handler_allowed_methods(Config) ->
  736. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  737. stop_handler_allow_missing_post(Config) ->
  738. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  739. stop_handler_charsets_provided(Config) ->
  740. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  741. stop_handler_content_types_accepted(Config) ->
  742. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  743. stop_handler_content_types_provided(Config) ->
  744. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  745. stop_handler_delete_completed(Config) ->
  746. do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
  747. stop_handler_delete_resource(Config) ->
  748. do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
  749. stop_handler_forbidden(Config) ->
  750. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  751. stop_handler_is_authorized(Config) ->
  752. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  753. stop_handler_is_conflict(Config) ->
  754. do_req_body_stop_handler(Config, put, ?FUNCTION_NAME).
  755. stop_handler_known_methods(Config) ->
  756. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  757. stop_handler_languages_provided(Config) ->
  758. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  759. stop_handler_malformed_request(Config) ->
  760. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  761. stop_handler_moved_permanently(Config) ->
  762. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  763. stop_handler_moved_temporarily(Config) ->
  764. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  765. stop_handler_multiple_choices(Config) ->
  766. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  767. stop_handler_options(Config) ->
  768. do_no_body_stop_handler(Config, options, ?FUNCTION_NAME).
  769. stop_handler_previously_existed(Config) ->
  770. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  771. stop_handler_range_satisfiable(Config) ->
  772. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  773. stop_handler_ranges_provided(Config) ->
  774. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  775. stop_handler_rate_limited(Config) ->
  776. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  777. stop_handler_resource_exists(Config) ->
  778. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  779. stop_handler_service_available(Config) ->
  780. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  781. stop_handler_uri_too_long(Config) ->
  782. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  783. stop_handler_valid_content_headers(Config) ->
  784. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  785. stop_handler_valid_entity_length(Config) ->
  786. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  787. stop_handler_accept(Config) ->
  788. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  789. stop_handler_provide(Config) ->
  790. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  791. stop_handler_provide_range(Config) ->
  792. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  793. do_no_body_stop_handler(Config, Method, StateName0) ->
  794. doc("Send a response manually and stop the REST handler."),
  795. ConnPid = gun_open(Config),
  796. "stop_handler_" ++ StateName = atom_to_list(StateName0),
  797. Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
  798. {<<"accept-encoding">>, <<"gzip">>},
  799. {<<"range">>, <<"bytes=0-">>}
  800. ]),
  801. {response, fin, 248, _} = gun:await(ConnPid, Ref),
  802. ok.
  803. do_req_body_stop_handler(Config, Method, StateName0) ->
  804. doc("Send a response manually and stop the REST handler."),
  805. ConnPid = gun_open(Config),
  806. "stop_handler_" ++ StateName = atom_to_list(StateName0),
  807. Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
  808. {<<"accept-encoding">>, <<"gzip">>},
  809. {<<"content-type">>, <<"text/plain">>}
  810. ], <<"Hocus PocuSwitch!">>),
  811. {response, fin, 248, _} = gun:await(ConnPid, Ref),
  812. ok.
  813. switch_handler_allowed_methods(Config) ->
  814. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  815. switch_handler_allow_missing_post(Config) ->
  816. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  817. switch_handler_charsets_provided(Config) ->
  818. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  819. switch_handler_content_types_accepted(Config) ->
  820. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  821. switch_handler_content_types_provided(Config) ->
  822. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  823. switch_handler_delete_completed(Config) ->
  824. do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
  825. switch_handler_delete_resource(Config) ->
  826. do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
  827. switch_handler_forbidden(Config) ->
  828. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  829. switch_handler_is_authorized(Config) ->
  830. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  831. switch_handler_is_conflict(Config) ->
  832. do_req_body_switch_handler(Config, put, ?FUNCTION_NAME).
  833. switch_handler_known_methods(Config) ->
  834. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  835. switch_handler_languages_provided(Config) ->
  836. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  837. switch_handler_malformed_request(Config) ->
  838. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  839. switch_handler_moved_permanently(Config) ->
  840. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  841. switch_handler_moved_temporarily(Config) ->
  842. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  843. switch_handler_multiple_choices(Config) ->
  844. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  845. switch_handler_options(Config) ->
  846. do_no_body_switch_handler(Config, options, ?FUNCTION_NAME).
  847. switch_handler_previously_existed(Config) ->
  848. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  849. switch_handler_range_satisfiable(Config) ->
  850. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  851. switch_handler_ranges_provided(Config) ->
  852. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  853. switch_handler_rate_limited(Config) ->
  854. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  855. switch_handler_resource_exists(Config) ->
  856. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  857. switch_handler_service_available(Config) ->
  858. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  859. switch_handler_uri_too_long(Config) ->
  860. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  861. switch_handler_valid_content_headers(Config) ->
  862. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  863. switch_handler_valid_entity_length(Config) ->
  864. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  865. switch_handler_accept(Config) ->
  866. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  867. switch_handler_provide(Config) ->
  868. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  869. switch_handler_provide_range(Config) ->
  870. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  871. do_no_body_switch_handler(Config, Method, StateName0) ->
  872. doc("Switch REST to loop handler for streaming the response body, "
  873. "with and without options."),
  874. "switch_handler_" ++ StateName = atom_to_list(StateName0),
  875. do_no_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
  876. do_no_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
  877. do_no_body_switch_handler1(Config, Method, Path) ->
  878. ConnPid = gun_open(Config),
  879. Ref = gun:Method(ConnPid, Path, [
  880. {<<"accept-encoding">>, <<"gzip">>},
  881. {<<"range">>, <<"bytes=0-">>}
  882. ]),
  883. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  884. {ok, Body} = gun:await_body(ConnPid, Ref),
  885. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  886. ok.
  887. do_req_body_switch_handler(Config, Method, StateName0) ->
  888. doc("Switch REST to loop handler for streaming the response body, "
  889. "with and without options."),
  890. "switch_handler_" ++ StateName = atom_to_list(StateName0),
  891. do_req_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
  892. do_req_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
  893. do_req_body_switch_handler1(Config, Method, Path) ->
  894. ConnPid = gun_open(Config),
  895. Ref = gun:Method(ConnPid, Path, [
  896. {<<"accept-encoding">>, <<"gzip">>},
  897. {<<"content-type">>, <<"text/plain">>}
  898. ], <<"Hocus PocuSwitch!">>),
  899. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  900. {ok, Body} = gun:await_body(ConnPid, Ref),
  901. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  902. ok.