http_SUITE.erl 2.8 KB

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