http_SUITE.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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([raw/1]). %% http.
  19. -export([http_200/1, http_404/1]). %% http and https.
  20. %% ct.
  21. all() ->
  22. [{group, http}, {group, https}].
  23. groups() ->
  24. BaseTests = [http_200, http_404],
  25. [{http, [], [raw] ++ BaseTests},
  26. {https, [], BaseTests}].
  27. init_per_suite(Config) ->
  28. application:start(inets),
  29. application:start(cowboy),
  30. Config.
  31. end_per_suite(_Config) ->
  32. application:stop(cowboy),
  33. application:stop(inets),
  34. ok.
  35. init_per_group(http, Config) ->
  36. Port = 33080,
  37. cowboy:start_listener(http, 100,
  38. cowboy_tcp_transport, [{port, Port}],
  39. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  40. ),
  41. [{scheme, "http"}, {port, Port}|Config];
  42. init_per_group(https, Config) ->
  43. Port = 33081,
  44. application:start(crypto),
  45. application:start(public_key),
  46. application:start(ssl),
  47. DataDir = ?config(data_dir, Config),
  48. cowboy:start_listener(https, 100,
  49. cowboy_ssl_transport, [
  50. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  51. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  52. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  53. ),
  54. [{scheme, "https"}, {port, Port}|Config].
  55. end_per_group(http, _Config) ->
  56. cowboy:stop_listener(http),
  57. ok;
  58. end_per_group(https, _Config) ->
  59. cowboy:stop_listener(https),
  60. application:stop(ssl),
  61. application:stop(public_key),
  62. application:stop(crypto),
  63. ok.
  64. %% Dispatch configuration.
  65. init_http_dispatch() ->
  66. [
  67. {["localhost"], [{[], http_handler, []}]}
  68. ].
  69. init_https_dispatch() ->
  70. init_http_dispatch().
  71. %% http.
  72. raw_req(Packet, Config) ->
  73. {port, Port} = lists:keyfind(port, 1, Config),
  74. {ok, Socket} = gen_tcp:connect("localhost", Port,
  75. [binary, {active, false}, {packet, raw}]),
  76. ok = gen_tcp:send(Socket, Packet),
  77. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>}
  78. = gen_tcp:recv(Socket, 0, 6000),
  79. gen_tcp:close(Socket),
  80. {Packet, list_to_integer(binary_to_list(Str))}.
  81. raw(Config) ->
  82. Tests = [
  83. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  84. {"Garbage\r\n\r\n", 400},
  85. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  86. {"", 408},
  87. {"\r\n", 408},
  88. {"\r\n\r\n", 408},
  89. {"GET / HTTP/1.1", 408},
  90. {"GET / HTTP/1.1\r\n", 408},
  91. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  92. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  93. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  94. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  95. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}
  96. ],
  97. [{Packet, StatusCode} = raw_req(Packet, Config)
  98. || {Packet, StatusCode} <- Tests].
  99. %% http and https.
  100. build_url(Path, Config) ->
  101. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  102. {port, Port} = lists:keyfind(port, 1, Config),
  103. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  104. http_200(Config) ->
  105. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  106. httpc:request(build_url("/", Config)).
  107. http_404(Config) ->
  108. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  109. httpc:request(build_url("/not/found", Config)).