http_SUITE.erl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ]}
  32. ].
  33. http10_keepalive_false(Config) ->
  34. doc("Confirm the option {http10_keepalive, false} disables keep-alive "
  35. "completely for HTTP/1.0 connections."),
  36. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  37. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  38. http10_keepalive => false
  39. }),
  40. Port = ranch:get_port(name()),
  41. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  42. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  43. ok = raw_send(Client, Keepalive),
  44. _ = case catch raw_recv_head(Client) of
  45. {'EXIT', _} -> error(closed);
  46. Data ->
  47. %% Cowboy always advertises itself as HTTP/1.1.
  48. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  49. {Headers, _} = cow_http:parse_headers(Rest),
  50. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  51. end,
  52. ok = raw_send(Client, Keepalive),
  53. case catch raw_recv_head(Client) of
  54. {'EXIT', _} -> closed;
  55. _ -> error(not_closed)
  56. end.
  57. idle_timeout_infinity(Config) ->
  58. doc("Ensure the idle_timeout option accepts the infinity value."),
  59. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  60. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  61. request_timeout => infinity
  62. }),
  63. Port = ranch:get_port(name()),
  64. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  65. _ = gun:post(ConnPid, "/echo/read_body", [], <<"TEST">>),
  66. #{socket := Socket} = gun:info(ConnPid),
  67. Pid = get_remote_pid_tcp(Socket),
  68. Ref = erlang:monitor(process, Pid),
  69. receive
  70. {'DOWN', Ref, process, Pid, Reason} ->
  71. error(Reason)
  72. after 1000 ->
  73. ok
  74. end.
  75. request_timeout_infinity(Config) ->
  76. doc("Ensure the request_timeout option accepts the infinity value."),
  77. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  78. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  79. idle_timeout => infinity
  80. }),
  81. Port = ranch:get_port(name()),
  82. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  83. #{socket := Socket} = gun:info(ConnPid),
  84. Pid = get_remote_pid_tcp(Socket),
  85. Ref = erlang:monitor(process, Pid),
  86. receive
  87. {'DOWN', Ref, process, Pid, Reason} ->
  88. error(Reason)
  89. after 1000 ->
  90. ok
  91. end.
  92. switch_protocol_flush(Config) ->
  93. doc("Confirm that switch_protocol does not flush unrelated messages."),
  94. ProtoOpts = #{
  95. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  96. stream_handlers => [switch_protocol_flush_h]
  97. },
  98. {ok, _} = cowboy:start_clear(switch_protocol_flush, [{port, 0}], ProtoOpts),
  99. Port = ranch:get_port(switch_protocol_flush),
  100. Self = self(),
  101. ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]),
  102. _ = gun:get(ConnPid, "/", [
  103. {<<"x-test-pid">>, pid_to_list(Self)}
  104. ]),
  105. receive
  106. {Self, Events} ->
  107. switch_protocol_flush_h:validate(Events)
  108. end.