rfc7231_SUITE.erl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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(rfc7231_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. -import(cowboy_test, [raw_open/1]).
  21. -import(cowboy_test, [raw_send/2]).
  22. -import(cowboy_test, [raw_recv_head/1]).
  23. -import(cowboy_test, [raw_recv/3]).
  24. all() ->
  25. cowboy_test:common_all().
  26. groups() ->
  27. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  28. init_per_group(Name, Config) ->
  29. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  30. end_per_group(Name, _) ->
  31. cowboy:stop_listener(Name).
  32. init_dispatch(_) ->
  33. cowboy_router:compile([{"[...]", [
  34. {"*", asterisk_h, []},
  35. {"/", hello_h, []},
  36. {"/echo/:key", echo_h, []},
  37. {"/delay/echo/:key", echo_h, []},
  38. {"/resp/:key[/:arg]", resp_h, []},
  39. {"/ws", ws_init_h, []}
  40. ]}]).
  41. %% @todo The documentation should list what methods, headers and status codes
  42. %% are handled automatically so users can know what befalls to them to implement.
  43. %% Representations.
  44. %% Cowboy has cowboy_compress_h that could be concerned with this.
  45. %% However Cowboy will not attempt to compress if any content-coding
  46. %% is already applied, regardless of what they are.
  47. %
  48. % If one or more encodings have been applied to a representation, the
  49. % sender that applied the encodings MUST generate a Content-Encoding
  50. % header field that lists the content codings in the order in which
  51. % they were applied. Additional information about the encoding
  52. % parameters can be provided by other header fields not defined by this
  53. % specification. (RFC7231 3.1.2.2)
  54. %% Methods.
  55. method_get(Config) ->
  56. doc("The GET method is accepted. (RFC7231 4.3.1)"),
  57. ConnPid = gun_open(Config),
  58. Ref = gun:get(ConnPid, "/", [
  59. {<<"accept-encoding">>, <<"gzip">>}
  60. ]),
  61. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  62. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  63. ok.
  64. method_head(Config) ->
  65. doc("The HEAD method is accepted. (RFC7231 4.3.2)"),
  66. ConnPid = gun_open(Config),
  67. Ref = gun:head(ConnPid, "/", [
  68. {<<"accept-encoding">>, <<"gzip">>}
  69. ]),
  70. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  71. ok.
  72. method_head_same_resp_headers_as_get(Config) ->
  73. doc("Responses to HEAD should return the same headers as GET. (RFC7231 4.3.2)"),
  74. ConnPid = gun_open(Config),
  75. Ref1 = gun:get(ConnPid, "/", [
  76. {<<"accept-encoding">>, <<"gzip">>}
  77. ]),
  78. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  79. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1),
  80. Ref2 = gun:head(ConnPid, "/", [
  81. {<<"accept-encoding">>, <<"gzip">>}
  82. ]),
  83. {response, fin, 200, Headers2} = gun:await(ConnPid, Ref2),
  84. %% We remove the date header since the date might have changed between requests.
  85. Headers = lists:keydelete(<<"date">>, 1, Headers1),
  86. Headers = lists:keydelete(<<"date">>, 1, Headers2),
  87. ok.
  88. method_head_same_resp_headers_as_get_stream_reply(Config) ->
  89. doc("Responses to HEAD should return the same headers as GET. (RFC7231 4.3.2)"),
  90. ConnPid = gun_open(Config),
  91. Ref1 = gun:get(ConnPid, "/resp/stream_reply2/200", [
  92. {<<"accept-encoding">>, <<"gzip">>}
  93. ]),
  94. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  95. {ok, _} = gun:await_body(ConnPid, Ref1),
  96. Ref2 = gun:head(ConnPid, "/resp/stream_reply2/200", [
  97. {<<"accept-encoding">>, <<"gzip">>}
  98. ]),
  99. {response, fin, 200, Headers2} = gun:await(ConnPid, Ref2),
  100. %% We remove the date header since the date might have changed between requests.
  101. Headers = lists:keydelete(<<"date">>, 1, Headers1),
  102. Headers = lists:keydelete(<<"date">>, 1, Headers2),
  103. ok.
  104. method_post(Config) ->
  105. doc("The POST method is accepted. (RFC7231 4.3.3)"),
  106. ConnPid = gun_open(Config),
  107. Ref = gun:post(ConnPid, "/echo/read_body", [
  108. {<<"accept-encoding">>, <<"gzip">>},
  109. {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
  110. ], <<"hello=world">>),
  111. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  112. {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
  113. ok.
  114. method_put(Config) ->
  115. doc("The PUT method is accepted. (RFC7231 4.3.4)"),
  116. ConnPid = gun_open(Config),
  117. Ref = gun:put(ConnPid, "/echo/read_body", [
  118. {<<"accept-encoding">>, <<"gzip">>},
  119. {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
  120. ], <<"hello=world">>),
  121. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  122. {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
  123. ok.
  124. method_delete(Config) ->
  125. doc("The DELETE method is accepted. (RFC7231 4.3.5)"),
  126. ConnPid = gun_open(Config),
  127. Ref = gun:delete(ConnPid, "/echo/method", [
  128. {<<"accept-encoding">>, <<"gzip">>}
  129. ]),
  130. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  131. {ok, <<"DELETE">>} = gun:await_body(ConnPid, Ref),
  132. ok.
  133. method_connect(Config) ->
  134. doc("The CONNECT method is currently not implemented. (RFC7231 4.3.6)"),
  135. ConnPid = gun_open(Config),
  136. Ref = gun:request(ConnPid, <<"CONNECT">>, "localhost:8080", [
  137. {<<"accept-encoding">>, <<"gzip">>}
  138. ]),
  139. {response, fin, 501, _} = gun:await(ConnPid, Ref),
  140. ok.
  141. % A client sending a CONNECT request MUST send the authority form of
  142. % request-target (Section 5.3 of [RFC7230]); i.e., the request-target
  143. % consists of only the host name and port number of the tunnel
  144. % destination, separated by a colon.
  145. %
  146. % A server MUST NOT send any Transfer-Encoding or Content-Length header
  147. % fields in a 2xx (Successful) response to CONNECT. A client MUST
  148. % ignore any Content-Length or Transfer-Encoding header fields received
  149. % in a successful response to CONNECT.
  150. %
  151. % A payload within a CONNECT request message has no defined semantics;
  152. % sending a payload body on a CONNECT request might cause some existing
  153. % implementations to reject the request.
  154. method_options(Config) ->
  155. doc("The OPTIONS method is accepted. (RFC7231 4.3.7)"),
  156. ConnPid = gun_open(Config),
  157. Ref = gun:options(ConnPid, "/echo/method", [
  158. {<<"accept-encoding">>, <<"gzip">>}
  159. ]),
  160. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  161. {ok, <<"OPTIONS">>} = gun:await_body(ConnPid, Ref),
  162. ok.
  163. method_options_asterisk(Config) ->
  164. doc("The OPTIONS method is accepted with an asterisk. (RFC7231 4.3.7)"),
  165. ConnPid = gun_open(Config),
  166. Ref = gun:options(ConnPid, "*", [
  167. {<<"accept-encoding">>, <<"gzip">>},
  168. {<<"x-echo">>, <<"method">>}
  169. ]),
  170. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  171. {ok, <<"OPTIONS">>} = gun:await_body(ConnPid, Ref),
  172. ok.
  173. method_options_content_length_0(Config) ->
  174. doc("The OPTIONS method must set the content-length header "
  175. "to 0 when no body is returned. (RFC7231 4.3.7)"),
  176. ConnPid = gun_open(Config),
  177. Ref = gun:options(ConnPid, "*", [
  178. {<<"accept-encoding">>, <<"gzip">>}
  179. ]),
  180. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  181. {_, <<"0">>} = lists:keyfind(<<"content-length">>, 1, Headers),
  182. ok.
  183. method_trace(Config) ->
  184. doc("The TRACE method is currently not implemented. (RFC7231 4.3.8)"),
  185. ConnPid = gun_open(Config),
  186. Ref = gun:request(ConnPid, <<"TRACE">>, "/", [
  187. {<<"accept-encoding">>, <<"gzip">>}
  188. ]),
  189. {response, fin, 501, _} = gun:await(ConnPid, Ref),
  190. ok.
  191. %% Request headers.
  192. expect(Config) ->
  193. doc("A server that receives a 100-continue expectation should honor it. (RFC7231 5.1.1)"),
  194. ConnPid = gun_open(Config),
  195. Ref = gun:post(ConnPid, "/echo/read_body", [
  196. {<<"accept-encoding">>, <<"gzip">>},
  197. {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
  198. {<<"expect">>, <<"100-continue">>}
  199. ]),
  200. {inform, 100, _} = gun:await(ConnPid, Ref),
  201. ok.
  202. http10_expect(Config) ->
  203. case config(protocol, Config) of
  204. http ->
  205. do_http10_expect(Config);
  206. http2 ->
  207. expect(Config)
  208. end.
  209. do_http10_expect(Config) ->
  210. doc("A server that receives a 100-continue expectation "
  211. "in an HTTP/1.0 request must ignore it. (RFC7231 5.1.1)"),
  212. Body = <<"hello=world">>,
  213. ConnPid = gun_open([{http_opts, #{version => 'HTTP/1.0'}}|Config]),
  214. Ref = gun:post(ConnPid, "/echo/read_body", [
  215. {<<"accept-encoding">>, <<"gzip">>},
  216. {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
  217. {<<"content-length">>, integer_to_binary(byte_size(Body))},
  218. {<<"expect">>, <<"100-continue">>}
  219. ]),
  220. timer:sleep(500),
  221. ok = gun:data(ConnPid, Ref, fin, Body),
  222. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  223. {ok, Body} = gun:await_body(ConnPid, Ref),
  224. ok.
  225. %% Cowboy ignores the expect header when the value is not 100-continue.
  226. %
  227. % A server that receives an Expect field-value other than 100-continue
  228. % MAY respond with a 417 (Expectation Failed) status code to indicate
  229. % that the unexpected expectation cannot be met.
  230. expect_receive_body_omit_100_continue(Config) ->
  231. doc("A server may omit sending a 100 Continue response if it has "
  232. "already started receiving the request body. (RFC7231 5.1.1)"),
  233. ConnPid = gun_open(Config),
  234. Ref = gun:post(ConnPid, "/delay/echo/read_body", [
  235. {<<"accept-encoding">>, <<"gzip">>},
  236. {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
  237. {<<"expect">>, <<"100-continue">>}
  238. ], <<"hello=world">>),
  239. %% We receive the response directly without a 100 Continue.
  240. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  241. {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
  242. ok.
  243. expect_discard_body_skip(Config) ->
  244. doc("A server that responds with a final status code before reading "
  245. "the entire message body should keep the connection open and skip "
  246. "the body when appropriate. (RFC7231 5.1.1)"),
  247. ConnPid = gun_open(Config),
  248. Ref1 = gun:post(ConnPid, "/echo/method", [
  249. {<<"accept-encoding">>, <<"gzip">>},
  250. {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
  251. {<<"expect">>, <<"100-continue">>}
  252. ], <<"hello=world">>),
  253. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  254. {ok, <<"POST">>} = gun:await_body(ConnPid, Ref1),
  255. Ref2 = gun:get(ConnPid, "/echo/method", [
  256. {<<"accept-encoding">>, <<"gzip">>},
  257. {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
  258. ]),
  259. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  260. {ok, <<"GET">>} = gun:await_body(ConnPid, Ref2),
  261. ok.
  262. expect_discard_body_close(Config) ->
  263. case config(protocol, Config) of
  264. http ->
  265. do_expect_discard_body_close(Config);
  266. http2 ->
  267. doc("There's no reason to close the connection when using HTTP/2, "
  268. "even if a stream body is too large. We just cancel the stream.")
  269. end.
  270. do_expect_discard_body_close(Config) ->
  271. doc("A server that responds with a final status code before reading "
  272. "the entire message body may close the connection to avoid "
  273. "reading a potentially large request body. (RFC7231 5.1.1, RFC7230 6.6)"),
  274. ConnPid = gun_open(Config),
  275. Ref1 = gun:post(ConnPid, "/echo/method", [
  276. {<<"accept-encoding">>, <<"gzip">>},
  277. {<<"content-length">>, <<"10000000">>},
  278. {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
  279. {<<"expect">>, <<"100-continue">>}
  280. ]),
  281. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref1),
  282. %% Ideally we would send a connection: close. Cowboy however
  283. %% cannot know the intent of the application until after we
  284. %% sent the response.
  285. % {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  286. {ok, <<"POST">>} = gun:await_body(ConnPid, Ref1),
  287. %% The connection is gone.
  288. receive
  289. {gun_down, ConnPid, _, closed, _, _} ->
  290. ok
  291. after 1000 ->
  292. error(timeout)
  293. end.
  294. no_accept_encoding(Config) ->
  295. doc("While a request with no accept-encoding header implies the "
  296. "user agent has no preferences and any would be acceptable, "
  297. "Cowboy will not serve content-codings by defaults to ensure "
  298. "the content can safely be read. (RFC7231 5.3.4)"),
  299. ConnPid = gun_open(Config),
  300. Ref = gun:get(ConnPid, "/resp/stream_reply2/200"),
  301. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  302. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  303. ok.
  304. %% Cowboy currently ignores any information about the identity content-coding
  305. %% and instead considers it always acceptable.
  306. %
  307. % 2. If the representation has no content-coding, then it is
  308. % acceptable by default unless specifically excluded by the
  309. % Accept-Encoding field stating either "identity;q=0" or "*;q=0"
  310. % without a more specific entry for "identity".
  311. accept_encoding_gzip(Config) ->
  312. doc("No qvalue means the content-coding is acceptable. (RFC7231 5.3.4)"),
  313. ConnPid = gun_open(Config),
  314. Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
  315. {<<"accept-encoding">>, <<"gzip">>}
  316. ]),
  317. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  318. _ = case config(flavor, Config) of
  319. compress ->
  320. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
  321. _ ->
  322. false = lists:keyfind(<<"content-encoding">>, 1, Headers)
  323. end,
  324. ok.
  325. accept_encoding_gzip_1(Config) ->
  326. doc("A qvalue different than 0 means the content-coding is acceptable. (RFC7231 5.3.4)"),
  327. ConnPid = gun_open(Config),
  328. Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
  329. {<<"accept-encoding">>, <<"gzip;q=1.0">>}
  330. ]),
  331. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  332. _ = case config(flavor, Config) of
  333. compress ->
  334. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
  335. _ ->
  336. false = lists:keyfind(<<"content-encoding">>, 1, Headers)
  337. end,
  338. ok.
  339. accept_encoding_gzip_0(Config) ->
  340. doc("A qvalue of 0 means the content-coding is not acceptable. (RFC7231 5.3.1, RFC7231 5.3.4)"),
  341. ConnPid = gun_open(Config),
  342. Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
  343. {<<"accept-encoding">>, <<"gzip;q=0">>}
  344. ]),
  345. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  346. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  347. ok.
  348. %% Cowboy currently only supports gzip automatically via cowboy_compress_h.
  349. %
  350. % 4. If multiple content-codings are acceptable, then the acceptable
  351. % content-coding with the highest non-zero qvalue is preferred.
  352. accept_encoding_empty(Config) ->
  353. doc("An empty content-coding means that the user agent does not "
  354. "want any content-coding applied to the response. (RFC7231 5.3.4)"),
  355. ConnPid = gun_open(Config),
  356. Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
  357. {<<"accept-encoding">>, <<>>}
  358. ]),
  359. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  360. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  361. ok.
  362. accept_encoding_unknown(Config) ->
  363. doc("An accept-encoding header only containing unknown content-codings "
  364. "should result in no content-coding being applied. (RFC7231 5.3.4)"),
  365. ConnPid = gun_open(Config),
  366. Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
  367. {<<"accept-encoding">>, <<"deflate">>}
  368. ]),
  369. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  370. false = lists:keyfind(<<"content-encoding">>, 1, Headers),
  371. ok.
  372. %% Status codes.
  373. http10_status_code_100(Config) ->
  374. case config(protocol, Config) of
  375. http ->
  376. doc("The 100 Continue status code must not "
  377. "be sent to HTTP/1.0 endpoints. (RFC7231 6.2)"),
  378. do_http10_status_code_1xx(100, Config);
  379. http2 ->
  380. status_code_100(Config)
  381. end.
  382. http10_status_code_101(Config) ->
  383. case config(protocol, Config) of
  384. http ->
  385. doc("The 101 Switching Protocols status code must not "
  386. "be sent to HTTP/1.0 endpoints. (RFC7231 6.2)"),
  387. do_http10_status_code_1xx(101, Config);
  388. http2 ->
  389. status_code_101(Config)
  390. end.
  391. do_http10_status_code_1xx(StatusCode, Config) ->
  392. ConnPid = gun_open([{http_opts, #{version => 'HTTP/1.0'}}|Config]),
  393. Ref = gun:get(ConnPid, "/resp/inform2/" ++ integer_to_list(StatusCode), [
  394. {<<"accept-encoding">>, <<"gzip">>}
  395. ]),
  396. {response, _, 200, _} = gun:await(ConnPid, Ref),
  397. ok.
  398. status_code_100(Config) ->
  399. doc("The 100 Continue status code can be sent. (RFC7231 6.2.1)"),
  400. ConnPid = gun_open(Config),
  401. Ref = gun:get(ConnPid, "/resp/inform2/100", [
  402. {<<"accept-encoding">>, <<"gzip">>}
  403. ]),
  404. {inform, 100, []} = gun:await(ConnPid, Ref),
  405. ok.
  406. status_code_101(Config) ->
  407. doc("The 101 Switching Protocols status code can be sent. (RFC7231 6.2.2)"),
  408. ConnPid = gun_open(Config),
  409. Ref = gun:get(ConnPid, "/resp/inform2/101", [
  410. {<<"accept-encoding">>, <<"gzip">>}
  411. ]),
  412. {inform, 101, []} = gun:await(ConnPid, Ref),
  413. ok.
  414. status_code_200(Config) ->
  415. doc("The 200 OK status code can be sent. (RFC7231 6.3.1)"),
  416. ConnPid = gun_open(Config),
  417. Ref = gun:get(ConnPid, "/resp/reply2/200", [
  418. {<<"accept-encoding">>, <<"gzip">>}
  419. ]),
  420. {response, _, 200, _} = gun:await(ConnPid, Ref),
  421. ok.
  422. status_code_201(Config) ->
  423. doc("The 201 Created status code can be sent. (RFC7231 6.3.2)"),
  424. ConnPid = gun_open(Config),
  425. Ref = gun:get(ConnPid, "/resp/reply2/201", [
  426. {<<"accept-encoding">>, <<"gzip">>}
  427. ]),
  428. {response, _, 201, _} = gun:await(ConnPid, Ref),
  429. ok.
  430. status_code_202(Config) ->
  431. doc("The 202 Accepted status code can be sent. (RFC7231 6.3.3)"),
  432. ConnPid = gun_open(Config),
  433. Ref = gun:get(ConnPid, "/resp/reply2/202", [
  434. {<<"accept-encoding">>, <<"gzip">>}
  435. ]),
  436. {response, _, 202, _} = gun:await(ConnPid, Ref),
  437. ok.
  438. status_code_203(Config) ->
  439. doc("The 203 Non-Authoritative Information status code can be sent. (RFC7231 6.3.4)"),
  440. ConnPid = gun_open(Config),
  441. Ref = gun:get(ConnPid, "/resp/reply2/203", [
  442. {<<"accept-encoding">>, <<"gzip">>}
  443. ]),
  444. {response, _, 203, _} = gun:await(ConnPid, Ref),
  445. ok.
  446. status_code_204(Config) ->
  447. doc("The 204 No Content status code can be sent. (RFC7231 6.3.5)"),
  448. ConnPid = gun_open(Config),
  449. Ref = gun:get(ConnPid, "/resp/reply2/204", [
  450. {<<"accept-encoding">>, <<"gzip">>}
  451. ]),
  452. {response, _, 204, _} = gun:await(ConnPid, Ref),
  453. ok.
  454. status_code_205(Config) ->
  455. doc("The 205 Reset Content status code can be sent. (RFC7231 6.3.6)"),
  456. ConnPid = gun_open(Config),
  457. Ref = gun:get(ConnPid, "/resp/reply2/205", [
  458. {<<"accept-encoding">>, <<"gzip">>}
  459. ]),
  460. {response, _, 205, _} = gun:await(ConnPid, Ref),
  461. ok.
  462. status_code_300(Config) ->
  463. doc("The 300 Multiple Choices status code can be sent. (RFC7231 6.4.1)"),
  464. ConnPid = gun_open(Config),
  465. Ref = gun:get(ConnPid, "/resp/reply2/300", [
  466. {<<"accept-encoding">>, <<"gzip">>}
  467. ]),
  468. {response, _, 300, _} = gun:await(ConnPid, Ref),
  469. ok.
  470. status_code_301(Config) ->
  471. doc("The 301 Moved Permanently status code can be sent. (RFC7231 6.4.2)"),
  472. ConnPid = gun_open(Config),
  473. Ref = gun:get(ConnPid, "/resp/reply2/301", [
  474. {<<"accept-encoding">>, <<"gzip">>}
  475. ]),
  476. {response, _, 301, _} = gun:await(ConnPid, Ref),
  477. ok.
  478. status_code_302(Config) ->
  479. doc("The 302 Found status code can be sent. (RFC7231 6.4.3)"),
  480. ConnPid = gun_open(Config),
  481. Ref = gun:get(ConnPid, "/resp/reply2/302", [
  482. {<<"accept-encoding">>, <<"gzip">>}
  483. ]),
  484. {response, _, 302, _} = gun:await(ConnPid, Ref),
  485. ok.
  486. status_code_303(Config) ->
  487. doc("The 303 See Other status code can be sent. (RFC7231 6.4.4)"),
  488. ConnPid = gun_open(Config),
  489. Ref = gun:get(ConnPid, "/resp/reply2/303", [
  490. {<<"accept-encoding">>, <<"gzip">>}
  491. ]),
  492. {response, _, 303, _} = gun:await(ConnPid, Ref),
  493. ok.
  494. status_code_305(Config) ->
  495. doc("The 305 Use Proxy status code can be sent. (RFC7231 6.4.5)"),
  496. ConnPid = gun_open(Config),
  497. Ref = gun:get(ConnPid, "/resp/reply2/305", [
  498. {<<"accept-encoding">>, <<"gzip">>}
  499. ]),
  500. {response, _, 305, _} = gun:await(ConnPid, Ref),
  501. ok.
  502. %% The status code 306 is no longer used. (RFC7231 6.4.6)
  503. status_code_307(Config) ->
  504. doc("The 307 Temporary Redirect status code can be sent. (RFC7231 6.4.7)"),
  505. ConnPid = gun_open(Config),
  506. Ref = gun:get(ConnPid, "/resp/reply2/307", [
  507. {<<"accept-encoding">>, <<"gzip">>}
  508. ]),
  509. {response, _, 307, _} = gun:await(ConnPid, Ref),
  510. ok.
  511. status_code_400(Config) ->
  512. doc("The 400 Bad Request status code can be sent. (RFC7231 6.5.1)"),
  513. ConnPid = gun_open(Config),
  514. Ref = gun:get(ConnPid, "/resp/reply2/400", [
  515. {<<"accept-encoding">>, <<"gzip">>}
  516. ]),
  517. {response, _, 400, _} = gun:await(ConnPid, Ref),
  518. ok.
  519. status_code_402(Config) ->
  520. doc("The 402 Payment Required status code can be sent. (RFC7231 6.5.2)"),
  521. ConnPid = gun_open(Config),
  522. Ref = gun:get(ConnPid, "/resp/reply2/402", [
  523. {<<"accept-encoding">>, <<"gzip">>}
  524. ]),
  525. {response, _, 402, _} = gun:await(ConnPid, Ref),
  526. ok.
  527. status_code_403(Config) ->
  528. doc("The 403 Forbidden status code can be sent. (RFC7231 6.5.3)"),
  529. ConnPid = gun_open(Config),
  530. Ref = gun:get(ConnPid, "/resp/reply2/403", [
  531. {<<"accept-encoding">>, <<"gzip">>}
  532. ]),
  533. {response, _, 403, _} = gun:await(ConnPid, Ref),
  534. ok.
  535. status_code_404(Config) ->
  536. doc("The 404 Not Found status code can be sent. (RFC7231 6.5.4)"),
  537. ConnPid = gun_open(Config),
  538. Ref = gun:get(ConnPid, "/resp/reply2/404", [
  539. {<<"accept-encoding">>, <<"gzip">>}
  540. ]),
  541. {response, _, 404, _} = gun:await(ConnPid, Ref),
  542. ok.
  543. status_code_405(Config) ->
  544. doc("The 405 Method Not Allowed status code can be sent. (RFC7231 6.5.5)"),
  545. ConnPid = gun_open(Config),
  546. Ref = gun:get(ConnPid, "/resp/reply2/405", [
  547. {<<"accept-encoding">>, <<"gzip">>}
  548. ]),
  549. {response, _, 405, _} = gun:await(ConnPid, Ref),
  550. ok.
  551. status_code_406(Config) ->
  552. doc("The 406 Not Acceptable status code can be sent. (RFC7231 6.5.6)"),
  553. ConnPid = gun_open(Config),
  554. Ref = gun:get(ConnPid, "/resp/reply2/406", [
  555. {<<"accept-encoding">>, <<"gzip">>}
  556. ]),
  557. {response, _, 406, _} = gun:await(ConnPid, Ref),
  558. ok.
  559. status_code_408(Config) ->
  560. doc("The 408 Request Timeout status code can be sent. (RFC7231 6.5.7)"),
  561. ConnPid = gun_open(Config),
  562. Ref = gun:get(ConnPid, "/resp/reply2/408", [
  563. {<<"accept-encoding">>, <<"gzip">>}
  564. ]),
  565. {response, _, 408, _} = gun:await(ConnPid, Ref),
  566. ok.
  567. status_code_408_connection_close(Config) ->
  568. case config(protocol, Config) of
  569. http ->
  570. do_http11_status_code_408_connection_close(Config);
  571. http2 ->
  572. doc("HTTP/2 connections are not closed on 408 responses.")
  573. end.
  574. do_http11_status_code_408_connection_close(Config) ->
  575. doc("A 408 response should result in a connection close "
  576. "for HTTP/1.1 connections. (RFC7231 6.5.7)"),
  577. Client = raw_open(Config),
  578. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  579. {_, 408, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
  580. {Headers, <<>>} = cow_http:parse_headers(Rest),
  581. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  582. {error, closed} = raw_recv(Client, 0, 1000),
  583. ok.
  584. status_code_409(Config) ->
  585. doc("The 409 Conflict status code can be sent. (RFC7231 6.5.8)"),
  586. ConnPid = gun_open(Config),
  587. Ref = gun:get(ConnPid, "/resp/reply2/409", [
  588. {<<"accept-encoding">>, <<"gzip">>}
  589. ]),
  590. {response, _, 409, _} = gun:await(ConnPid, Ref),
  591. ok.
  592. status_code_410(Config) ->
  593. doc("The 410 Gone status code can be sent. (RFC7231 6.5.9)"),
  594. ConnPid = gun_open(Config),
  595. Ref = gun:get(ConnPid, "/resp/reply2/410", [
  596. {<<"accept-encoding">>, <<"gzip">>}
  597. ]),
  598. {response, _, 410, _} = gun:await(ConnPid, Ref),
  599. ok.
  600. status_code_411(Config) ->
  601. doc("The 411 Length Required status code can be sent. (RFC7231 6.5.10)"),
  602. ConnPid = gun_open(Config),
  603. Ref = gun:get(ConnPid, "/resp/reply2/411", [
  604. {<<"accept-encoding">>, <<"gzip">>}
  605. ]),
  606. {response, _, 411, _} = gun:await(ConnPid, Ref),
  607. ok.
  608. status_code_413(Config) ->
  609. doc("The 413 Payload Too Large status code can be sent. (RFC7231 6.5.11)"),
  610. ConnPid = gun_open(Config),
  611. Ref = gun:get(ConnPid, "/resp/reply2/413", [
  612. {<<"accept-encoding">>, <<"gzip">>}
  613. ]),
  614. {response, _, 413, _} = gun:await(ConnPid, Ref),
  615. ok.
  616. status_code_414(Config) ->
  617. doc("The 414 URI Too Long status code can be sent. (RFC7231 6.5.12)"),
  618. ConnPid = gun_open(Config),
  619. Ref = gun:get(ConnPid, "/resp/reply2/414", [
  620. {<<"accept-encoding">>, <<"gzip">>}
  621. ]),
  622. {response, _, 414, _} = gun:await(ConnPid, Ref),
  623. ok.
  624. status_code_415(Config) ->
  625. doc("The 415 Unsupported Media Type status code can be sent. (RFC7231 6.5.13)"),
  626. ConnPid = gun_open(Config),
  627. Ref = gun:get(ConnPid, "/resp/reply2/415", [
  628. {<<"accept-encoding">>, <<"gzip">>}
  629. ]),
  630. {response, _, 415, _} = gun:await(ConnPid, Ref),
  631. ok.
  632. status_code_417(Config) ->
  633. doc("The 417 Expectation Failed status code can be sent. (RFC7231 6.5.14)"),
  634. ConnPid = gun_open(Config),
  635. Ref = gun:get(ConnPid, "/resp/reply2/417", [
  636. {<<"accept-encoding">>, <<"gzip">>}
  637. ]),
  638. {response, _, 417, _} = gun:await(ConnPid, Ref),
  639. ok.
  640. status_code_426(Config) ->
  641. doc("The 426 Upgrade Required status code can be sent. (RFC7231 6.5.15)"),
  642. ConnPid = gun_open(Config),
  643. Ref = gun:get(ConnPid, "/resp/reply2/426", [
  644. {<<"accept-encoding">>, <<"gzip">>}
  645. ]),
  646. {response, _, 426, _} = gun:await(ConnPid, Ref),
  647. ok.
  648. status_code_426_upgrade_header(Config) ->
  649. case config(protocol, Config) of
  650. http ->
  651. do_status_code_426_upgrade_header(Config);
  652. http2 ->
  653. doc("HTTP/2 does not support the HTTP/1.1 Upgrade mechanism.")
  654. end.
  655. do_status_code_426_upgrade_header(Config) ->
  656. doc("A 426 response must include a upgrade header. (RFC7231 6.5.15)"),
  657. ConnPid = gun_open(Config),
  658. Ref = gun:get(ConnPid, "/ws?ok", [
  659. {<<"accept-encoding">>, <<"gzip">>}
  660. ]),
  661. {response, _, 426, Headers} = gun:await(ConnPid, Ref),
  662. {_, <<"upgrade">>} = lists:keyfind(<<"connection">>, 1, Headers),
  663. {_, <<"websocket">>} = lists:keyfind(<<"upgrade">>, 1, Headers),
  664. ok.
  665. status_code_500(Config) ->
  666. doc("The 500 Internal Server Error status code can be sent. (RFC7231 6.6.1)"),
  667. ConnPid = gun_open(Config),
  668. Ref = gun:get(ConnPid, "/resp/reply2/500", [
  669. {<<"accept-encoding">>, <<"gzip">>}
  670. ]),
  671. {response, _, 500, _} = gun:await(ConnPid, Ref),
  672. ok.
  673. status_code_501(Config) ->
  674. doc("The 501 Not Implemented status code can be sent. (RFC7231 6.6.2)"),
  675. ConnPid = gun_open(Config),
  676. Ref = gun:get(ConnPid, "/resp/reply2/501", [
  677. {<<"accept-encoding">>, <<"gzip">>}
  678. ]),
  679. {response, _, 501, _} = gun:await(ConnPid, Ref),
  680. ok.
  681. status_code_502(Config) ->
  682. doc("The 502 Bad Gateway status code can be sent. (RFC7231 6.6.3)"),
  683. ConnPid = gun_open(Config),
  684. Ref = gun:get(ConnPid, "/resp/reply2/502", [
  685. {<<"accept-encoding">>, <<"gzip">>}
  686. ]),
  687. {response, _, 502, _} = gun:await(ConnPid, Ref),
  688. ok.
  689. status_code_503(Config) ->
  690. doc("The 503 Service Unavailable status code can be sent. (RFC7231 6.6.4)"),
  691. ConnPid = gun_open(Config),
  692. Ref = gun:get(ConnPid, "/resp/reply2/503", [
  693. {<<"accept-encoding">>, <<"gzip">>}
  694. ]),
  695. {response, _, 503, _} = gun:await(ConnPid, Ref),
  696. ok.
  697. status_code_504(Config) ->
  698. doc("The 504 Gateway Timeout status code can be sent. (RFC7231 6.6.5)"),
  699. ConnPid = gun_open(Config),
  700. Ref = gun:get(ConnPid, "/resp/reply2/504", [
  701. {<<"accept-encoding">>, <<"gzip">>}
  702. ]),
  703. {response, _, 504, _} = gun:await(ConnPid, Ref),
  704. ok.
  705. status_code_505(Config) ->
  706. doc("The 505 HTTP Version Not Supported status code can be sent. (RFC7231 6.6.6)"),
  707. ConnPid = gun_open(Config),
  708. Ref = gun:get(ConnPid, "/resp/reply2/505", [
  709. {<<"accept-encoding">>, <<"gzip">>}
  710. ]),
  711. {response, _, 505, _} = gun:await(ConnPid, Ref),
  712. ok.
  713. %% The 505 response code is supposed to be about the major HTTP version.
  714. %% Cowboy instead rejects any version that isn't HTTP/1.0 or HTTP/1.1
  715. %% when expecting an h1 request. While this is not correct in theory
  716. %% it works in practice because there are no other minor versions.
  717. %%
  718. %% Cowboy does not do version checking for HTTP/2 since the protocol
  719. %% does not include a version number in the messages.