cowboy_test.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. -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. %% Common group of listeners used by most suites.
  28. common_all() ->
  29. [
  30. {group, http},
  31. {group, https},
  32. {group, http_compress},
  33. {group, https_compress}
  34. ].
  35. common_groups(Tests) ->
  36. [
  37. {http, [parallel], Tests},
  38. {https, [parallel], Tests},
  39. {http_compress, [parallel], Tests},
  40. {https_compress, [parallel], Tests}
  41. ].
  42. init_common_groups(Name = http, Config, Mod) ->
  43. init_http(Name, #{env => #{dispatch => Mod:init_dispatch(Config)}}, Config);
  44. init_common_groups(Name = https, Config, Mod) ->
  45. init_https(Name, #{env => #{dispatch => Mod:init_dispatch(Config)}}, Config);
  46. init_common_groups(Name = http_compress, Config, Mod) ->
  47. init_http(Name, #{
  48. env => #{dispatch => Mod:init_dispatch(Config)},
  49. compress => true
  50. }, Config);
  51. init_common_groups(Name = https_compress, Config, Mod) ->
  52. init_https(Name, #{
  53. env => #{dispatch => Mod:init_dispatch(Config)},
  54. compress => true
  55. }, Config).
  56. %% Support functions for testing using Gun.
  57. gun_open(Config) ->
  58. gun_open(Config, #{}).
  59. gun_open(Config, Opts) ->
  60. {ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
  61. retry => 0,
  62. transport => config(type, Config),
  63. protocols => [config(protocol, Config)]
  64. }),
  65. ConnPid.
  66. gun_down(ConnPid) ->
  67. receive {gun_down, ConnPid, _, _, _, _} -> ok
  68. after 500 -> error(timeout) end.
  69. %% Support functions for testing using a raw socket.
  70. raw_open(Config) ->
  71. Transport = case config(type, Config) of
  72. tcp -> gen_tcp;
  73. ssl -> ssl
  74. end,
  75. {_, Opts} = lists:keyfind(opts, 1, Config),
  76. {ok, Socket} = Transport:connect("localhost", config(port, Config),
  77. [binary, {active, false}, {packet, raw},
  78. {reuseaddr, true}, {nodelay, true}|Opts]),
  79. {raw_client, Socket, Transport}.
  80. raw_send({raw_client, Socket, Transport}, Data) ->
  81. Transport:send(Socket, Data).
  82. raw_recv_head({raw_client, Socket, Transport}) ->
  83. {ok, Data} = Transport:recv(Socket, 0, 10000),
  84. raw_recv_head(Socket, Transport, Data).
  85. raw_recv_head(Socket, Transport, Buffer) ->
  86. case binary:match(Buffer, <<"\r\n\r\n">>) of
  87. nomatch ->
  88. {ok, Data} = Transport:recv(Socket, 0, 10000),
  89. raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
  90. {_, _} ->
  91. Buffer
  92. end.
  93. raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
  94. Transport:recv(Socket, Length, Timeout).
  95. raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
  96. {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
  97. ok.