http_SUITE.erl 9.3 KB

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