cowboy_test.erl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. %% Copyright (c) 2014, 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. %% Start and stop applications and their dependencies.
  17. start(Apps) ->
  18. _ = [do_start(App) || App <- Apps],
  19. ok.
  20. do_start(App) ->
  21. case application:start(App) of
  22. ok ->
  23. ok;
  24. {error, {not_started, Dep}} ->
  25. do_start(Dep),
  26. do_start(App)
  27. end.
  28. %% SSL certificate creation and safekeeping.
  29. make_certs() ->
  30. {_, Cert, Key} = ct_helper:make_certs(),
  31. CertOpts = [{cert, Cert}, {key, Key}],
  32. Pid = spawn(fun() -> receive after infinity -> ok end end),
  33. ?MODULE = ets:new(?MODULE, [ordered_set, public, named_table,
  34. {heir, Pid, undefined}]),
  35. ets:insert(?MODULE, {cert_opts, CertOpts}),
  36. ok.
  37. get_certs() ->
  38. ets:lookup_element(?MODULE, cert_opts, 2).
  39. %% Quick configuration value retrieval.
  40. config(Key, Config) ->
  41. {_, Value} = lists:keyfind(Key, 1, Config),
  42. Value.
  43. %% Test case description.
  44. doc(String) ->
  45. ct:comment(String),
  46. ct:log(String).
  47. %% List of all test cases in the suite.
  48. all(Suite) ->
  49. lists:usort([F || {F, 1} <- Suite:module_info(exports),
  50. F =/= module_info,
  51. F =/= test, %% This is leftover from the eunit parse_transform...
  52. F =/= all,
  53. F =/= groups,
  54. string:substr(atom_to_list(F), 1, 5) =/= "init_",
  55. string:substr(atom_to_list(F), 1, 4) =/= "end_",
  56. string:substr(atom_to_list(F), 1, 3) =/= "do_"
  57. ]).
  58. %% Listeners initialization.
  59. init_http(Ref, ProtoOpts, Config) ->
  60. {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
  61. {max_keepalive, 50},
  62. {timeout, 500}
  63. |ProtoOpts]),
  64. Port = ranch:get_port(Ref),
  65. [{type, tcp}, {port, Port}, {opts, []}|Config].
  66. init_https(Ref, ProtoOpts, Config) ->
  67. Opts = get_certs(),
  68. {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
  69. {max_keepalive, 50},
  70. {timeout, 500}
  71. |ProtoOpts]),
  72. Port = ranch:get_port(Ref),
  73. [{type, ssl}, {port, Port}, {opts, Opts}|Config].
  74. init_spdy(Ref, ProtoOpts, Config) ->
  75. Opts = get_certs(),
  76. {ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}],
  77. ProtoOpts),
  78. Port = ranch:get_port(Ref),
  79. [{type, ssl}, {port, Port}, {opts, Opts}|Config].
  80. %% Common group of listeners used by most suites.
  81. common_all() ->
  82. [
  83. {group, http},
  84. {group, https},
  85. {group, spdy},
  86. {group, http_compress},
  87. {group, https_compress},
  88. {group, spdy_compress}
  89. ].
  90. common_groups(Tests) ->
  91. [
  92. {http, [parallel], Tests},
  93. {https, [parallel], Tests},
  94. {spdy, [parallel], Tests},
  95. {http_compress, [parallel], Tests},
  96. {https_compress, [parallel], Tests},
  97. {spdy_compress, [parallel], Tests}
  98. ].
  99. init_common_groups(Name = http, Config, Mod) ->
  100. init_http(Name, [
  101. {env, [{dispatch, Mod:init_dispatch(Config)}]}
  102. ], Config);
  103. init_common_groups(Name = https, Config, Mod) ->
  104. init_https(Name, [
  105. {env, [{dispatch, Mod:init_dispatch(Config)}]}
  106. ], Config);
  107. init_common_groups(Name = spdy, Config, Mod) ->
  108. init_spdy(Name, [
  109. {env, [{dispatch, Mod:init_dispatch(Config)}]}
  110. ], Config);
  111. init_common_groups(Name = http_compress, Config, Mod) ->
  112. init_http(Name, [
  113. {env, [{dispatch, Mod:init_dispatch(Config)}]},
  114. {compress, true}
  115. ], Config);
  116. init_common_groups(Name = https_compress, Config, Mod) ->
  117. init_https(Name, [
  118. {env, [{dispatch, Mod:init_dispatch(Config)}]},
  119. {compress, true}
  120. ], Config);
  121. init_common_groups(Name = spdy_compress, Config, Mod) ->
  122. init_spdy(Name, [
  123. {env, [{dispatch, Mod:init_dispatch(Config)}]},
  124. {compress, true}
  125. ], Config).
  126. %% Support functions for testing using Gun.
  127. gun_open(Config) ->
  128. gun_open(Config, []).
  129. gun_open(Config, Opts) ->
  130. {ok, ConnPid} = gun:open("localhost", config(port, Config),
  131. [{retry, 0}, {type, config(type, Config)}|Opts]),
  132. ConnPid.
  133. gun_monitor_open(Config) ->
  134. gun_monitor_open(Config, []).
  135. gun_monitor_open(Config, Opts) ->
  136. ConnPid = gun_open(Config, Opts),
  137. {ConnPid, monitor(process, ConnPid)}.
  138. gun_is_gone(ConnPid, MRef) ->
  139. receive {'DOWN', MRef, process, ConnPid, gone} -> ok
  140. after 500 -> error(timeout) end.
  141. %% Support functions for testing using a raw socket.
  142. raw_open(Config) ->
  143. Transport = case config(type, Config) of
  144. tcp -> gen_tcp;
  145. ssl -> ssl
  146. end,
  147. {_, Opts} = lists:keyfind(opts, 1, Config),
  148. {ok, Socket} = Transport:connect("localhost", config(port, Config),
  149. [binary, {active, false}, {packet, raw},
  150. {reuseaddr, true}, {nodelay, true}|Opts]),
  151. {raw_client, Socket, Transport}.
  152. raw_send({raw_client, Socket, Transport}, Data) ->
  153. Transport:send(Socket, Data).
  154. raw_recv_head({raw_client, Socket, Transport}) ->
  155. {ok, Data} = Transport:recv(Socket, 0, 5000),
  156. raw_recv_head(Socket, Transport, Data).
  157. raw_recv_head(Socket, Transport, Buffer) ->
  158. case binary:match(Buffer, <<"\r\n\r\n">>) of
  159. nomatch ->
  160. {ok, Data} = Transport:recv(Socket, 0, 5000),
  161. raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
  162. {_, _} ->
  163. Buffer
  164. end.
  165. raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
  166. {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
  167. ok.