http2_SUITE.erl 2.3 KB

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