http2_SUITE.erl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. %% Copyright (c) 2017, 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(http2_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. all() -> [{group, clear}].
  20. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  21. init_routes(_) -> [
  22. {"localhost", [
  23. {"/", hello_h, []}
  24. ]}
  25. ].
  26. %% Do a prior knowledge handshake (function copied from rfc7540_SUITE).
  27. do_handshake(Config) ->
  28. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  29. %% Send a valid preface.
  30. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  31. %% Receive the server preface.
  32. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  33. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  34. %% Send the SETTINGS ack.
  35. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  36. %% Receive the SETTINGS ack.
  37. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  38. {ok, Socket}.
  39. inactivity_timeout(Config) ->
  40. doc("Terminate when the inactivity timeout is reached"),
  41. Ref = inactivity_timeout_listener,
  42. ProtoOpts = #{
  43. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  44. inactivity_timeout => 1000
  45. },
  46. {ok, _} = cowboy:start_clear(Ref, [{port, 0}], ProtoOpts),
  47. Port = ranch:get_port(Ref),
  48. SocketConfig = [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config],
  49. {ok, Socket} = do_handshake(SocketConfig),
  50. receive after 1000 -> ok end,
  51. %% Receive a GOAWAY frame back with an INTERNAL_ERROR.
  52. {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000),
  53. ok.