http2_SUITE.erl 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. -import(cowboy_test, [gun_open/1]).
  20. all() -> [{group, clear}].
  21. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  22. init_routes(_) -> [
  23. {"localhost", [
  24. {"/", hello_h, []},
  25. {"/resp_iolist_body", resp_iolist_body_h, []}
  26. ]}
  27. ].
  28. %% Do a prior knowledge handshake (function copied from rfc7540_SUITE).
  29. do_handshake(Config) ->
  30. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  31. %% Send a valid preface.
  32. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  33. %% Receive the server preface.
  34. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  35. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  36. %% Send the SETTINGS ack.
  37. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  38. %% Receive the SETTINGS ack.
  39. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  40. {ok, Socket}.
  41. inactivity_timeout(Config) ->
  42. doc("Terminate when the inactivity timeout is reached"),
  43. ProtoOpts = #{
  44. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  45. inactivity_timeout => 1000
  46. },
  47. {ok, _} = cowboy:start_clear(inactivity_timeout, [{port, 0}], ProtoOpts),
  48. Port = ranch:get_port(inactivity_timeout),
  49. {ok, Socket} = do_handshake([{port, Port}|Config]),
  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.
  54. preface_timeout_infinity(Config) ->
  55. doc("Ensure infinity for preface_timeout is accepted"),
  56. ProtoOpts = #{
  57. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  58. preface_timeout => infinity
  59. },
  60. {ok, Pid} = cowboy:start_clear(preface_timeout_infinity, [{port, 0}], ProtoOpts),
  61. Ref = erlang:monitor(process, Pid),
  62. Port = ranch:get_port(preface_timeout_infinity),
  63. {ok, _} = do_handshake([{port, Port}|Config]),
  64. receive
  65. {'DOWN', Ref, process, Pid, Reason} ->
  66. error(Reason)
  67. after 1000 ->
  68. cowboy:stop_listener(preface_timeout_infinity)
  69. end.
  70. resp_iolist_body(Config) ->
  71. doc("Regression test when response bodies are iolists that "
  72. "include improper lists, empty lists and empty binaries. "
  73. "The original issue failed to split the body into frames properly."),
  74. ProtoOpts = #{
  75. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  76. },
  77. {ok, _} = cowboy:start_clear(resp_iolist_body, [{port, 0}], ProtoOpts),
  78. Port = ranch:get_port(resp_iolist_body),
  79. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  80. Ref = gun:get(ConnPid, "/resp_iolist_body"),
  81. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  82. {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  83. Len = binary_to_integer(BinLen),
  84. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  85. Len = iolist_size(RespBody),
  86. gun:close(ConnPid).