rfc7231_SUITE.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. all() ->
  21. cowboy_test:common_all().
  22. groups() ->
  23. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  24. init_per_group(Name, Config) ->
  25. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  26. end_per_group(Name, _) ->
  27. cowboy:stop_listener(Name).
  28. init_dispatch(_) ->
  29. cowboy_router:compile([{"[...]", [
  30. {"/", hello_h, []},
  31. {"/echo/:key", echo_h, []},
  32. {"/resp/:key[/:arg]", resp_h, []}
  33. ]}]).
  34. %% @todo The documentation should list what methods, headers and status codes
  35. %% are handled automatically so users can know what befalls to them to implement.
  36. %% Methods.
  37. method_get(Config) ->
  38. doc("The GET method is accepted. (RFC7231 4.3.1)"),
  39. ConnPid = gun_open(Config),
  40. Ref = gun:get(ConnPid, "/", [
  41. {<<"accept-encoding">>, <<"gzip">>}
  42. ]),
  43. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  44. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  45. ok.
  46. method_head(Config) ->
  47. doc("The HEAD method is accepted. (RFC7231 4.3.2)"),
  48. ConnPid = gun_open(Config),
  49. Ref = gun:head(ConnPid, "/", [
  50. {<<"accept-encoding">>, <<"gzip">>}
  51. ]),
  52. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  53. ok.
  54. method_head_same_resp_headers_as_get(Config) ->
  55. doc("Responses to HEAD should return the same headers as GET. (RFC7231 4.3.2)"),
  56. ConnPid = gun_open(Config),
  57. Ref1 = gun:get(ConnPid, "/", [
  58. {<<"accept-encoding">>, <<"gzip">>}
  59. ]),
  60. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  61. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1),
  62. Ref2 = gun:head(ConnPid, "/", [
  63. {<<"accept-encoding">>, <<"gzip">>}
  64. ]),
  65. {response, fin, 200, Headers2} = gun:await(ConnPid, Ref2),
  66. %% We remove the date header since the date might have changed between requests.
  67. Headers = lists:keydelete(<<"date">>, 1, Headers1),
  68. Headers = lists:keydelete(<<"date">>, 1, Headers2),
  69. ok.
  70. method_head_same_resp_headers_as_get_stream_reply(Config) ->
  71. doc("Responses to HEAD should return the same headers as GET. (RFC7231 4.3.2)"),
  72. ConnPid = gun_open(Config),
  73. Ref1 = gun:get(ConnPid, "/resp/stream_reply2/200", [
  74. {<<"accept-encoding">>, <<"gzip">>}
  75. ]),
  76. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  77. {ok, _} = gun:await_body(ConnPid, Ref1),
  78. Ref2 = gun:head(ConnPid, "/resp/stream_reply2/200", [
  79. {<<"accept-encoding">>, <<"gzip">>}
  80. ]),
  81. {response, fin, 200, Headers2} = gun:await(ConnPid, Ref2),
  82. %% We remove the date header since the date might have changed between requests.
  83. Headers = lists:keydelete(<<"date">>, 1, Headers1),
  84. Headers = lists:keydelete(<<"date">>, 1, Headers2),
  85. ok.
  86. method_post(Config) ->
  87. doc("The POST method is accepted. (RFC7231 4.3.3)"),
  88. ConnPid = gun_open(Config),
  89. Ref = gun:post(ConnPid, "/echo/read_body", [
  90. {<<"accept-encoding">>, <<"gzip">>},
  91. {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
  92. ], <<"hello=world">>),
  93. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  94. {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
  95. ok.
  96. method_put(Config) ->
  97. doc("The PUT method is accepted. (RFC7231 4.3.4)"),
  98. ConnPid = gun_open(Config),
  99. Ref = gun:put(ConnPid, "/echo/read_body", [
  100. {<<"accept-encoding">>, <<"gzip">>},
  101. {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
  102. ], <<"hello=world">>),
  103. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  104. {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
  105. ok.
  106. method_delete(Config) ->
  107. doc("The DELETE method is accepted. (RFC7231 4.3.5)"),
  108. ConnPid = gun_open(Config),
  109. Ref = gun:delete(ConnPid, "/echo/method", [
  110. {<<"accept-encoding">>, <<"gzip">>}
  111. ]),
  112. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  113. {ok, <<"DELETE">>} = gun:await_body(ConnPid, Ref),
  114. ok.
  115. method_connect(Config) ->
  116. doc("The CONNECT method is currently not implemented. (RFC7231 4.3.6)"),
  117. ConnPid = gun_open(Config),
  118. Ref = gun:request(ConnPid, <<"CONNECT">>, "localhost:8080", [
  119. {<<"accept-encoding">>, <<"gzip">>}
  120. ]),
  121. {response, fin, 501, _} = gun:await(ConnPid, Ref),
  122. ok.
  123. method_options(Config) ->
  124. doc("The OPTIONS method is accepted. (RFC7231 4.3.7)"),
  125. ConnPid = gun_open(Config),
  126. Ref = gun:options(ConnPid, "/echo/method", [
  127. {<<"accept-encoding">>, <<"gzip">>}
  128. ]),
  129. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  130. {ok, <<"OPTIONS">>} = gun:await_body(ConnPid, Ref),
  131. ok.
  132. %method_options_asterisk(Config) ->
  133. %method_options_content_length_0(Config) ->
  134. %% @todo Should probably disable TRACE entirely until they're implemented.
  135. %method_trace(Config) ->
  136. %% Request headers.
  137. %% @todo
  138. %% Status codes.
  139. status_code_100(Config) ->
  140. doc("The 100 Continue status code can be sent. (RFC7231 6.2.1)"),
  141. ConnPid = gun_open(Config),
  142. Ref = gun:get(ConnPid, "/resp/inform2/100", [
  143. {<<"accept-encoding">>, <<"gzip">>}
  144. ]),
  145. {inform, 100, []} = gun:await(ConnPid, Ref),
  146. ok.
  147. %http10_status_code_100(Config) ->
  148. status_code_101(Config) ->
  149. doc("The 101 Switching Protocols status code can be sent. (RFC7231 6.2.2)"),
  150. ConnPid = gun_open(Config),
  151. Ref = gun:get(ConnPid, "/resp/inform2/101", [
  152. {<<"accept-encoding">>, <<"gzip">>}
  153. ]),
  154. {inform, 101, []} = gun:await(ConnPid, Ref),
  155. ok.
  156. %http10_status_code_100(Config) ->
  157. status_code_200(Config) ->
  158. doc("The 200 OK status code can be sent. (RFC7231 6.3.1)"),
  159. ConnPid = gun_open(Config),
  160. Ref = gun:get(ConnPid, "/resp/reply2/200", [
  161. {<<"accept-encoding">>, <<"gzip">>}
  162. ]),
  163. {response, _, 200, _} = gun:await(ConnPid, Ref),
  164. ok.
  165. status_code_201(Config) ->
  166. doc("The 201 Created status code can be sent. (RFC7231 6.3.2)"),
  167. ConnPid = gun_open(Config),
  168. Ref = gun:get(ConnPid, "/resp/reply2/201", [
  169. {<<"accept-encoding">>, <<"gzip">>}
  170. ]),
  171. {response, _, 201, _} = gun:await(ConnPid, Ref),
  172. ok.
  173. status_code_202(Config) ->
  174. doc("The 202 Accepted status code can be sent. (RFC7231 6.3.3)"),
  175. ConnPid = gun_open(Config),
  176. Ref = gun:get(ConnPid, "/resp/reply2/202", [
  177. {<<"accept-encoding">>, <<"gzip">>}
  178. ]),
  179. {response, _, 202, _} = gun:await(ConnPid, Ref),
  180. ok.
  181. status_code_203(Config) ->
  182. doc("The 203 Non-Authoritative Information status code can be sent. (RFC7231 6.3.4)"),
  183. ConnPid = gun_open(Config),
  184. Ref = gun:get(ConnPid, "/resp/reply2/203", [
  185. {<<"accept-encoding">>, <<"gzip">>}
  186. ]),
  187. {response, _, 203, _} = gun:await(ConnPid, Ref),
  188. ok.
  189. status_code_204(Config) ->
  190. doc("The 204 No Content status code can be sent. (RFC7231 6.3.5)"),
  191. ConnPid = gun_open(Config),
  192. Ref = gun:get(ConnPid, "/resp/reply2/204", [
  193. {<<"accept-encoding">>, <<"gzip">>}
  194. ]),
  195. {response, _, 204, _} = gun:await(ConnPid, Ref),
  196. ok.
  197. status_code_205(Config) ->
  198. doc("The 205 Reset Content status code can be sent. (RFC7231 6.3.6)"),
  199. ConnPid = gun_open(Config),
  200. Ref = gun:get(ConnPid, "/resp/reply2/205", [
  201. {<<"accept-encoding">>, <<"gzip">>}
  202. ]),
  203. {response, _, 205, _} = gun:await(ConnPid, Ref),
  204. ok.
  205. status_code_300(Config) ->
  206. doc("The 300 Multiple Choices status code can be sent. (RFC7231 6.4.1)"),
  207. ConnPid = gun_open(Config),
  208. Ref = gun:get(ConnPid, "/resp/reply2/300", [
  209. {<<"accept-encoding">>, <<"gzip">>}
  210. ]),
  211. {response, _, 300, _} = gun:await(ConnPid, Ref),
  212. ok.
  213. status_code_301(Config) ->
  214. doc("The 301 Moved Permanently status code can be sent. (RFC7231 6.4.2)"),
  215. ConnPid = gun_open(Config),
  216. Ref = gun:get(ConnPid, "/resp/reply2/301", [
  217. {<<"accept-encoding">>, <<"gzip">>}
  218. ]),
  219. {response, _, 301, _} = gun:await(ConnPid, Ref),
  220. ok.
  221. status_code_302(Config) ->
  222. doc("The 302 Found status code can be sent. (RFC7231 6.4.3)"),
  223. ConnPid = gun_open(Config),
  224. Ref = gun:get(ConnPid, "/resp/reply2/302", [
  225. {<<"accept-encoding">>, <<"gzip">>}
  226. ]),
  227. {response, _, 302, _} = gun:await(ConnPid, Ref),
  228. ok.
  229. status_code_303(Config) ->
  230. doc("The 303 See Other status code can be sent. (RFC7231 6.4.4)"),
  231. ConnPid = gun_open(Config),
  232. Ref = gun:get(ConnPid, "/resp/reply2/303", [
  233. {<<"accept-encoding">>, <<"gzip">>}
  234. ]),
  235. {response, _, 303, _} = gun:await(ConnPid, Ref),
  236. ok.
  237. status_code_305(Config) ->
  238. doc("The 305 Use Proxy status code can be sent. (RFC7231 6.4.5)"),
  239. ConnPid = gun_open(Config),
  240. Ref = gun:get(ConnPid, "/resp/reply2/305", [
  241. {<<"accept-encoding">>, <<"gzip">>}
  242. ]),
  243. {response, _, 305, _} = gun:await(ConnPid, Ref),
  244. ok.
  245. %% The status code 306 is no longer used. (RFC7231 6.4.6)
  246. status_code_307(Config) ->
  247. doc("The 307 Temporary Redirect status code can be sent. (RFC7231 6.4.7)"),
  248. ConnPid = gun_open(Config),
  249. Ref = gun:get(ConnPid, "/resp/reply2/307", [
  250. {<<"accept-encoding">>, <<"gzip">>}
  251. ]),
  252. {response, _, 307, _} = gun:await(ConnPid, Ref),
  253. ok.
  254. status_code_400(Config) ->
  255. doc("The 400 Bad Request status code can be sent. (RFC7231 6.5.1)"),
  256. ConnPid = gun_open(Config),
  257. Ref = gun:get(ConnPid, "/resp/reply2/400", [
  258. {<<"accept-encoding">>, <<"gzip">>}
  259. ]),
  260. {response, _, 400, _} = gun:await(ConnPid, Ref),
  261. ok.
  262. status_code_402(Config) ->
  263. doc("The 402 Payment Required status code can be sent. (RFC7231 6.5.2)"),
  264. ConnPid = gun_open(Config),
  265. Ref = gun:get(ConnPid, "/resp/reply2/402", [
  266. {<<"accept-encoding">>, <<"gzip">>}
  267. ]),
  268. {response, _, 402, _} = gun:await(ConnPid, Ref),
  269. ok.
  270. status_code_403(Config) ->
  271. doc("The 403 Forbidden status code can be sent. (RFC7231 6.5.3)"),
  272. ConnPid = gun_open(Config),
  273. Ref = gun:get(ConnPid, "/resp/reply2/403", [
  274. {<<"accept-encoding">>, <<"gzip">>}
  275. ]),
  276. {response, _, 403, _} = gun:await(ConnPid, Ref),
  277. ok.
  278. status_code_404(Config) ->
  279. doc("The 404 Not Found status code can be sent. (RFC7231 6.5.4)"),
  280. ConnPid = gun_open(Config),
  281. Ref = gun:get(ConnPid, "/resp/reply2/404", [
  282. {<<"accept-encoding">>, <<"gzip">>}
  283. ]),
  284. {response, _, 404, _} = gun:await(ConnPid, Ref),
  285. ok.
  286. status_code_405(Config) ->
  287. doc("The 405 Method Not Allowed status code can be sent. (RFC7231 6.5.5)"),
  288. ConnPid = gun_open(Config),
  289. Ref = gun:get(ConnPid, "/resp/reply2/405", [
  290. {<<"accept-encoding">>, <<"gzip">>}
  291. ]),
  292. {response, _, 405, _} = gun:await(ConnPid, Ref),
  293. ok.
  294. status_code_406(Config) ->
  295. doc("The 406 Not Acceptable status code can be sent. (RFC7231 6.5.6)"),
  296. ConnPid = gun_open(Config),
  297. Ref = gun:get(ConnPid, "/resp/reply2/406", [
  298. {<<"accept-encoding">>, <<"gzip">>}
  299. ]),
  300. {response, _, 406, _} = gun:await(ConnPid, Ref),
  301. ok.
  302. status_code_408(Config) ->
  303. doc("The 408 Request Timeout status code can be sent. (RFC7231 6.5.7)"),
  304. ConnPid = gun_open(Config),
  305. Ref = gun:get(ConnPid, "/resp/reply2/408", [
  306. {<<"accept-encoding">>, <<"gzip">>}
  307. ]),
  308. {response, _, 408, _} = gun:await(ConnPid, Ref),
  309. ok.
  310. status_code_409(Config) ->
  311. doc("The 409 Conflict status code can be sent. (RFC7231 6.5.8)"),
  312. ConnPid = gun_open(Config),
  313. Ref = gun:get(ConnPid, "/resp/reply2/409", [
  314. {<<"accept-encoding">>, <<"gzip">>}
  315. ]),
  316. {response, _, 409, _} = gun:await(ConnPid, Ref),
  317. ok.
  318. status_code_410(Config) ->
  319. doc("The 410 Gone status code can be sent. (RFC7231 6.5.9)"),
  320. ConnPid = gun_open(Config),
  321. Ref = gun:get(ConnPid, "/resp/reply2/410", [
  322. {<<"accept-encoding">>, <<"gzip">>}
  323. ]),
  324. {response, _, 410, _} = gun:await(ConnPid, Ref),
  325. ok.
  326. status_code_411(Config) ->
  327. doc("The 411 Length Required status code can be sent. (RFC7231 6.5.10)"),
  328. ConnPid = gun_open(Config),
  329. Ref = gun:get(ConnPid, "/resp/reply2/411", [
  330. {<<"accept-encoding">>, <<"gzip">>}
  331. ]),
  332. {response, _, 411, _} = gun:await(ConnPid, Ref),
  333. ok.
  334. status_code_413(Config) ->
  335. doc("The 413 Payload Too Large status code can be sent. (RFC7231 6.5.11)"),
  336. ConnPid = gun_open(Config),
  337. Ref = gun:get(ConnPid, "/resp/reply2/413", [
  338. {<<"accept-encoding">>, <<"gzip">>}
  339. ]),
  340. {response, _, 413, _} = gun:await(ConnPid, Ref),
  341. ok.
  342. status_code_414(Config) ->
  343. doc("The 414 URI Too Long status code can be sent. (RFC7231 6.5.12)"),
  344. ConnPid = gun_open(Config),
  345. Ref = gun:get(ConnPid, "/resp/reply2/414", [
  346. {<<"accept-encoding">>, <<"gzip">>}
  347. ]),
  348. {response, _, 414, _} = gun:await(ConnPid, Ref),
  349. ok.
  350. status_code_415(Config) ->
  351. doc("The 415 Unsupported Media Type status code can be sent. (RFC7231 6.5.13)"),
  352. ConnPid = gun_open(Config),
  353. Ref = gun:get(ConnPid, "/resp/reply2/415", [
  354. {<<"accept-encoding">>, <<"gzip">>}
  355. ]),
  356. {response, _, 415, _} = gun:await(ConnPid, Ref),
  357. ok.
  358. status_code_417(Config) ->
  359. doc("The 417 Expectation Failed status code can be sent. (RFC7231 6.5.14)"),
  360. ConnPid = gun_open(Config),
  361. Ref = gun:get(ConnPid, "/resp/reply2/417", [
  362. {<<"accept-encoding">>, <<"gzip">>}
  363. ]),
  364. {response, _, 417, _} = gun:await(ConnPid, Ref),
  365. ok.
  366. status_code_426(Config) ->
  367. doc("The 426 Upgrade Required status code can be sent. (RFC7231 6.5.15)"),
  368. ConnPid = gun_open(Config),
  369. Ref = gun:get(ConnPid, "/resp/reply2/426", [
  370. {<<"accept-encoding">>, <<"gzip">>}
  371. ]),
  372. {response, _, 426, _} = gun:await(ConnPid, Ref),
  373. ok.
  374. status_code_500(Config) ->
  375. doc("The 500 Internal Server Error status code can be sent. (RFC7231 6.6.1)"),
  376. ConnPid = gun_open(Config),
  377. Ref = gun:get(ConnPid, "/resp/reply2/500", [
  378. {<<"accept-encoding">>, <<"gzip">>}
  379. ]),
  380. {response, _, 500, _} = gun:await(ConnPid, Ref),
  381. ok.
  382. status_code_501(Config) ->
  383. doc("The 501 Not Implemented status code can be sent. (RFC7231 6.6.2)"),
  384. ConnPid = gun_open(Config),
  385. Ref = gun:get(ConnPid, "/resp/reply2/501", [
  386. {<<"accept-encoding">>, <<"gzip">>}
  387. ]),
  388. {response, _, 501, _} = gun:await(ConnPid, Ref),
  389. ok.
  390. status_code_502(Config) ->
  391. doc("The 502 Bad Gateway status code can be sent. (RFC7231 6.6.3)"),
  392. ConnPid = gun_open(Config),
  393. Ref = gun:get(ConnPid, "/resp/reply2/502", [
  394. {<<"accept-encoding">>, <<"gzip">>}
  395. ]),
  396. {response, _, 502, _} = gun:await(ConnPid, Ref),
  397. ok.
  398. status_code_503(Config) ->
  399. doc("The 503 Service Unavailable status code can be sent. (RFC7231 6.6.4)"),
  400. ConnPid = gun_open(Config),
  401. Ref = gun:get(ConnPid, "/resp/reply2/503", [
  402. {<<"accept-encoding">>, <<"gzip">>}
  403. ]),
  404. {response, _, 503, _} = gun:await(ConnPid, Ref),
  405. ok.
  406. status_code_504(Config) ->
  407. doc("The 504 Gateway Timeout status code can be sent. (RFC7231 6.6.5)"),
  408. ConnPid = gun_open(Config),
  409. Ref = gun:get(ConnPid, "/resp/reply2/504", [
  410. {<<"accept-encoding">>, <<"gzip">>}
  411. ]),
  412. {response, _, 504, _} = gun:await(ConnPid, Ref),
  413. ok.
  414. status_code_505(Config) ->
  415. doc("The 505 HTTP Version Not Supported status code can be sent. (RFC7231 6.6.6)"),
  416. ConnPid = gun_open(Config),
  417. Ref = gun:get(ConnPid, "/resp/reply2/505", [
  418. {<<"accept-encoding">>, <<"gzip">>}
  419. ]),
  420. {response, _, 505, _} = gun:await(ConnPid, Ref),
  421. ok.