cowboy_test.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. %% Copyright (c) 2014-2017, 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(cowboy_test).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. %% Listeners initialization.
  18. init_http(Ref, ProtoOpts, Config) ->
  19. {ok, _} = cowboy:start_clear(Ref, 100, [{port, 0}], ProtoOpts),
  20. Port = ranch:get_port(Ref),
  21. [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config].
  22. init_https(Ref, ProtoOpts, Config) ->
  23. Opts = ct_helper:get_certs_from_ets(),
  24. {ok, _} = cowboy:start_tls(Ref, 100, Opts ++ [{port, 0}], ProtoOpts),
  25. Port = ranch:get_port(Ref),
  26. [{type, ssl}, {protocol, http}, {port, Port}, {opts, Opts}|Config].
  27. init_http2(Ref, ProtoOpts, Config) ->
  28. Opts = ct_helper:get_certs_from_ets(),
  29. {ok, _} = cowboy:start_tls(Ref, 100, Opts ++ [{port, 0}], ProtoOpts),
  30. Port = ranch:get_port(Ref),
  31. [{type, ssl}, {protocol, http2}, {port, Port}, {opts, Opts}|Config].
  32. %% Common group of listeners used by most suites.
  33. common_all() ->
  34. [
  35. {group, http},
  36. {group, https},
  37. {group, h2},
  38. {group, h2c},
  39. {group, http_compress},
  40. {group, https_compress},
  41. {group, h2_compress},
  42. {group, h2c_compress}
  43. ].
  44. common_groups(Tests) ->
  45. [
  46. {http, [parallel], Tests},
  47. {https, [parallel], Tests},
  48. {h2, [parallel], Tests},
  49. {h2c, [parallel], Tests},
  50. {http_compress, [parallel], Tests},
  51. {https_compress, [parallel], Tests},
  52. {h2_compress, [parallel], Tests},
  53. {h2c_compress, [parallel], Tests}
  54. ].
  55. init_common_groups(Name = http, Config, Mod) ->
  56. init_http(Name, #{
  57. env => #{dispatch => Mod:init_dispatch(Config)}
  58. }, Config);
  59. init_common_groups(Name = https, Config, Mod) ->
  60. init_https(Name, #{
  61. env => #{dispatch => Mod:init_dispatch(Config)}
  62. }, Config);
  63. init_common_groups(Name = h2, Config, Mod) ->
  64. init_http2(Name, #{
  65. env => #{dispatch => Mod:init_dispatch(Config)}
  66. }, Config);
  67. init_common_groups(Name = h2c, Config, Mod) ->
  68. Config1 = init_http(Name, #{
  69. env => #{dispatch => Mod:init_dispatch(Config)}
  70. }, Config),
  71. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  72. init_common_groups(Name = http_compress, Config, Mod) ->
  73. init_http(Name, #{
  74. env => #{dispatch => Mod:init_dispatch(Config)},
  75. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  76. }, Config);
  77. init_common_groups(Name = https_compress, Config, Mod) ->
  78. init_https(Name, #{
  79. env => #{dispatch => Mod:init_dispatch(Config)},
  80. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  81. }, Config);
  82. init_common_groups(Name = h2_compress, Config, Mod) ->
  83. init_http2(Name, #{
  84. env => #{dispatch => Mod:init_dispatch(Config)},
  85. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  86. }, Config);
  87. init_common_groups(Name = h2c_compress, Config, Mod) ->
  88. Config1 = init_http(Name, #{
  89. env => #{dispatch => Mod:init_dispatch(Config)},
  90. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  91. }, Config),
  92. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  93. %% Support functions for testing using Gun.
  94. gun_open(Config) ->
  95. gun_open(Config, #{}).
  96. gun_open(Config, Opts) ->
  97. {ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
  98. retry => 0,
  99. transport => config(type, Config),
  100. protocols => [config(protocol, Config)]
  101. }),
  102. ConnPid.
  103. gun_down(ConnPid) ->
  104. receive {gun_down, ConnPid, _, _, _, _} -> ok
  105. after 500 -> error(timeout) end.
  106. %% Support functions for testing using a raw socket.
  107. raw_open(Config) ->
  108. Transport = case config(type, Config) of
  109. tcp -> gen_tcp;
  110. ssl -> ssl
  111. end,
  112. {_, Opts} = lists:keyfind(opts, 1, Config),
  113. {ok, Socket} = Transport:connect("localhost", config(port, Config),
  114. [binary, {active, false}, {packet, raw},
  115. {reuseaddr, true}, {nodelay, true}|Opts]),
  116. {raw_client, Socket, Transport}.
  117. raw_send({raw_client, Socket, Transport}, Data) ->
  118. Transport:send(Socket, Data).
  119. raw_recv_head({raw_client, Socket, Transport}) ->
  120. {ok, Data} = Transport:recv(Socket, 0, 10000),
  121. raw_recv_head(Socket, Transport, Data).
  122. raw_recv_head(Socket, Transport, Buffer) ->
  123. case binary:match(Buffer, <<"\r\n\r\n">>) of
  124. nomatch ->
  125. {ok, Data} = Transport:recv(Socket, 0, 10000),
  126. raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
  127. {_, _} ->
  128. Buffer
  129. end.
  130. raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
  131. Transport:recv(Socket, Length, Timeout).
  132. raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
  133. {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
  134. ok.