rest_handler_SUITE.erl 56 KB

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