cowboy_test.erl 3.1 KB

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