http_SUITE.erl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, websocket/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, websocket] ++ 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. {["websocket"], websocket_handler, []},
  69. {["headers", "dupe"], http_handler,
  70. [{headers, [{"Connection", "close"}]}]},
  71. {[], http_handler, []}
  72. ]}
  73. ].
  74. init_https_dispatch() ->
  75. init_http_dispatch().
  76. %% http.
  77. headers_dupe(Config) ->
  78. {port, Port} = lists:keyfind(port, 1, Config),
  79. {ok, Socket} = gen_tcp:connect("localhost", Port,
  80. [binary, {active, false}, {packet, raw}]),
  81. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  82. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  83. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  84. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  85. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  86. ok = gen_tcp:close(Socket).
  87. pipeline(Config) ->
  88. {port, Port} = lists:keyfind(port, 1, Config),
  89. {ok, Socket} = gen_tcp:connect("localhost", Port,
  90. [binary, {active, false}, {packet, raw}]),
  91. ok = gen_tcp:send(Socket,
  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: keep-alive\r\n\r\n"
  96. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  97. Data = pipeline_recv(Socket, <<>>),
  98. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  99. 5 = length(Reqs),
  100. pipeline_check(Reqs).
  101. pipeline_check([]) ->
  102. ok;
  103. pipeline_check([Req|Tail]) ->
  104. << "HTTP/1.1 200", _Rest/bits >> = Req,
  105. pipeline_check(Tail).
  106. pipeline_recv(Socket, SoFar) ->
  107. case gen_tcp:recv(Socket, 0, 6000) of
  108. {ok, Data} ->
  109. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  110. {error, closed} ->
  111. ok = gen_tcp:close(Socket),
  112. SoFar
  113. end.
  114. raw_req(Packet, Config) ->
  115. {port, Port} = lists:keyfind(port, 1, Config),
  116. {ok, Socket} = gen_tcp:connect("localhost", Port,
  117. [binary, {active, false}, {packet, raw}]),
  118. ok = gen_tcp:send(Socket, Packet),
  119. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>}
  120. = gen_tcp:recv(Socket, 0, 6000),
  121. gen_tcp:close(Socket),
  122. {Packet, list_to_integer(binary_to_list(Str))}.
  123. raw(Config) ->
  124. Tests = [
  125. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  126. {"\n", 400},
  127. {"Garbage\r\n\r\n", 400},
  128. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  129. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  130. {"", 408},
  131. {"\r\n", 408},
  132. {"\r\n\r\n", 408},
  133. {"GET / HTTP/1.1", 408},
  134. {"GET / HTTP/1.1\r\n", 408},
  135. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  136. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  137. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  138. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  139. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}
  140. ],
  141. [{Packet, StatusCode} = raw_req(Packet, Config)
  142. || {Packet, StatusCode} <- Tests].
  143. websocket(Config) ->
  144. {port, Port} = lists:keyfind(port, 1, Config),
  145. {ok, Socket} = gen_tcp:connect("localhost", Port,
  146. [binary, {active, false}, {packet, raw}]),
  147. ok = gen_tcp:send(Socket, [
  148. "GET /websocket HTTP/1.1\r\n"
  149. "Host: localhost\r\n"
  150. "Connection: Upgrade\r\n"
  151. "Upgrade: WebSocket\r\n"
  152. "Origin: http://localhost\r\n"
  153. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  154. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  155. "\r\n", <<15,245,8,18,2,204,133,33>>]),
  156. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  157. {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
  158. = erlang:decode_packet(http, Handshake, []),
  159. [Headers, Body] = websocket_headers(erlang:decode_packet(httph, Rest, []), []),
  160. {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  161. {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
  162. {"sec-websocket-location", "ws://localhost/websocket"}
  163. = lists:keyfind("sec-websocket-location", 1, Headers),
  164. {"sec-websocket-origin", "http://localhost"}
  165. = lists:keyfind("sec-websocket-origin", 1, Headers),
  166. <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
  167. ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>),
  168. {ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  169. {ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  170. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  171. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  172. {ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
  173. ok = gen_tcp:send(Socket, << 255, 0 >>),
  174. {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
  175. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  176. ok.
  177. websocket_headers({ok, http_eoh, Rest}, Acc) ->
  178. [Acc, Rest];
  179. websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  180. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  181. websocket_headers(erlang:decode_packet(httph, Rest, []),
  182. [{F(Key), Value}|Acc]).
  183. %% http and https.
  184. build_url(Path, Config) ->
  185. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  186. {port, Port} = lists:keyfind(port, 1, Config),
  187. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  188. http_200(Config) ->
  189. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  190. httpc:request(build_url("/", Config)).
  191. http_404(Config) ->
  192. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  193. httpc:request(build_url("/not/found", Config)).