rest_handler_SUITE.erl 50 KB

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