http_SUITE.erl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. all() -> [{group, clear}].
  23. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  24. init_routes(_) -> [
  25. {"localhost", [
  26. {"/", hello_h, []},
  27. {"/echo/:key", echo_h, []}
  28. ]}
  29. ].
  30. idle_timeout_infinity(Config) ->
  31. doc("Ensure the idle_timeout option accepts the infinity value."),
  32. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  33. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  34. request_timeout => infinity
  35. }),
  36. Port = ranch:get_port(name()),
  37. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  38. _ = gun:post(ConnPid, "/echo/read_body", [], <<"TEST">>),
  39. %% @todo Gun should have a debug function to retrieve the socket.
  40. Socket = element(9, element(2, sys:get_state(ConnPid))),
  41. Pid = get_remote_pid_tcp(Socket),
  42. Ref = erlang:monitor(process, Pid),
  43. receive
  44. {'DOWN', Ref, process, Pid, Reason} ->
  45. error(Reason)
  46. after 1000 ->
  47. ok
  48. end.
  49. request_timeout_infinity(Config) ->
  50. doc("Ensure the request_timeout option accepts the infinity value."),
  51. {ok, _} = cowboy:start_clear(name(), [{port, 0}], #{
  52. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  53. idle_timeout => infinity
  54. }),
  55. Port = ranch:get_port(name()),
  56. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  57. %% @todo Gun should have a debug function to retrieve the socket.
  58. Socket = element(9, element(2, sys:get_state(ConnPid))),
  59. Pid = get_remote_pid_tcp(Socket),
  60. Ref = erlang:monitor(process, Pid),
  61. receive
  62. {'DOWN', Ref, process, Pid, Reason} ->
  63. error(Reason)
  64. after 1000 ->
  65. ok
  66. end.
  67. switch_protocol_flush(Config) ->
  68. doc("Confirm that switch_protocol does not flush unrelated messages."),
  69. ProtoOpts = #{
  70. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  71. stream_handlers => [switch_protocol_flush_h]
  72. },
  73. {ok, _} = cowboy:start_clear(switch_protocol_flush, [{port, 0}], ProtoOpts),
  74. Port = ranch:get_port(switch_protocol_flush),
  75. Self = self(),
  76. ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]),
  77. _ = gun:get(ConnPid, "/", [
  78. {<<"x-test-pid">>, pid_to_list(Self)}
  79. ]),
  80. receive
  81. {Self, Events} ->
  82. switch_protocol_flush_h:validate(Events)
  83. end.