http_SUITE.erl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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(http_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. -export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
  17. init_per_group/2, end_per_group/2]). %% ct.
  18. -export([http_200/1, http_404/1]). %% Common tests for http and https.
  19. %% ct.
  20. all() ->
  21. [{group, http}, {group, https}].
  22. groups() ->
  23. BaseTests = [http_200, http_404],
  24. [{http, [], BaseTests},
  25. {https, [], BaseTests}].
  26. init_per_suite(Config) ->
  27. application:start(inets),
  28. application:start(cowboy),
  29. Config.
  30. end_per_suite(_Config) ->
  31. application:stop(cowboy),
  32. application:stop(inets),
  33. ok.
  34. init_per_group(http, Config) ->
  35. Port = 33080,
  36. cowboy:start_listener(http, 100,
  37. cowboy_tcp_transport, [{port, Port}],
  38. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  39. ),
  40. [{scheme, "http"}, {port, Port}|Config];
  41. init_per_group(https, Config) ->
  42. Port = 33081,
  43. application:start(crypto),
  44. application:start(public_key),
  45. application:start(ssl),
  46. DataDir = ?config(data_dir, Config),
  47. cowboy:start_listener(https, 100,
  48. cowboy_ssl_transport, [
  49. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  50. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  51. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  52. ),
  53. [{scheme, "https"}, {port, Port}|Config].
  54. end_per_group(http, _Config) ->
  55. cowboy:stop_listener(http),
  56. ok;
  57. end_per_group(https, _Config) ->
  58. cowboy:stop_listener(https),
  59. application:stop(ssl),
  60. application:stop(public_key),
  61. application:stop(crypto),
  62. ok.
  63. %% Dispatch configuration.
  64. init_http_dispatch() ->
  65. [
  66. {["localhost"], [{[], http_handler, []}]}
  67. ].
  68. init_https_dispatch() ->
  69. init_http_dispatch().
  70. %% Common tests for http and https.
  71. build_url(Path, Config) ->
  72. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  73. {port, Port} = lists:keyfind(port, 1, Config),
  74. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  75. http_200(Config) ->
  76. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  77. httpc:request(build_url("/", Config)).
  78. http_404(Config) ->
  79. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  80. httpc:request(build_url("/not/found", Config)).