rest_handler_SUITE.erl 43 KB

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