rest_handler_SUITE.erl 34 KB

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