spdy_SUITE.erl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. %% Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.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(spdy_SUITE).
  15. -compile(export_all).
  16. -import(cowboy_test, [config/2]).
  17. -import(cowboy_test, [gun_monitor_open/1]).
  18. -import(cowboy_test, [raw_open/1]).
  19. -import(cowboy_test, [raw_send/2]).
  20. %% ct.
  21. all() ->
  22. [{group, spdy}].
  23. groups() ->
  24. [{spdy, [], cowboy_test:all(?MODULE)}].
  25. init_per_suite(Config) ->
  26. case proplists:get_value(ssl_app, ssl:versions()) of
  27. Version when Version < "5.2.1" ->
  28. {skip, "No NPN support in SSL application."};
  29. _ ->
  30. Dir = config(priv_dir, Config) ++ "/static",
  31. ct_helper:create_static_dir(Dir),
  32. [{static_dir, Dir}|Config]
  33. end.
  34. end_per_suite(Config) ->
  35. ct_helper:delete_static_dir(config(static_dir, Config)).
  36. init_per_group(Name, Config) ->
  37. cowboy_test:init_spdy(Name, [
  38. {env, [{dispatch, init_dispatch(Config)}]}
  39. ], Config).
  40. end_per_group(Name, _) ->
  41. cowboy:stop_listener(Name).
  42. %% Dispatch configuration.
  43. init_dispatch(Config) ->
  44. cowboy_router:compile([
  45. {"localhost", [
  46. {"/static/[...]", cowboy_static,
  47. {dir, config(static_dir, Config)}},
  48. {"/echo/body", http_echo_body, []},
  49. {"/chunked", http_chunked, []},
  50. {"/", http_handler, []}
  51. ]}
  52. ]).
  53. %% Convenience functions.
  54. do_get(ConnPid, MRef, Host, Path) ->
  55. StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
  56. {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
  57. {IsFin, Status}.
  58. %% Tests.
  59. check_status(Config) ->
  60. Tests = [
  61. {200, nofin, "localhost", "/"},
  62. {200, nofin, "localhost", "/chunked"},
  63. {200, nofin, "localhost", "/static/style.css"},
  64. {400, fin, "bad-host", "/"},
  65. {400, fin, "localhost", "bad-path"},
  66. {404, fin, "localhost", "/this/path/does/not/exist"}
  67. ],
  68. {ConnPid, MRef} = gun_monitor_open(Config),
  69. _ = [{Status, Fin, Host, Path} = begin
  70. {IsFin, Ret} = do_get(ConnPid, MRef, Host, Path),
  71. {Ret, IsFin, Host, Path}
  72. end || {Status, Fin, Host, Path} <- Tests],
  73. gun:close(ConnPid).
  74. echo_body(Config) ->
  75. {ConnPid, MRef} = gun_monitor_open(Config),
  76. Body = << 0:800000 >>,
  77. StreamRef = gun:post(ConnPid, "/echo/body", [
  78. {<<"content-type">>, "application/octet-stream"}
  79. ], Body),
  80. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
  81. {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
  82. gun:close(ConnPid).
  83. echo_body_multi(Config) ->
  84. {ConnPid, MRef} = gun_monitor_open(Config),
  85. BodyChunk = << 0:80000 >>,
  86. StreamRef = gun:post(ConnPid, "/echo/body", [
  87. %% @todo I'm still unhappy with this. It shouldn't be required...
  88. {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
  89. {<<"content-type">>, "application/octet-stream"}
  90. ]),
  91. _ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
  92. gun:data(ConnPid, StreamRef, fin, BodyChunk),
  93. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
  94. {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
  95. gun:close(ConnPid).
  96. two_frames_one_packet(Config) ->
  97. {raw_client, Socket, Transport} = Client = raw_open([
  98. {opts, [{client_preferred_next_protocols,
  99. {client, [<<"spdy/3">>], <<"spdy/3">>}}]}
  100. |Config]),
  101. Zdef = cow_spdy:deflate_init(),
  102. Zinf = cow_spdy:inflate_init(),
  103. ok = raw_send(Client, iolist_to_binary([
  104. cow_spdy:syn_stream(Zdef, 1, 0, true, false,
  105. 0, <<"GET">>, <<"https">>, <<"localhost">>,
  106. <<"/">>, <<"HTTP/1.1">>, []),
  107. cow_spdy:syn_stream(Zdef, 3, 0, true, false,
  108. 0, <<"GET">>, <<"https">>, <<"localhost">>,
  109. <<"/">>, <<"HTTP/1.1">>, [])
  110. ])),
  111. {Frame1, Rest1} = spdy_recv(Socket, Transport, <<>>),
  112. {syn_reply, _, false, <<"200 OK">>, _, _} = cow_spdy:parse(Frame1, Zinf),
  113. {Frame2, Rest2} = spdy_recv(Socket, Transport, Rest1),
  114. {data, 1, true, _} = cow_spdy:parse(Frame2, Zinf),
  115. {Frame3, Rest3} = spdy_recv(Socket, Transport, Rest2),
  116. {syn_reply, _, false, <<"200 OK">>, _, _} = cow_spdy:parse(Frame3, Zinf),
  117. {Frame4, <<>>} = spdy_recv(Socket, Transport, Rest3),
  118. {data, 3, true, _} = cow_spdy:parse(Frame4, Zinf),
  119. ok.
  120. spdy_recv(Socket, Transport, Acc) ->
  121. {ok, Data} = Transport:recv(Socket, 0, 5000),
  122. Data2 = << Acc/binary, Data/bits >>,
  123. case cow_spdy:split(Data2) of
  124. false ->
  125. spdy_recv(Socket, Transport, Data2);
  126. {true, Frame, Rest} ->
  127. {Frame, Rest}
  128. end.