aboutsummaryrefslogtreecommitdiffstats
path: root/test/plum/hpack/test_context.rb
blob: 4a54e6cc77b975a32302ed1768ace44827664c1d (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
63
64
65
require "test_helper"

class HPACKContextTest < Minitest::Test
  def test_store
    context = new_context
    context.store("あああ", "いい")
    assert_equal([["あああ", "いい"]], context.dynamic_table)
    assert_equal("あああいい".bytesize + 32, context.size)
  end

  def test_store_eviction
    context = new_context(1)
    context.store("あああ", "いい")
    assert_equal([], context.dynamic_table)
    assert_equal(0, context.size)
  end

  def test_fetch_static
    context = new_context
    assert_equal([":method", "POST"], context.fetch(3))
  end

  def test_fetch_dynamic
    context = new_context
    context.store("あああ", "いい")
    assert_equal(["あああ", "いい"], context.fetch(62))
  end

  def test_fetch_error
    context = new_context
    context.store("あああ", "いい")
    assert_raises(Plum::HPACKError) {
      context.fetch(64)
    }
  end

  def test_search_static
    context = new_context
    i1 = context.search(":method", "POST")
    assert_equal(3, i1)
    i2 = context.search_half(":method")
    assert_equal(2, i2)
  end

  def test_search_dynamic
    context = new_context
    context.store("あああ", "abc")
    context.store("あああ", "いい")
    i1 = context.search("あああ", "abc")
    assert_equal(63, i1)
    i2 = context.search("あああ", "AAA")
    assert_equal(nil, i2)
    i3 = context.search_half("あああ")
    assert_equal(62, i3)
  end

  private
  def new_context(limit = 1 << 31)
    klass = Class.new {
      include Plum::HPACK::Context
      public *Plum::HPACK::Context.private_instance_methods
    }
    klass.new(limit)
  end
end