aboutsummaryrefslogtreecommitdiffstats
path: root/test/utils/server.rb
blob: 5f1baa799904caa761790cdf01af225f3aed0d56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require "timeout"

module ServerUtils
  def open_server_connection(scheme = :https)
    @_io = StringIO.new
    @_con = (scheme == :https ? ServerConnection : HTTPServerConnection).new(@_io.method(:write))
    @_con << Connection::CLIENT_CONNECTION_PREFACE
    @_con << Frame.new(type: :settings, stream_id: 0).assemble
    if block_given?
      yield @_con
    else
      @_con
    end
  end

  def open_new_stream(arg1 = nil, state: :idle, **kwargs)
    if arg1.is_a?(ServerConnection)
      con = arg1
    else
      con = open_server_connection
    end

    @_stream = con.instance_eval { stream(@max_stream_ids[1] + 2) }
    @_stream.set_state(state)
    @_stream.update_dependency(**kwargs)
    if block_given?
      yield @_stream
    else
      @_stream
    end
  end

  def sent_frames(io = nil)
    resp = (io || @_io).string.dup.force_encoding(Encoding::BINARY)
    frames = []
    while f = Frame.parse!(resp)
      frames << f
    end
    frames
  end

  def parse_frames(io, &blk)
    pos = io.string.bytesize
    blk.call
    resp = io.string.byteslice(pos, io.string.bytesize - pos).force_encoding(Encoding::BINARY)
    frames = []
    while f = Frame.parse!(resp)
      frames << f
    end
    frames
  end

  def parse_frame(io, &blk)
    frames = capture_frames(io, &blk)
    assert_equal(1, frames.size, "Supplied block sent no frames or more than 1 frame")
    frames.first
  end
end

class Minitest::Test
  include ServerUtils
end