http_SUITE.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. %% Copyright (c) 2018, 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(http_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(ct_helper, [get_remote_pid_tcp/1]).
  20. -import(cowboy_test, [gun_open/1]).
  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. -import(cowboy_test, [raw_expect_recv/2]).
  26. all() -> [{group, clear}].
  27. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  28. init_routes(_) -> [
  29. {"localhost", [
  30. {"/", hello_h, []},
  31. {"/echo/:key", echo_h, []},
  32. {"/resp/:key[/:arg]", resp_h, []},
  33. {"/set_options/:key", set_options_h, []}
  34. ]}
  35. ].
  36. chunked_false(Config) ->
  37. doc("Confirm the option chunked => false disables chunked "
  38. "transfer-encoding for HTTP/1.1 connections."),
  39. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  40. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  41. chunked => false
  42. }),
  43. Port = ranch:get_port(?FUNCTION_NAME),
  44. try
  45. Request = "GET /resp/stream_reply2/200 HTTP/1.1\r\nhost: localhost\r\n\r\n",
  46. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  47. ok = raw_send(Client, Request),
  48. Rest = case catch raw_recv_head(Client) of
  49. {'EXIT', _} -> error(closed);
  50. Data ->
  51. %% Cowboy always advertises itself as HTTP/1.1.
  52. {'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data),
  53. {Headers, Rest1} = cow_http:parse_headers(Rest0),
  54. false = lists:keyfind(<<"content-length">>, 1, Headers),
  55. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  56. Rest1
  57. end,
  58. Bits = 8000000 - bit_size(Rest),
  59. raw_expect_recv(Client, <<0:Bits>>),
  60. {error, closed} = raw_recv(Client, 1, 1000)
  61. after
  62. cowboy:stop_listener(?FUNCTION_NAME)
  63. end.
  64. http10_keepalive_false(Config) ->
  65. doc("Confirm the option http10_keepalive => false disables keep-alive "
  66. "completely for HTTP/1.0 connections."),
  67. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  68. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  69. http10_keepalive => false
  70. }),
  71. Port = ranch:get_port(?FUNCTION_NAME),
  72. try
  73. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  74. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  75. ok = raw_send(Client, Keepalive),
  76. _ = case catch raw_recv_head(Client) of
  77. {'EXIT', _} -> error(closed);
  78. Data ->
  79. %% Cowboy always advertises itself as HTTP/1.1.
  80. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  81. {Headers, _} = cow_http:parse_headers(Rest),
  82. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  83. end,
  84. ok = raw_send(Client, Keepalive),
  85. case catch raw_recv_head(Client) of
  86. {'EXIT', _} -> closed;
  87. _ -> error(not_closed)
  88. end
  89. after
  90. cowboy:stop_listener(?FUNCTION_NAME)
  91. end.
  92. idle_timeout_infinity(Config) ->
  93. doc("Ensure the idle_timeout option accepts the infinity value."),
  94. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  95. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  96. request_timeout => 500,
  97. idle_timeout => infinity
  98. }),
  99. Port = ranch:get_port(?FUNCTION_NAME),
  100. try
  101. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  102. {ok, http} = gun:await_up(ConnPid),
  103. _ = gun:post(ConnPid, "/echo/read_body",
  104. [{<<"content-type">>, <<"text/plain">>}]),
  105. #{socket := Socket} = gun:info(ConnPid),
  106. Pid = get_remote_pid_tcp(Socket),
  107. Ref = erlang:monitor(process, Pid),
  108. receive
  109. {'DOWN', Ref, process, Pid, Reason} ->
  110. error(Reason)
  111. after 1000 ->
  112. ok
  113. end
  114. after
  115. cowboy:stop_listener(?FUNCTION_NAME)
  116. end.
  117. request_timeout_infinity(Config) ->
  118. doc("Ensure the request_timeout option accepts the infinity value."),
  119. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  120. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  121. request_timeout => infinity
  122. }),
  123. Port = ranch:get_port(?FUNCTION_NAME),
  124. try
  125. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  126. {ok, http} = gun:await_up(ConnPid),
  127. #{socket := Socket} = gun:info(ConnPid),
  128. Pid = get_remote_pid_tcp(Socket),
  129. Ref = erlang:monitor(process, Pid),
  130. receive
  131. {'DOWN', Ref, process, Pid, Reason} ->
  132. error(Reason)
  133. after 1000 ->
  134. ok
  135. end
  136. after
  137. cowboy:stop_listener(?FUNCTION_NAME)
  138. end.
  139. set_options_chunked_false(Config) ->
  140. doc("Confirm the option chunked can be dynamically set to disable "
  141. "chunked transfer-encoding. This results in the closing of the "
  142. "connection after the current request."),
  143. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  144. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  145. chunked => true
  146. }),
  147. Port = ranch:get_port(?FUNCTION_NAME),
  148. try
  149. Request = "GET /set_options/chunked_false HTTP/1.1\r\nhost: localhost\r\n\r\n",
  150. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  151. ok = raw_send(Client, Request),
  152. Rest = case catch raw_recv_head(Client) of
  153. {'EXIT', _} -> error(closed);
  154. Data ->
  155. %% Cowboy always advertises itself as HTTP/1.1.
  156. {'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data),
  157. {Headers, Rest1} = cow_http:parse_headers(Rest0),
  158. false = lists:keyfind(<<"content-length">>, 1, Headers),
  159. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  160. Rest1
  161. end,
  162. Bits = 8000000 - bit_size(Rest),
  163. raw_expect_recv(Client, <<0:Bits>>),
  164. {error, closed} = raw_recv(Client, 1, 1000)
  165. after
  166. cowboy:stop_listener(?FUNCTION_NAME)
  167. end.
  168. set_options_chunked_false_ignored(Config) ->
  169. doc("Confirm the option chunked can be dynamically set to disable "
  170. "chunked transfer-encoding, and that it is ignored if the "
  171. "response is not streamed."),
  172. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  173. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  174. chunked => true
  175. }),
  176. Port = ranch:get_port(?FUNCTION_NAME),
  177. try
  178. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  179. %% We do a first request setting the option but not
  180. %% using chunked transfer-encoding in the response.
  181. StreamRef1 = gun:get(ConnPid, "/set_options/chunked_false_ignored"),
  182. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef1),
  183. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, StreamRef1),
  184. %% We then do a second request to confirm that chunked
  185. %% is not disabled for that second request.
  186. StreamRef2 = gun:get(ConnPid, "/resp/stream_reply2/200"),
  187. {response, nofin, 200, Headers} = gun:await(ConnPid, StreamRef2),
  188. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers)
  189. after
  190. cowboy:stop_listener(?FUNCTION_NAME)
  191. end.
  192. set_options_idle_timeout(Config) ->
  193. doc("Confirm that the idle_timeout option can be dynamically "
  194. "set to change how long Cowboy will wait before it closes the connection."),
  195. %% We start with a long timeout and then cut it short.
  196. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  197. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  198. idle_timeout => 60000
  199. }),
  200. Port = ranch:get_port(?FUNCTION_NAME),
  201. try
  202. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  203. {ok, http} = gun:await_up(ConnPid),
  204. _ = gun:post(ConnPid, "/set_options/idle_timeout_short",
  205. [{<<"content-type">>, <<"text/plain">>}]),
  206. #{socket := Socket} = gun:info(ConnPid),
  207. Pid = get_remote_pid_tcp(Socket),
  208. Ref = erlang:monitor(process, Pid),
  209. receive
  210. {'DOWN', Ref, process, Pid, _} ->
  211. ok
  212. after 2000 ->
  213. error(timeout)
  214. end
  215. after
  216. cowboy:stop_listener(?FUNCTION_NAME)
  217. end.
  218. set_options_idle_timeout_only_applies_to_current_request(Config) ->
  219. doc("Confirm that changes to the idle_timeout option only apply to the current stream."),
  220. %% We start with a long timeout and then cut it short.
  221. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  222. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  223. idle_timeout => 500
  224. }),
  225. Port = ranch:get_port(?FUNCTION_NAME),
  226. try
  227. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  228. {ok, http} = gun:await_up(ConnPid),
  229. StreamRef = gun:post(ConnPid, "/set_options/idle_timeout_long",
  230. [{<<"content-type">>, <<"text/plain">>}]),
  231. #{socket := Socket} = gun:info(ConnPid),
  232. Pid = get_remote_pid_tcp(Socket),
  233. Ref = erlang:monitor(process, Pid),
  234. receive
  235. {'DOWN', Ref, process, Pid, Reason} ->
  236. error(Reason)
  237. after 2000 ->
  238. ok
  239. end,
  240. %% Finish the first request and start a second one to confirm
  241. %% the idle_timeout option is back to normal.
  242. gun:data(ConnPid, StreamRef, fin, <<"Hello!">>),
  243. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
  244. {ok, <<"Hello!">>} = gun:await_body(ConnPid, StreamRef),
  245. _ = gun:post(ConnPid, "/echo/read_body",
  246. [{<<"content-type">>, <<"text/plain">>}]),
  247. receive
  248. {'DOWN', Ref, process, Pid, _} ->
  249. ok
  250. after 2000 ->
  251. error(timeout)
  252. end
  253. after
  254. cowboy:stop_listener(?FUNCTION_NAME)
  255. end.
  256. switch_protocol_flush(Config) ->
  257. doc("Confirm that switch_protocol does not flush unrelated messages."),
  258. ProtoOpts = #{
  259. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  260. stream_handlers => [switch_protocol_flush_h]
  261. },
  262. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  263. Port = ranch:get_port(?FUNCTION_NAME),
  264. try
  265. Self = self(),
  266. ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]),
  267. _ = gun:get(ConnPid, "/", [
  268. {<<"x-test-pid">>, pid_to_list(Self)}
  269. ]),
  270. receive
  271. {Self, Events} ->
  272. switch_protocol_flush_h:validate(Events)
  273. after 5000 ->
  274. error(timeout)
  275. end
  276. after
  277. cowboy:stop_listener(?FUNCTION_NAME)
  278. end.