rest_handler_SUITE.erl 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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(Config) ->
  323. doc("When a wildcard is returned from the content_types_accepted "
  324. "callback, any content-type must be accepted."),
  325. ConnPid = gun_open(Config),
  326. Ref1 = gun:put(ConnPid, "/content_types_accepted?wildcard", [
  327. {<<"accept-encoding">>, <<"gzip">>},
  328. {<<"content-type">>, <<"text/plain">>}
  329. ]),
  330. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  331. {response, fin, 204, _} = gun:await(ConnPid, Ref1),
  332. Ref2 = gun:put(ConnPid, "/content_types_accepted?wildcard", [
  333. {<<"accept-encoding">>, <<"gzip">>},
  334. {<<"content-type">>, <<"application/vnd.plain;charset=UTF-8">>}
  335. ]),
  336. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  337. {response, fin, 204, _} = gun:await(ConnPid, Ref2),
  338. ok.
  339. content_types_accepted_wildcard_param_no_content_type_param(Config) ->
  340. doc("When a wildcard is returned for parameters from the "
  341. "content_types_accepted callback, a content-type header "
  342. "with no parameters must be accepted."),
  343. ConnPid = gun_open(Config),
  344. Ref = gun:put(ConnPid, "/content_types_accepted?wildcard-param", [
  345. {<<"accept-encoding">>, <<"gzip">>},
  346. {<<"content-type">>, <<"text/plain">>}
  347. ]),
  348. gun:data(ConnPid, Ref, fin, "Hello world!"),
  349. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  350. ok.
  351. content_types_accepted_wildcard_param_content_type_with_param(Config) ->
  352. doc("When a wildcard is returned for parameters from the "
  353. "content_types_accepted callback, a content-type header "
  354. "with a parameter must be accepted."),
  355. ConnPid = gun_open(Config),
  356. Ref = gun:put(ConnPid, "/content_types_accepted?wildcard-param", [
  357. {<<"accept-encoding">>, <<"gzip">>},
  358. {<<"content-type">>, <<"text/plain; charset=utf-8">>}
  359. ]),
  360. gun:data(ConnPid, Ref, fin, "Hello world!"),
  361. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  362. ok.
  363. content_types_provided_wildcard_param_no_accept_param(Config) ->
  364. doc("When a wildcard is returned for parameters from the "
  365. "content_types_provided callback, an accept header "
  366. "with no parameters must be accepted."),
  367. ConnPid = gun_open(Config),
  368. Ref = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
  369. {<<"accept">>, <<"text/plain">>},
  370. {<<"accept-encoding">>, <<"gzip">>}
  371. ]),
  372. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  373. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref),
  374. ok.
  375. content_types_provided_wildcard_param_accept_with_param(Config) ->
  376. doc("When a wildcard is returned for parameters from the "
  377. "content_types_provided callback, an accept header "
  378. "with a parameter must be accepted."),
  379. ConnPid = gun_open(Config),
  380. Ref = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
  381. {<<"accept">>, <<"text/plain;level=1">>},
  382. {<<"accept-encoding">>, <<"gzip">>}
  383. ]),
  384. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  385. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref),
  386. ok.
  387. content_types_provided_wildcard_param_accept_with_param_and_qvalue(Config) ->
  388. doc("When a wildcard is returned for parameters from the "
  389. "content_types_provided callback, an accept header "
  390. "with two media types containing parameters including a "
  391. "q-value must be accepted. The q-value determines which."),
  392. ConnPid = gun_open(Config),
  393. Ref1 = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
  394. {<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>},
  395. {<<"accept-encoding">>, <<"gzip">>}
  396. ]),
  397. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  398. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref1),
  399. Ref2 = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
  400. {<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>},
  401. {<<"accept-encoding">>, <<"gzip">>}
  402. ]),
  403. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  404. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref2),
  405. ok.
  406. content_types_provided_wildcard_param_no_accept_header(Config) ->
  407. doc("When a wildcard is returned for parameters from the "
  408. "content_types_provided callback, the lack of accept header "
  409. "results in the first media type returned being accepted. "
  410. "The wildcard must however not be present in the media_type "
  411. "value added to the Req object."),
  412. ConnPid = gun_open(Config),
  413. Ref = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
  414. {<<"accept-encoding">>, <<"gzip">>}
  415. ]),
  416. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  417. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref),
  418. ok.
  419. delete_resource_missing(Config) ->
  420. doc("When a resource accepts the DELETE method and the "
  421. "delete_resource callback is not exported, the "
  422. "resource is incorrect and a 500 response is expected."),
  423. ConnPid = gun_open(Config),
  424. Ref = gun:delete(ConnPid, "/delete_resource?missing", [
  425. {<<"accept-encoding">>, <<"gzip">>}
  426. ]),
  427. {response, _, 500, _} = gun:await(ConnPid, Ref),
  428. ok.
  429. error_on_malformed_accept(Config) ->
  430. doc("A malformed Accept header must result in a 400 response."),
  431. do_error_on_malformed_header(Config, <<"accept">>).
  432. error_on_malformed_if_match(Config) ->
  433. doc("A malformed If-Match header must result in a 400 response."),
  434. do_error_on_malformed_header(Config, <<"if-match">>).
  435. error_on_malformed_if_none_match(Config) ->
  436. doc("A malformed If-None-Match header must result in a 400 response."),
  437. do_error_on_malformed_header(Config, <<"if-none-match">>).
  438. do_error_on_malformed_header(Config, Name) ->
  439. ConnPid = gun_open(Config),
  440. Ref = gun:get(ConnPid, "/", [
  441. {<<"accept-encoding">>, <<"gzip">>},
  442. {Name, <<"bad">>}
  443. ]),
  444. {response, _, 400, _} = gun:await(ConnPid, Ref),
  445. ok.
  446. expires_binary(Config) ->
  447. doc("The expires header can also be given as a binary "
  448. "to indicate a date in the past. (RFC7234 5.3)"),
  449. ConnPid = gun_open(Config),
  450. Ref = gun:get(ConnPid, "/expires?binary", [
  451. {<<"accept-encoding">>, <<"gzip">>}
  452. ]),
  453. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  454. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  455. ok.
  456. expires_missing(Config) ->
  457. doc("The expires header must not be sent when the callback is not exported."),
  458. ConnPid = gun_open(Config),
  459. Ref = gun:get(ConnPid, "/expires?missing", [
  460. {<<"accept-encoding">>, <<"gzip">>}
  461. ]),
  462. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  463. false = lists:keyfind(<<"expires">>, 1, Headers),
  464. ok.
  465. expires_tuple(Config) ->
  466. doc("The expires header can be given as a date tuple. (RFC7234 5.3)"),
  467. ConnPid = gun_open(Config),
  468. Ref = gun:get(ConnPid, "/expires?tuple", [
  469. {<<"accept-encoding">>, <<"gzip">>}
  470. ]),
  471. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  472. {_, <<"Fri, 21 Sep 2012 22:36:14 GMT">>} = lists:keyfind(<<"expires">>, 1, Headers),
  473. ok.
  474. expires_undefined(Config) ->
  475. doc("The expires header must not be sent when undefined is returned."),
  476. ConnPid = gun_open(Config),
  477. Ref = gun:get(ConnPid, "/expires?undefined", [
  478. {<<"accept-encoding">>, <<"gzip">>}
  479. ]),
  480. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  481. false = lists:keyfind(<<"expires">>, 1, Headers),
  482. ok.
  483. generate_etag_missing(Config) ->
  484. doc("The etag header must not be sent when "
  485. "the generate_etag callback is not exported."),
  486. ConnPid = gun_open(Config),
  487. Ref = gun:get(ConnPid, "/generate_etag?missing", [
  488. {<<"accept-encoding">>, <<"gzip">>}
  489. ]),
  490. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  491. false = lists:keyfind(<<"etag">>, 1, Headers),
  492. ok.
  493. generate_etag_binary_strong(Config) ->
  494. doc("The etag header must be sent when the generate_etag "
  495. "callback returns a strong binary. (RFC7232 2.3)"),
  496. do_generate_etag(Config, "binary-strong-quoted",
  497. [], 200, {<<"etag">>, <<"\"etag-header-value\"">>}).
  498. generate_etag_binary_weak(Config) ->
  499. doc("The etag header must be sent when the generate_etag "
  500. "callback returns a weak binary. (RFC7232 2.3)"),
  501. do_generate_etag(Config, "binary-weak-quoted",
  502. [], 200, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
  503. generate_etag_invalid_binary_strong_unquoted(Config) ->
  504. doc("When Cowboy cannot parse the generate_etag callback's "
  505. "return value, a 500 response is returned without the etag header."),
  506. do_generate_etag(Config, "binary-strong-unquoted", [], 500, false).
  507. generate_etag_invalid_binary_weak_unquoted(Config) ->
  508. doc("When Cowboy cannot parse the generate_etag callback's "
  509. "return value, a 500 response is returned without the etag header."),
  510. do_generate_etag(Config, "binary-weak-unquoted", [], 500, false).
  511. generate_etag_tuple_strong(Config) ->
  512. doc("The etag header must be sent when the generate_etag "
  513. "callback returns a strong tuple. (RFC7232 2.3)"),
  514. do_generate_etag(Config, "tuple-strong",
  515. [], 200, {<<"etag">>, <<"\"etag-header-value\"">>}).
  516. generate_etag_tuple_weak(Config) ->
  517. doc("The etag header must be sent when the generate_etag "
  518. "callback returns a weak tuple. (RFC7232 2.3)"),
  519. do_generate_etag(Config, "tuple-weak",
  520. [], 200, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
  521. if_none_match_binary_strong(Config) ->
  522. doc("When the if-none-match header matches a strong etag, "
  523. "a 304 not modified response is returned. (RFC7232 3.2)"),
  524. do_generate_etag(Config, "binary-strong-quoted",
  525. [{<<"if-none-match">>, <<"\"etag-header-value\"">>}],
  526. 304, {<<"etag">>, <<"\"etag-header-value\"">>}).
  527. if_none_match_binary_weak(Config) ->
  528. doc("When the if-none-match header matches a weak etag, "
  529. "a 304 not modified response is returned. (RFC7232 3.2)"),
  530. do_generate_etag(Config, "binary-weak-quoted",
  531. [{<<"if-none-match">>, <<"W/\"etag-header-value\"">>}],
  532. 304, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
  533. if_none_match_tuple_strong(Config) ->
  534. doc("When the if-none-match header matches a strong etag, "
  535. "a 304 not modified response is returned. (RFC7232 3.2)"),
  536. do_generate_etag(Config, "tuple-strong",
  537. [{<<"if-none-match">>, <<"\"etag-header-value\"">>}],
  538. 304, {<<"etag">>, <<"\"etag-header-value\"">>}).
  539. if_none_match_tuple_weak(Config) ->
  540. doc("When the if-none-match header matches a weak etag, "
  541. "a 304 not modified response is returned. (RFC7232 3.2)"),
  542. do_generate_etag(Config, "tuple-weak",
  543. [{<<"if-none-match">>, <<"W/\"etag-header-value\"">>}],
  544. 304, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
  545. do_generate_etag(Config, Qs, ReqHeaders, Status, Etag) ->
  546. ConnPid = gun_open(Config),
  547. Ref = gun:get(ConnPid, "/generate_etag?" ++ Qs, [
  548. {<<"accept-encoding">>, <<"gzip">>}
  549. |ReqHeaders
  550. ]),
  551. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  552. Etag = lists:keyfind(<<"etag">>, 1, RespHeaders),
  553. ok.
  554. if_range_etag_equal(Config) ->
  555. doc("When the if-range header matches, a 206 partial content "
  556. "response is expected for an otherwise valid range request. (RFC7233 3.2)"),
  557. ConnPid = gun_open(Config),
  558. Ref = gun:get(ConnPid, "/if_range", [
  559. {<<"accept-encoding">>, <<"gzip">>},
  560. {<<"range">>, <<"bytes=0-">>},
  561. {<<"if-range">>, <<"\"strong-and-match\"">>}
  562. ]),
  563. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  564. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  565. {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  566. ok.
  567. if_range_etag_not_equal(Config) ->
  568. doc("When the if-range header does not match, the range header "
  569. "must be ignored and a 200 OK response is expected for "
  570. "an otherwise valid range request. (RFC7233 3.2)"),
  571. ConnPid = gun_open(Config),
  572. Ref = gun:get(ConnPid, "/if_range", [
  573. {<<"accept-encoding">>, <<"gzip">>},
  574. {<<"range">>, <<"bytes=0-">>},
  575. {<<"if-range">>, <<"\"strong-but-no-match\"">>}
  576. ]),
  577. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  578. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  579. false = lists:keyfind(<<"content-range">>, 1, Headers),
  580. ok.
  581. if_range_ignored_when_no_range_header(Config) ->
  582. doc("When there is no range header the if-range header is ignored "
  583. "and a 200 OK response is expected (RFC7233 3.2)"),
  584. ConnPid = gun_open(Config),
  585. Ref = gun:get(ConnPid, "/if_range", [
  586. {<<"accept-encoding">>, <<"gzip">>},
  587. {<<"if-range">>, <<"\"strong-and-match\"">>}
  588. ]),
  589. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  590. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  591. false = lists:keyfind(<<"content-range">>, 1, Headers),
  592. ok.
  593. if_range_ignored_when_ranges_provided_missing(Config) ->
  594. doc("When the resource does not support range requests "
  595. "the range and if-range headers must be ignored"
  596. "and a 200 OK response is expected (RFC7233 3.2)"),
  597. ConnPid = gun_open(Config),
  598. Ref = gun:get(ConnPid, "/if_range?missing-ranges_provided", [
  599. {<<"accept-encoding">>, <<"gzip">>},
  600. {<<"range">>, <<"bytes=0-">>},
  601. {<<"if-range">>, <<"\"strong-and-match\"">>}
  602. ]),
  603. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  604. false = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  605. false = lists:keyfind(<<"content-range">>, 1, Headers),
  606. ok.
  607. if_range_ignored_when_ranges_provided_empty(Config) ->
  608. doc("When the resource does not support range requests "
  609. "the range and if-range headers must be ignored"
  610. "and a 200 OK response is expected (RFC7233 3.2)"),
  611. ConnPid = gun_open(Config),
  612. Ref = gun:get(ConnPid, "/if_range?empty-ranges_provided", [
  613. {<<"accept-encoding">>, <<"gzip">>},
  614. {<<"range">>, <<"bytes=0-">>},
  615. {<<"if-range">>, <<"\"strong-and-match\"">>}
  616. ]),
  617. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  618. {_, <<"none">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  619. false = lists:keyfind(<<"content-range">>, 1, Headers),
  620. ok.
  621. if_range_weak_etag_not_equal(Config) ->
  622. doc("The if-range header must not match weak etags; the range header "
  623. "must be ignored and a 200 OK response is expected for "
  624. "an otherwise valid range request. (RFC7233 3.2)"),
  625. ConnPid = gun_open(Config),
  626. Ref = gun:get(ConnPid, "/if_range?weak-etag", [
  627. {<<"accept-encoding">>, <<"gzip">>},
  628. {<<"range">>, <<"bytes=0-">>},
  629. {<<"if-range">>, <<"W/\"weak-no-match\"">>}
  630. ]),
  631. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  632. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  633. false = lists:keyfind(<<"content-range">>, 1, Headers),
  634. ok.
  635. if_range_date_not_equal(Config) ->
  636. doc("The if-range header must not match weak dates. Cowboy "
  637. "currently has no way of knowing whether a resource was "
  638. "updated twice within the same second. The range header "
  639. "must be ignored and a 200 OK response is expected for "
  640. "an otherwise valid range request. (RFC7233 3.2)"),
  641. ConnPid = gun_open(Config),
  642. Ref = gun:get(ConnPid, "/if_range", [
  643. {<<"accept-encoding">>, <<"gzip">>},
  644. {<<"range">>, <<"bytes=0-">>},
  645. {<<"if-range">>, <<"Fri, 22 Feb 2222 11:11:11 GMT">>}
  646. ]),
  647. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  648. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  649. false = lists:keyfind(<<"content-range">>, 1, Headers),
  650. ok.
  651. last_modified(Config) ->
  652. doc("The last-modified header can be given as a date tuple. (RFC7232 2.2)"),
  653. ConnPid = gun_open(Config),
  654. Ref = gun:get(ConnPid, "/last_modified?tuple", [
  655. {<<"accept-encoding">>, <<"gzip">>}
  656. ]),
  657. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  658. {_, <<"Fri, 21 Sep 2012 22:36:14 GMT">>} = lists:keyfind(<<"last-modified">>, 1, Headers),
  659. ok.
  660. last_modified_missing(Config) ->
  661. doc("The last-modified header must not be sent when the callback is not exported."),
  662. ConnPid = gun_open(Config),
  663. Ref = gun:get(ConnPid, "/last_modified?missing", [
  664. {<<"accept-encoding">>, <<"gzip">>}
  665. ]),
  666. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  667. false = lists:keyfind(<<"last-modified">>, 1, Headers),
  668. ok.
  669. options_missing(Config) ->
  670. doc("A successful OPTIONS request to a simple handler results in "
  671. "a 200 OK response with the allow header set. (RFC7231 4.3.7)"),
  672. ConnPid = gun_open(Config),
  673. Ref = gun:options(ConnPid, "/", [
  674. {<<"accept-encoding">>, <<"gzip">>}
  675. ]),
  676. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  677. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  678. ok.
  679. provide_callback(Config) ->
  680. doc("A successful GET request to a simple handler results in "
  681. "a 200 OK response with the content-type set. (RFC7231 4.3.1, RFC7231 6.3.1)"),
  682. ConnPid = gun_open(Config),
  683. Ref = gun:get(ConnPid, "/", [
  684. {<<"accept">>, <<"*/*">>},
  685. {<<"accept-encoding">>, <<"gzip">>}
  686. ]),
  687. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  688. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  689. {ok, <<"This is REST!">>} = gun:await_body(ConnPid, Ref),
  690. ok.
  691. provide_callback_missing(Config) ->
  692. doc("A 500 response must be sent when the ProvideCallback can't be called."),
  693. ConnPid = gun_open(Config),
  694. Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  695. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  696. ok.
  697. provide_range_callback(Config) ->
  698. doc("A successful request for a single range results in a "
  699. "206 partial content response with content-range set. (RFC7233 4.1, RFC7233 4.2)"),
  700. ConnPid = gun_open(Config),
  701. Ref = gun:get(ConnPid, "/provide_range_callback", [
  702. {<<"accept-encoding">>, <<"gzip">>},
  703. {<<"range">>, <<"bytes=0-">>}
  704. ]),
  705. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  706. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  707. {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  708. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  709. {ok, <<"This is ranged REST!">>} = gun:await_body(ConnPid, Ref),
  710. ok.
  711. provide_range_callback_sendfile(Config) ->
  712. doc("A successful request for a single range results in a "
  713. "206 partial content response with content-range set. (RFC7233 4.1, RFC7233 4.2)"),
  714. ConnPid = gun_open(Config),
  715. Ref = gun:get(ConnPid, "/provide_range_callback?sendfile", [
  716. {<<"accept-encoding">>, <<"gzip">>},
  717. {<<"range">>, <<"bytes=0-">>}
  718. ]),
  719. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  720. Size = filelib:file_size(Path),
  721. {ok, Body} = file:read_file(Path),
  722. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  723. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  724. {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
  725. ContentRange = iolist_to_binary([
  726. <<"bytes 0-">>,
  727. integer_to_binary(Size - 1),
  728. <<"/">>,
  729. integer_to_binary(Size)
  730. ]),
  731. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  732. {ok, Body} = gun:await_body(ConnPid, Ref),
  733. ok.
  734. provide_range_callback_multipart(Config) ->
  735. doc("A successful request for multiple ranges results in a "
  736. "206 partial content response using the multipart/byteranges "
  737. "content-type and the content-range not being set. The real "
  738. "content-type and content-range of the parts can be found in "
  739. "the multipart headers. (RFC7233 4.1, RFC7233 A)"),
  740. ConnPid = gun_open(Config),
  741. Ref = gun:get(ConnPid, "/provide_range_callback", [
  742. {<<"accept-encoding">>, <<"gzip">>},
  743. %% This range selects everything except the space characters.
  744. {<<"range">>, <<"bytes=0-3, 5-6, 8-13, 15-">>}
  745. ]),
  746. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  747. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  748. false = lists:keyfind(<<"content-range">>, 1, Headers),
  749. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  750. = lists:keyfind(<<"content-type">>, 1, Headers),
  751. {ok, Body0} = gun:await_body(ConnPid, Ref),
  752. Body = do_decode(Headers, Body0),
  753. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  754. [
  755. {bytes, 0, 3, 20},
  756. {bytes, 5, 6, 20},
  757. {bytes, 8, 13, 20},
  758. {bytes, 15, 19, 20}
  759. ] = ContentRanges,
  760. <<"ThisisrangedREST!">> = BodyAcc,
  761. ok.
  762. provide_range_callback_multipart_sendfile(Config) ->
  763. doc("A successful request for multiple ranges results in a "
  764. "206 partial content response using the multipart/byteranges "
  765. "content-type and the content-range not being set. The real "
  766. "content-type and content-range of the parts can be found in "
  767. "the multipart headers. (RFC7233 4.1, RFC7233 A)"),
  768. ConnPid = gun_open(Config),
  769. Ref = gun:get(ConnPid, "/provide_range_callback?sendfile", [
  770. {<<"accept-encoding">>, <<"gzip">>},
  771. %% This range selects a few random chunks of the file.
  772. {<<"range">>, <<"bytes=50-99, 150-199, 250-299, -99">>}
  773. ]),
  774. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  775. Size = filelib:file_size(Path),
  776. Skip = Size - 399,
  777. {ok, <<
  778. _:50/binary, Body1:50/binary,
  779. _:50/binary, Body2:50/binary,
  780. _:50/binary, Body3:50/binary,
  781. _:Skip/binary, Body4/bits>>} = file:read_file(Path),
  782. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  783. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  784. false = lists:keyfind(<<"content-range">>, 1, Headers),
  785. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  786. = lists:keyfind(<<"content-type">>, 1, Headers),
  787. {ok, Body0} = gun:await_body(ConnPid, Ref),
  788. Body = do_decode(Headers, Body0),
  789. %% We will receive the ranges in the same order as requested.
  790. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  791. LastFrom = 300 + Skip,
  792. LastTo = Size - 1,
  793. [
  794. {bytes, 50, 99, Size},
  795. {bytes, 150, 199, Size},
  796. {bytes, 250, 299, Size},
  797. {bytes, LastFrom, LastTo, Size}
  798. ] = ContentRanges,
  799. BodyAcc = <<Body1/binary, Body2/binary, Body3/binary, Body4/binary>>,
  800. ok.
  801. do_provide_range_callback_multipart_body(Rest, Boundary, ContentRangesAcc, BodyAcc) ->
  802. case cow_multipart:parse_headers(Rest, Boundary) of
  803. {ok, Headers, Rest1} ->
  804. {_, ContentRange0} = lists:keyfind(<<"content-range">>, 1, Headers),
  805. ContentRange = cow_http_hd:parse_content_range(ContentRange0),
  806. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  807. case cow_multipart:parse_body(Rest1, Boundary) of
  808. {done, Body} ->
  809. do_provide_range_callback_multipart_body(<<>>, Boundary,
  810. [ContentRange|ContentRangesAcc],
  811. <<BodyAcc/binary, Body/binary>>);
  812. {done, Body, Rest2} ->
  813. do_provide_range_callback_multipart_body(Rest2, Boundary,
  814. [ContentRange|ContentRangesAcc],
  815. <<BodyAcc/binary, Body/binary>>)
  816. end;
  817. {done, <<>>} ->
  818. {lists:reverse(ContentRangesAcc), BodyAcc}
  819. end.
  820. provide_range_callback_metadata(Config) ->
  821. doc("A successful request for a single range results in a "
  822. "206 partial content response with the same headers that "
  823. "a normal 200 OK response would, like vary or etag. (RFC7233 4.1)"),
  824. ConnPid = gun_open(Config),
  825. Ref = gun:get(ConnPid, "/provide_range_callback", [
  826. {<<"accept-encoding">>, <<"gzip">>},
  827. {<<"range">>, <<"bytes=0-">>}
  828. ]),
  829. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  830. {_, _} = lists:keyfind(<<"date">>, 1, Headers),
  831. {_, _} = lists:keyfind(<<"etag">>, 1, Headers),
  832. {_, _} = lists:keyfind(<<"expires">>, 1, Headers),
  833. {_, _} = lists:keyfind(<<"last-modified">>, 1, Headers),
  834. {_, _} = lists:keyfind(<<"vary">>, 1, Headers),
  835. %% Also cache-control and content-location but we don't send those.
  836. ok.
  837. provide_range_callback_missing(Config) ->
  838. doc("A 500 response must be sent when the ProvideRangeCallback can't be called."),
  839. ConnPid = gun_open(Config),
  840. Ref = gun:get(ConnPid, "/provide_range_callback?missing", [
  841. {<<"accept-encoding">>, <<"gzip">>},
  842. {<<"range">>, <<"bytes=0-">>}
  843. ]),
  844. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  845. ok.
  846. range_ignore_unknown_unit(Config) ->
  847. doc("The range header must be ignored when the range unit "
  848. "is not found in ranges_provided. (RFC7233 3.1)"),
  849. ConnPid = gun_open(Config),
  850. Ref = gun:get(ConnPid, "/if_range", [
  851. {<<"accept-encoding">>, <<"gzip">>},
  852. {<<"range">>, <<"chapters=1-">>}
  853. ]),
  854. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  855. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  856. false = lists:keyfind(<<"content-range">>, 1, Headers),
  857. ok.
  858. range_ignore_when_not_modified(Config) ->
  859. doc("The range header must be ignored when a conditional "
  860. "GET results in a 304 not modified response. (RFC7233 3.1)"),
  861. ConnPid = gun_open(Config),
  862. Ref = gun:get(ConnPid, "/if_range", [
  863. {<<"accept-encoding">>, <<"gzip">>},
  864. {<<"range">>, <<"bytes=0-">>},
  865. {<<"if-none-match">>, <<"\"strong-and-match\"">>}
  866. ]),
  867. {response, fin, 304, Headers} = gun:await(ConnPid, Ref),
  868. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  869. false = lists:keyfind(<<"content-range">>, 1, Headers),
  870. ok.
  871. range_satisfiable(Config) ->
  872. doc("When the range_satisfiable callback returns true "
  873. "a 206 partial content response is expected for "
  874. "an otherwise valid range request. (RFC7233 4.1)"),
  875. ConnPid = gun_open(Config),
  876. Ref = gun:get(ConnPid, "/range_satisfiable?true", [
  877. {<<"accept-encoding">>, <<"gzip">>},
  878. {<<"range">>, <<"bytes=0-">>}
  879. ]),
  880. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  881. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  882. {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  883. ok.
  884. range_not_satisfiable(Config) ->
  885. doc("When the range_satisfiable callback returns false "
  886. "a 416 range not satisfiable response is expected for "
  887. "an otherwise valid range request. (RFC7233 4.4)"),
  888. ConnPid = gun_open(Config),
  889. Ref = gun:get(ConnPid, "/range_satisfiable?false", [
  890. {<<"accept-encoding">>, <<"gzip">>},
  891. {<<"range">>, <<"bytes=0-">>}
  892. ]),
  893. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  894. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  895. false = lists:keyfind(<<"content-range">>, 1, Headers),
  896. ok.
  897. range_not_satisfiable_int(Config) ->
  898. doc("When the range_satisfiable callback returns false "
  899. "a 416 range not satisfiable response is expected for "
  900. "an otherwise valid range request. If an integer is "
  901. "provided it is used to construct the content-range "
  902. "header. (RFC7233 4.2, RFC7233 4.4)"),
  903. ConnPid = gun_open(Config),
  904. Ref = gun:get(ConnPid, "/range_satisfiable?false-int", [
  905. {<<"accept-encoding">>, <<"gzip">>},
  906. {<<"range">>, <<"bytes=0-">>}
  907. ]),
  908. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  909. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  910. {_, <<"bytes */123">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  911. ok.
  912. range_not_satisfiable_bin(Config) ->
  913. doc("When the range_satisfiable callback returns false "
  914. "a 416 range not satisfiable response is expected for "
  915. "an otherwise valid range request. If a binary is "
  916. "provided it is used to construct the content-range "
  917. "header. (RFC7233 4.2, RFC7233 4.4)"),
  918. ConnPid = gun_open(Config),
  919. Ref = gun:get(ConnPid, "/range_satisfiable?false-bin", [
  920. {<<"accept-encoding">>, <<"gzip">>},
  921. {<<"range">>, <<"bytes=0-">>}
  922. ]),
  923. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  924. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  925. {_, <<"bytes */456">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  926. ok.
  927. range_satisfiable_missing(Config) ->
  928. doc("When the range_satisfiable callback is missing "
  929. "a 206 partial content response is expected for "
  930. "an otherwise valid range request. (RFC7233 4.1)"),
  931. ConnPid = gun_open(Config),
  932. Ref = gun:get(ConnPid, "/range_satisfiable?missing", [
  933. {<<"accept-encoding">>, <<"gzip">>},
  934. {<<"range">>, <<"bytes=0-">>}
  935. ]),
  936. {response, _, 206, Headers} = gun:await(ConnPid, Ref),
  937. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  938. {_, <<"bytes ", _/bits>>} = lists:keyfind(<<"content-range">>, 1, Headers),
  939. ok.
  940. ranges_provided_accept_ranges(Config) ->
  941. doc("When the ranges_provided callback exists the accept-ranges header "
  942. "is sent in the response. (RFC7233 2.3)"),
  943. ConnPid = gun_open(Config),
  944. Ref = gun:get(ConnPid, "/ranges_provided?list", [{<<"accept-encoding">>, <<"gzip">>}]),
  945. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  946. {_, <<"bytes, pages, chapters">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  947. ok.
  948. %% @todo Probably should have options to do this automatically for auto at least.
  949. %%
  950. %% A server that supports range requests MAY ignore or reject a Range
  951. %% header field that consists of more than two overlapping ranges, or a
  952. %% set of many small ranges that are not listed in ascending order,
  953. %% since both are indications of either a broken client or a deliberate
  954. %% denial-of-service attack (Section 6.1).
  955. %% @todo Probably should have options for auto as well to join ranges that
  956. %% are very close from each other.
  957. ranges_provided_auto_data(Config) ->
  958. doc("When the unit range is bytes and the callback is 'auto' "
  959. "Cowboy will call the normal ProvideCallback and perform "
  960. "the range calculations automatically."),
  961. ConnPid = gun_open(Config),
  962. Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
  963. {<<"accept-encoding">>, <<"gzip">>},
  964. {<<"range">>, <<"bytes=8-">>}
  965. ]),
  966. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  967. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  968. {_, <<"bytes 8-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  969. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  970. {ok, <<"ranged REST!">>} = gun:await_body(ConnPid, Ref),
  971. ok.
  972. ranges_provided_auto_sendfile(Config) ->
  973. doc("When the unit range is bytes and the callback is 'auto' "
  974. "Cowboy will call the normal ProvideCallback and perform "
  975. "the range calculations automatically."),
  976. ConnPid = gun_open(Config),
  977. Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
  978. {<<"accept-encoding">>, <<"gzip">>},
  979. {<<"range">>, <<"bytes=8-">>}
  980. ]),
  981. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  982. Size = filelib:file_size(Path),
  983. {ok, <<_:8/binary, Body/bits>>} = file:read_file(Path),
  984. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  985. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  986. {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
  987. ContentRange = iolist_to_binary([
  988. <<"bytes 8-">>,
  989. integer_to_binary(Size - 1),
  990. <<"/">>,
  991. integer_to_binary(Size)
  992. ]),
  993. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  994. {ok, Body} = gun:await_body(ConnPid, Ref),
  995. ok.
  996. ranges_provided_auto_multipart_data(Config) ->
  997. doc("When the unit range is bytes and the callback is 'auto' "
  998. "Cowboy will call the normal ProvideCallback and perform "
  999. "the range calculations automatically."),
  1000. ConnPid = gun_open(Config),
  1001. Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
  1002. {<<"accept-encoding">>, <<"gzip">>},
  1003. %% This range selects everything except the space characters.
  1004. {<<"range">>, <<"bytes=0-3, 5-6, 8-13, 15-">>}
  1005. ]),
  1006. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  1007. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  1008. false = lists:keyfind(<<"content-range">>, 1, Headers),
  1009. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  1010. = lists:keyfind(<<"content-type">>, 1, Headers),
  1011. {ok, Body0} = gun:await_body(ConnPid, Ref),
  1012. Body = do_decode(Headers, Body0),
  1013. %% We will receive the ranges in the same order as requested.
  1014. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  1015. [
  1016. {bytes, 0, 3, 20},
  1017. {bytes, 5, 6, 20},
  1018. {bytes, 8, 13, 20},
  1019. {bytes, 15, 19, 20}
  1020. ] = ContentRanges,
  1021. <<"ThisisrangedREST!">> = BodyAcc,
  1022. ok.
  1023. ranges_provided_auto_multipart_sendfile(Config) ->
  1024. doc("When the unit range is bytes and the callback is 'auto' "
  1025. "Cowboy will call the normal ProvideCallback and perform "
  1026. "the range calculations automatically."),
  1027. ConnPid = gun_open(Config),
  1028. Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
  1029. {<<"accept-encoding">>, <<"gzip">>},
  1030. %% This range selects a few random chunks of the file.
  1031. {<<"range">>, <<"bytes=50-99, 150-199, 250-299, -99">>}
  1032. ]),
  1033. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  1034. Size = filelib:file_size(Path),
  1035. Skip = Size - 399,
  1036. {ok, <<
  1037. _:50/binary, Body1:50/binary,
  1038. _:50/binary, Body2:50/binary,
  1039. _:50/binary, Body3:50/binary,
  1040. _:Skip/binary, Body4/bits>>} = file:read_file(Path),
  1041. {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
  1042. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  1043. false = lists:keyfind(<<"content-range">>, 1, Headers),
  1044. {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
  1045. = lists:keyfind(<<"content-type">>, 1, Headers),
  1046. {ok, Body0} = gun:await_body(ConnPid, Ref),
  1047. Body = do_decode(Headers, Body0),
  1048. %% We will receive the ranges in the same order as requested.
  1049. {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
  1050. LastFrom = 300 + Skip,
  1051. LastTo = Size - 1,
  1052. [
  1053. {bytes, 50, 99, Size},
  1054. {bytes, 150, 199, Size},
  1055. {bytes, 250, 299, Size},
  1056. {bytes, LastFrom, LastTo, Size}
  1057. ] = ContentRanges,
  1058. BodyAcc = <<Body1/binary, Body2/binary, Body3/binary, Body4/binary>>,
  1059. ok.
  1060. ranges_provided_auto_not_satisfiable_data(Config) ->
  1061. doc("When the unit range is bytes and the callback is 'auto' "
  1062. "Cowboy will call the normal ProvideCallback and perform "
  1063. "the range calculations automatically. When the requested "
  1064. "range is not satisfiable a 416 range not satisfiable response "
  1065. "is expected. The content-range header will be set. (RFC7233 4.4)"),
  1066. ConnPid = gun_open(Config),
  1067. Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
  1068. {<<"accept-encoding">>, <<"gzip">>},
  1069. {<<"range">>, <<"bytes=1000-">>}
  1070. ]),
  1071. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  1072. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  1073. {_, <<"bytes */20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  1074. ok.
  1075. ranges_provided_auto_not_satisfiable_sendfile(Config) ->
  1076. doc("When the unit range is bytes and the callback is 'auto' "
  1077. "Cowboy will call the normal ProvideCallback and perform "
  1078. "the range calculations automatically. When the requested "
  1079. "range is not satisfiable a 416 range not satisfiable response "
  1080. "is expected. The content-range header will be set. (RFC7233 4.4)"),
  1081. ConnPid = gun_open(Config),
  1082. Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
  1083. {<<"accept-encoding">>, <<"gzip">>},
  1084. {<<"range">>, <<"bytes=1000-">>}
  1085. ]),
  1086. {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
  1087. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  1088. Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
  1089. Size = filelib:file_size(Path),
  1090. ContentRange = iolist_to_binary([<<"bytes */">>, integer_to_binary(Size)]),
  1091. {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
  1092. ok.
  1093. ranges_provided_empty_accept_ranges_none(Config) ->
  1094. doc("When the ranges_provided callback exists but returns an empty list "
  1095. "the accept-ranges header is sent in the response with the value none. (RFC7233 2.3)"),
  1096. ConnPid = gun_open(Config),
  1097. Ref = gun:get(ConnPid, "/ranges_provided?none", [{<<"accept-encoding">>, <<"gzip">>}]),
  1098. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  1099. {_, <<"none">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  1100. ok.
  1101. ranges_provided_missing_no_accept_ranges(Config) ->
  1102. doc("When the ranges_provided callback does not exist "
  1103. "the accept-ranges header is not sent in the response."),
  1104. ConnPid = gun_open(Config),
  1105. Ref = gun:get(ConnPid, "/ranges_provided?missing", [{<<"accept-encoding">>, <<"gzip">>}]),
  1106. {response, _, 200, Headers} = gun:await(ConnPid, Ref),
  1107. false = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  1108. ok.
  1109. rate_limited(Config) ->
  1110. doc("A 429 response must be sent when the rate_limited callback returns true. "
  1111. "The retry-after header is specified as an integer. (RFC6585 4, RFC7231 7.1.3)"),
  1112. ConnPid = gun_open(Config),
  1113. Ref = gun:get(ConnPid, "/rate_limited?true", [{<<"accept-encoding">>, <<"gzip">>}]),
  1114. {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
  1115. {_, <<"3600">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
  1116. ok.
  1117. rate_limited_datetime(Config) ->
  1118. doc("A 429 response must be sent when the rate_limited callback returns true. "
  1119. "The retry-after header is specified as a date/time tuple. (RFC6585 4, RFC7231 7.1.3)"),
  1120. ConnPid = gun_open(Config),
  1121. Ref = gun:get(ConnPid, "/rate_limited?true-date", [{<<"accept-encoding">>, <<"gzip">>}]),
  1122. {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
  1123. {_, <<"Fri, 22 Feb 2222 11:11:11 GMT">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
  1124. ok.
  1125. rate_not_limited(Config) ->
  1126. doc("A success response must be sent when the rate_limited callback returns false."),
  1127. ConnPid = gun_open(Config),
  1128. Ref = gun:get(ConnPid, "/rate_limited?false", [{<<"accept-encoding">>, <<"gzip">>}]),
  1129. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  1130. ok.
  1131. stop_handler_allowed_methods(Config) ->
  1132. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1133. stop_handler_allow_missing_post(Config) ->
  1134. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  1135. stop_handler_charsets_provided(Config) ->
  1136. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1137. stop_handler_content_types_accepted(Config) ->
  1138. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  1139. stop_handler_content_types_provided(Config) ->
  1140. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1141. stop_handler_delete_completed(Config) ->
  1142. do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
  1143. stop_handler_delete_resource(Config) ->
  1144. do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
  1145. stop_handler_forbidden(Config) ->
  1146. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1147. stop_handler_is_authorized(Config) ->
  1148. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1149. stop_handler_is_conflict(Config) ->
  1150. do_req_body_stop_handler(Config, put, ?FUNCTION_NAME).
  1151. stop_handler_known_methods(Config) ->
  1152. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1153. stop_handler_languages_provided(Config) ->
  1154. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1155. stop_handler_malformed_request(Config) ->
  1156. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1157. stop_handler_moved_permanently(Config) ->
  1158. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1159. stop_handler_moved_temporarily(Config) ->
  1160. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1161. stop_handler_multiple_choices(Config) ->
  1162. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1163. stop_handler_options(Config) ->
  1164. do_no_body_stop_handler(Config, options, ?FUNCTION_NAME).
  1165. stop_handler_previously_existed(Config) ->
  1166. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1167. stop_handler_range_satisfiable(Config) ->
  1168. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1169. stop_handler_ranges_provided(Config) ->
  1170. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1171. stop_handler_rate_limited(Config) ->
  1172. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1173. stop_handler_resource_exists(Config) ->
  1174. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1175. stop_handler_service_available(Config) ->
  1176. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1177. stop_handler_uri_too_long(Config) ->
  1178. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1179. stop_handler_valid_content_headers(Config) ->
  1180. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1181. stop_handler_valid_entity_length(Config) ->
  1182. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1183. stop_handler_accept(Config) ->
  1184. do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
  1185. stop_handler_provide(Config) ->
  1186. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1187. stop_handler_provide_range(Config) ->
  1188. do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
  1189. do_no_body_stop_handler(Config, Method, StateName0) ->
  1190. doc("Send a response manually and stop the REST handler."),
  1191. ConnPid = gun_open(Config),
  1192. "stop_handler_" ++ StateName = atom_to_list(StateName0),
  1193. Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
  1194. {<<"accept-encoding">>, <<"gzip">>},
  1195. {<<"range">>, <<"bytes=0-">>}
  1196. ]),
  1197. {response, fin, 248, _} = gun:await(ConnPid, Ref),
  1198. ok.
  1199. do_req_body_stop_handler(Config, Method, StateName0) ->
  1200. doc("Send a response manually and stop the REST handler."),
  1201. ConnPid = gun_open(Config),
  1202. "stop_handler_" ++ StateName = atom_to_list(StateName0),
  1203. Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
  1204. {<<"accept-encoding">>, <<"gzip">>},
  1205. {<<"content-type">>, <<"text/plain">>}
  1206. ], <<"Hocus PocuSwitch!">>),
  1207. {response, fin, 248, _} = gun:await(ConnPid, Ref),
  1208. ok.
  1209. switch_handler_allowed_methods(Config) ->
  1210. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1211. switch_handler_allow_missing_post(Config) ->
  1212. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  1213. switch_handler_charsets_provided(Config) ->
  1214. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1215. switch_handler_content_types_accepted(Config) ->
  1216. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  1217. switch_handler_content_types_provided(Config) ->
  1218. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1219. switch_handler_delete_completed(Config) ->
  1220. do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
  1221. switch_handler_delete_resource(Config) ->
  1222. do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
  1223. switch_handler_forbidden(Config) ->
  1224. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1225. switch_handler_is_authorized(Config) ->
  1226. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1227. switch_handler_is_conflict(Config) ->
  1228. do_req_body_switch_handler(Config, put, ?FUNCTION_NAME).
  1229. switch_handler_known_methods(Config) ->
  1230. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1231. switch_handler_languages_provided(Config) ->
  1232. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1233. switch_handler_malformed_request(Config) ->
  1234. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1235. switch_handler_moved_permanently(Config) ->
  1236. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1237. switch_handler_moved_temporarily(Config) ->
  1238. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1239. switch_handler_multiple_choices(Config) ->
  1240. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1241. switch_handler_options(Config) ->
  1242. do_no_body_switch_handler(Config, options, ?FUNCTION_NAME).
  1243. switch_handler_previously_existed(Config) ->
  1244. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1245. switch_handler_range_satisfiable(Config) ->
  1246. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1247. switch_handler_ranges_provided(Config) ->
  1248. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1249. switch_handler_rate_limited(Config) ->
  1250. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1251. switch_handler_resource_exists(Config) ->
  1252. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1253. switch_handler_service_available(Config) ->
  1254. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1255. switch_handler_uri_too_long(Config) ->
  1256. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1257. switch_handler_valid_content_headers(Config) ->
  1258. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1259. switch_handler_valid_entity_length(Config) ->
  1260. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1261. switch_handler_accept(Config) ->
  1262. do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
  1263. switch_handler_provide(Config) ->
  1264. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1265. switch_handler_provide_range(Config) ->
  1266. do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
  1267. do_no_body_switch_handler(Config, Method, StateName0) ->
  1268. doc("Switch REST to loop handler for streaming the response body, "
  1269. "with and without options."),
  1270. "switch_handler_" ++ StateName = atom_to_list(StateName0),
  1271. do_no_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
  1272. do_no_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
  1273. do_no_body_switch_handler1(Config, Method, Path) ->
  1274. ConnPid = gun_open(Config),
  1275. Ref = gun:Method(ConnPid, Path, [
  1276. {<<"accept-encoding">>, <<"gzip">>},
  1277. {<<"range">>, <<"bytes=0-">>}
  1278. ]),
  1279. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  1280. {ok, Body} = gun:await_body(ConnPid, Ref),
  1281. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  1282. ok.
  1283. do_req_body_switch_handler(Config, Method, StateName0) ->
  1284. doc("Switch REST to loop handler for streaming the response body, "
  1285. "with and without options."),
  1286. "switch_handler_" ++ StateName = atom_to_list(StateName0),
  1287. do_req_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
  1288. do_req_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
  1289. do_req_body_switch_handler1(Config, Method, Path) ->
  1290. ConnPid = gun_open(Config),
  1291. Ref = gun:Method(ConnPid, Path, [
  1292. {<<"accept-encoding">>, <<"gzip">>},
  1293. {<<"content-type">>, <<"text/plain">>}
  1294. ], <<"Hocus PocuSwitch!">>),
  1295. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  1296. {ok, Body} = gun:await_body(ConnPid, Ref),
  1297. <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
  1298. ok.