cowboy_test.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. %% Quick configuration value retrieval.
  29. config(Key, Config) ->
  30. {_, Value} = lists:keyfind(Key, 1, Config),
  31. Value.
  32. %% List of all test cases in the suite.
  33. all(Suite) ->
  34. lists:usort([F || {F, 1} <- Suite:module_info(exports),
  35. F =/= module_info,
  36. F =/= test, %% This is leftover from the eunit parse_transform...
  37. F =/= all,
  38. F =/= groups,
  39. string:substr(atom_to_list(F), 1, 5) =/= "init_",
  40. string:substr(atom_to_list(F), 1, 4) =/= "end_",
  41. string:substr(atom_to_list(F), 1, 3) =/= "do_"
  42. ]).
  43. %% Support functions for testing using Gun.
  44. gun_open(Config) ->
  45. gun_open(Config, []).
  46. gun_open(Config, Opts) ->
  47. {ok, ConnPid} = gun:open("localhost", config(port, Config),
  48. [{retry, 0}, {type, config(type, Config)}|Opts]),
  49. ConnPid.
  50. gun_monitor_open(Config) ->
  51. gun_monitor_open(Config, []).
  52. gun_monitor_open(Config, Opts) ->
  53. ConnPid = gun_open(Config, Opts),
  54. {ConnPid, monitor(process, ConnPid)}.
  55. gun_is_gone(ConnPid, MRef) ->
  56. receive {'DOWN', MRef, process, ConnPid, gone} -> ok
  57. after 500 -> error(timeout) end.
  58. %% Support functions for testing using a raw socket.
  59. raw_open(Config) ->
  60. Transport = case config(type, Config) of
  61. tcp -> gen_tcp;
  62. ssl -> ssl
  63. end,
  64. {_, Opts} = lists:keyfind(opts, 1, Config),
  65. {ok, Socket} = Transport:connect("localhost", config(port, Config),
  66. [binary, {active, false}, {packet, raw},
  67. {reuseaddr, true}, {nodelay, true}|Opts]),
  68. {raw_client, Socket, Transport}.
  69. raw_send({raw_client, Socket, Transport}, Data) ->
  70. Transport:send(Socket, Data).
  71. raw_recv_head({raw_client, Socket, Transport}) ->
  72. {ok, Data} = Transport:recv(Socket, 0, 5000),
  73. raw_recv_head(Socket, Transport, Data).
  74. raw_recv_head(Socket, Transport, Buffer) ->
  75. case binary:match(Buffer, <<"\r\n\r\n">>) of
  76. nomatch ->
  77. {ok, Data} = Transport:recv(Socket, 0, 5000),
  78. raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
  79. {_, _} ->
  80. Buffer
  81. end.
  82. raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
  83. {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
  84. ok.