rfc7231_SUITE.erl 32 KB

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