http_SUITE.erl 5.5 KB

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