aboutsummaryrefslogtreecommitdiffstats
path: root/test/plum/server/test_http_connection.rb
blob: 3093d5bca5f318400154f2b403bc65696763c099 (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 "test_helper"

using Plum::BinaryString

class HTTPConnectionNegotiationTest < Minitest::Test
  ## with Prior Knowledge (same as over TLS)
  def test_server_must_raise_cprotocol_error_non_settings_after_magic
    io = StringIO.new
    con = HTTPServerConnection.new(io.method(:write))
    con << Connection::CLIENT_CONNECTION_PREFACE
    assert_connection_error(:protocol_error) {
      con << Frame.new(type: :window_update, stream_id: 0, payload: "".push_uint32(1)).assemble
    }
  end

  def test_server_accept_fragmented_magic
    magic = Connection::CLIENT_CONNECTION_PREFACE
    io = StringIO.new
    con = HTTPServerConnection.new(io.method(:write))
    assert_no_error {
      con << magic[0...5]
      con << magic[5..-1]
      con << Frame.new(type: :settings, stream_id: 0).assemble
    }
  end

  ## with HTTP/1.1 Upgrade
  def test_server_accept_upgrade
    io = StringIO.new
    con = HTTPServerConnection.new(io.method(:write))
    heads = nil
    con.on(:headers) {|_, _h| heads = _h.to_h }
    req = "GET / HTTP/1.1\r\n" <<
          "Host: rhe.jp\r\n" <<
          "User-Agent: nya\r\n" <<
          "Upgrade: h2c\r\n" <<
          "Connection: HTTP2-Settings, Upgrade\r\n" <<
          "HTTP2-Settings: \r\n" <<
          "\r\n"
    con << req
    assert(io.string.include?("HTTP/1.1 101 "), "Response is not HTTP/1.1 101: #{io.string}")
    assert_no_error {
      con << Connection::CLIENT_CONNECTION_PREFACE
      con << Frame.new(type: :settings, stream_id: 0).assemble
    }
    assert_equal(:half_closed_remote, con.streams[1].state)
    assert_equal({ ":method" => "GET", ":path" => "/", ":authority" => "rhe.jp", "user-agent" => "nya"}, heads)
  end

  def test_server_deny_non_upgrade
    io = StringIO.new
    con = HTTPServerConnection.new(io.method(:write))
    req = "GET / HTTP/1.1\r\n" <<
          "Host: rhe.jp\r\n" <<
          "User-Agent: nya\r\n" <<
          "Connection: close\r\n" <<
          "\r\n"
    assert_raises(LegacyHTTPError) {
      con << req
    }
  end
end