cowboy_test.erl 5.8 KB

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