rest_handler_SUITE.erl 58 KB

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