http_SUITE.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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([pipeline/1, 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, [], [pipeline, 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. pipeline(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,
  77. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  78. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  79. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  80. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  81. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  82. Data = pipeline_recv(Socket, <<>>),
  83. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  84. 5 = length(Reqs),
  85. pipeline_check(Reqs).
  86. pipeline_check([]) ->
  87. ok;
  88. pipeline_check([Req|Tail]) ->
  89. << "HTTP/1.1 200", _Rest/bits >> = Req,
  90. pipeline_check(Tail).
  91. pipeline_recv(Socket, SoFar) ->
  92. case gen_tcp:recv(Socket, 0, 6000) of
  93. {ok, Data} ->
  94. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  95. {error, closed} ->
  96. ok = gen_tcp:close(Socket),
  97. SoFar
  98. end.
  99. raw_req(Packet, Config) ->
  100. {port, Port} = lists:keyfind(port, 1, Config),
  101. {ok, Socket} = gen_tcp:connect("localhost", Port,
  102. [binary, {active, false}, {packet, raw}]),
  103. ok = gen_tcp:send(Socket, Packet),
  104. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>}
  105. = gen_tcp:recv(Socket, 0, 6000),
  106. gen_tcp:close(Socket),
  107. {Packet, list_to_integer(binary_to_list(Str))}.
  108. raw(Config) ->
  109. Tests = [
  110. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  111. {"\n", 400},
  112. {"Garbage\r\n\r\n", 400},
  113. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  114. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  115. {"", 408},
  116. {"\r\n", 408},
  117. {"\r\n\r\n", 408},
  118. {"GET / HTTP/1.1", 408},
  119. {"GET / HTTP/1.1\r\n", 408},
  120. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  121. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  122. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  123. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  124. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}
  125. ],
  126. [{Packet, StatusCode} = raw_req(Packet, Config)
  127. || {Packet, StatusCode} <- Tests].
  128. %% http and https.
  129. build_url(Path, Config) ->
  130. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  131. {port, Port} = lists:keyfind(port, 1, Config),
  132. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  133. http_200(Config) ->
  134. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  135. httpc:request(build_url("/", Config)).
  136. http_404(Config) ->
  137. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  138. httpc:request(build_url("/not/found", Config)).