http_SUITE.erl 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. _ = gun:post(ConnPid, "/echo/read_body",
  103. [{<<"content-type">>, <<"text/plain">>}]),
  104. #{socket := Socket} = gun:info(ConnPid),
  105. Pid = get_remote_pid_tcp(Socket),
  106. Ref = erlang:monitor(process, Pid),
  107. receive
  108. {'DOWN', Ref, process, Pid, Reason} ->
  109. error(Reason)
  110. after 1000 ->
  111. ok
  112. end
  113. after
  114. cowboy:stop_listener(?FUNCTION_NAME)
  115. end.
  116. request_timeout_infinity(Config) ->
  117. doc("Ensure the request_timeout option accepts the infinity value."),
  118. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  119. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  120. idle_timeout => infinity
  121. }),
  122. Port = ranch:get_port(?FUNCTION_NAME),
  123. try
  124. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  125. #{socket := Socket} = gun:info(ConnPid),
  126. Pid = get_remote_pid_tcp(Socket),
  127. Ref = erlang:monitor(process, Pid),
  128. receive
  129. {'DOWN', Ref, process, Pid, Reason} ->
  130. error(Reason)
  131. after 1000 ->
  132. ok
  133. end
  134. after
  135. cowboy:stop_listener(?FUNCTION_NAME)
  136. end.
  137. set_options_chunked_false(Config) ->
  138. doc("Confirm the option chunked can be dynamically set to disable "
  139. "chunked transfer-encoding. This results in the closing of the "
  140. "connection after the current request."),
  141. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  142. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  143. chunked => true
  144. }),
  145. Port = ranch:get_port(?FUNCTION_NAME),
  146. try
  147. Request = "GET /set_options/chunked_false HTTP/1.1\r\nhost: localhost\r\n\r\n",
  148. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  149. ok = raw_send(Client, Request),
  150. _ = case catch raw_recv_head(Client) of
  151. {'EXIT', _} -> error(closed);
  152. Data ->
  153. %% Cowboy always advertises itself as HTTP/1.1.
  154. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  155. {Headers, <<>>} = cow_http:parse_headers(Rest),
  156. false = lists:keyfind(<<"content-length">>, 1, Headers),
  157. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers)
  158. end,
  159. raw_expect_recv(Client, <<0:8000000>>),
  160. {error, closed} = raw_recv(Client, 1, 1000)
  161. after
  162. cowboy:stop_listener(?FUNCTION_NAME)
  163. end.
  164. set_options_chunked_false_ignored(Config) ->
  165. doc("Confirm the option chunked can be dynamically set to disable "
  166. "chunked transfer-encoding, and that it is ignored if the "
  167. "response is not streamed."),
  168. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  169. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  170. chunked => true
  171. }),
  172. Port = ranch:get_port(?FUNCTION_NAME),
  173. try
  174. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  175. %% We do a first request setting the option but not
  176. %% using chunked transfer-encoding in the response.
  177. StreamRef1 = gun:get(ConnPid, "/set_options/chunked_false_ignored"),
  178. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef1),
  179. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, StreamRef1),
  180. %% We then do a second request to confirm that chunked
  181. %% is not disabled for that second request.
  182. StreamRef2 = gun:get(ConnPid, "/resp/stream_reply2/200"),
  183. {response, nofin, 200, Headers} = gun:await(ConnPid, StreamRef2),
  184. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers)
  185. after
  186. cowboy:stop_listener(?FUNCTION_NAME)
  187. end.
  188. set_options_idle_timeout(Config) ->
  189. doc("Confirm that the idle_timeout option can be dynamically "
  190. "set to change how long Cowboy will wait before it closes the connection."),
  191. %% We start with a long timeout and then cut it short.
  192. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  193. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  194. idle_timeout => 60000
  195. }),
  196. Port = ranch:get_port(?FUNCTION_NAME),
  197. try
  198. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  199. _ = gun:post(ConnPid, "/set_options/idle_timeout_short",
  200. [{<<"content-type">>, <<"text/plain">>}]),
  201. #{socket := Socket} = gun:info(ConnPid),
  202. Pid = get_remote_pid_tcp(Socket),
  203. Ref = erlang:monitor(process, Pid),
  204. receive
  205. {'DOWN', Ref, process, Pid, _} ->
  206. ok
  207. after 2000 ->
  208. error(timeout)
  209. end
  210. after
  211. cowboy:stop_listener(?FUNCTION_NAME)
  212. end.
  213. set_options_idle_timeout_only_applies_to_current_request(Config) ->
  214. doc("Confirm that changes to the idle_timeout option only apply to the current stream."),
  215. %% We start with a long timeout and then cut it short.
  216. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  217. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  218. idle_timeout => 500
  219. }),
  220. Port = ranch:get_port(?FUNCTION_NAME),
  221. try
  222. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  223. StreamRef = gun:post(ConnPid, "/set_options/idle_timeout_long",
  224. [{<<"content-type">>, <<"text/plain">>}]),
  225. #{socket := Socket} = gun:info(ConnPid),
  226. Pid = get_remote_pid_tcp(Socket),
  227. Ref = erlang:monitor(process, Pid),
  228. receive
  229. {'DOWN', Ref, process, Pid, Reason} ->
  230. error(Reason)
  231. after 2000 ->
  232. ok
  233. end,
  234. %% Finish the first request and start a second one to confirm
  235. %% the idle_timeout option is back to normal.
  236. gun:data(ConnPid, StreamRef, fin, <<"Hello!">>),
  237. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
  238. {ok, <<"Hello!">>} = gun:await_body(ConnPid, StreamRef),
  239. _ = gun:post(ConnPid, "/echo/read_body",
  240. [{<<"content-type">>, <<"text/plain">>}]),
  241. receive
  242. {'DOWN', Ref, process, Pid, _} ->
  243. ok
  244. after 2000 ->
  245. error(timeout)
  246. end
  247. after
  248. cowboy:stop_listener(?FUNCTION_NAME)
  249. end.
  250. switch_protocol_flush(Config) ->
  251. doc("Confirm that switch_protocol does not flush unrelated messages."),
  252. ProtoOpts = #{
  253. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  254. stream_handlers => [switch_protocol_flush_h]
  255. },
  256. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  257. Port = ranch:get_port(?FUNCTION_NAME),
  258. try
  259. Self = self(),
  260. ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]),
  261. _ = gun:get(ConnPid, "/", [
  262. {<<"x-test-pid">>, pid_to_list(Self)}
  263. ]),
  264. receive
  265. {Self, Events} ->
  266. switch_protocol_flush_h:validate(Events)
  267. after 5000 ->
  268. error(timeout)
  269. end
  270. after
  271. cowboy:stop_listener(?FUNCTION_NAME)
  272. end.