http_SUITE.erl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. all() -> [{group, clear}].
  26. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  27. init_routes(_) -> [
  28. {"localhost", [
  29. {"/", hello_h, []},
  30. {"/echo/:key", echo_h, []},
  31. {"/set_options/:key", set_options_h, []}
  32. ]}
  33. ].
  34. http10_keepalive_false(Config) ->
  35. doc("Confirm the option {http10_keepalive, false} disables keep-alive "
  36. "completely for HTTP/1.0 connections."),
  37. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  38. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  39. http10_keepalive => false
  40. }),
  41. Port = ranch:get_port(name()),
  42. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  43. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  44. ok = raw_send(Client, Keepalive),
  45. _ = case catch raw_recv_head(Client) of
  46. {'EXIT', _} -> error(closed);
  47. Data ->
  48. %% Cowboy always advertises itself as HTTP/1.1.
  49. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  50. {Headers, _} = cow_http:parse_headers(Rest),
  51. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  52. end,
  53. ok = raw_send(Client, Keepalive),
  54. case catch raw_recv_head(Client) of
  55. {'EXIT', _} -> closed;
  56. _ -> error(not_closed)
  57. end.
  58. idle_timeout_infinity(Config) ->
  59. doc("Ensure the idle_timeout option accepts the infinity value."),
  60. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  61. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  62. request_timeout => 500,
  63. idle_timeout => infinity
  64. }),
  65. Port = ranch:get_port(name()),
  66. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  67. _ = gun:post(ConnPid, "/echo/read_body",
  68. [{<<"content-type">>, <<"text/plain">>}]),
  69. #{socket := Socket} = gun:info(ConnPid),
  70. Pid = get_remote_pid_tcp(Socket),
  71. Ref = erlang:monitor(process, Pid),
  72. receive
  73. {'DOWN', Ref, process, Pid, Reason} ->
  74. error(Reason)
  75. after 1000 ->
  76. ok
  77. end.
  78. request_timeout_infinity(Config) ->
  79. doc("Ensure the request_timeout option accepts the infinity value."),
  80. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  81. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  82. idle_timeout => infinity
  83. }),
  84. Port = ranch:get_port(name()),
  85. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  86. #{socket := Socket} = gun:info(ConnPid),
  87. Pid = get_remote_pid_tcp(Socket),
  88. Ref = erlang:monitor(process, Pid),
  89. receive
  90. {'DOWN', Ref, process, Pid, Reason} ->
  91. error(Reason)
  92. after 1000 ->
  93. ok
  94. end.
  95. set_options_idle_timeout(Config) ->
  96. doc("Confirm that the idle_timeout option can be dynamically "
  97. "set to change how long Cowboy will wait before it closes the connection."),
  98. %% We start with a long timeout and then cut it short.
  99. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  100. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  101. idle_timeout => 60000
  102. }),
  103. Port = ranch:get_port(name()),
  104. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  105. _ = gun:post(ConnPid, "/set_options/idle_timeout_short",
  106. [{<<"content-type">>, <<"text/plain">>}]),
  107. #{socket := Socket} = gun:info(ConnPid),
  108. Pid = get_remote_pid_tcp(Socket),
  109. Ref = erlang:monitor(process, Pid),
  110. receive
  111. {'DOWN', Ref, process, Pid, _} ->
  112. ok
  113. after 2000 ->
  114. error(timeout)
  115. end.
  116. set_options_idle_timeout_only_applies_to_current_request(Config) ->
  117. doc("Confirm that changes to the idle_timeout option only apply to the current stream."),
  118. %% We start with a long timeout and then cut it short.
  119. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  120. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  121. idle_timeout => 500
  122. }),
  123. Port = ranch:get_port(name()),
  124. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  125. StreamRef = gun:post(ConnPid, "/set_options/idle_timeout_long",
  126. [{<<"content-type">>, <<"text/plain">>}]),
  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 2000 ->
  134. ok
  135. end,
  136. %% Finish the first request and start a second one to confirm
  137. %% the idle_timeout option is back to normal.
  138. gun:data(ConnPid, StreamRef, fin, <<"Hello!">>),
  139. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
  140. {ok, <<"Hello!">>} = gun:await_body(ConnPid, StreamRef),
  141. _ = gun:post(ConnPid, "/echo/read_body",
  142. [{<<"content-type">>, <<"text/plain">>}]),
  143. receive
  144. {'DOWN', Ref, process, Pid, _} ->
  145. ok
  146. after 2000 ->
  147. error(timeout)
  148. end.
  149. switch_protocol_flush(Config) ->
  150. doc("Confirm that switch_protocol does not flush unrelated messages."),
  151. ProtoOpts = #{
  152. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  153. stream_handlers => [switch_protocol_flush_h]
  154. },
  155. {ok, _} = cowboy:start_clear(switch_protocol_flush, [{port, 0}], ProtoOpts),
  156. Port = ranch:get_port(switch_protocol_flush),
  157. Self = self(),
  158. ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]),
  159. _ = gun:get(ConnPid, "/", [
  160. {<<"x-test-pid">>, pid_to_list(Self)}
  161. ]),
  162. receive
  163. {Self, Events} ->
  164. switch_protocol_flush_h:validate(Events)
  165. end.