|
| 1 | +from hpack.table import HeaderTable |
| 2 | +import pytest |
| 3 | + |
| 4 | +class TestHeaderTable(object): |
| 5 | + def test_getitem_dynamic_table(self): |
| 6 | + tbl = HeaderTable() |
| 7 | + off = len(HeaderTable.STATIC_TABLE) |
| 8 | + val = (b'TestName', b'TestValue') |
| 9 | + tbl.add(val[0], val[1]) |
| 10 | + res = tbl[off + 1] |
| 11 | + assert res == val |
| 12 | + |
| 13 | + def test_getitem_static_table(self): |
| 14 | + tbl = HeaderTable() |
| 15 | + exp = HeaderTable.STATIC_TABLE[0] |
| 16 | + res = tbl[1] |
| 17 | + assert res == exp |
| 18 | + off = len(HeaderTable.STATIC_TABLE) |
| 19 | + exp = HeaderTable.STATIC_TABLE[off - 1] |
| 20 | + res = tbl[off] |
| 21 | + assert res == exp |
| 22 | + |
| 23 | + def test_getitem_zero_index(self): |
| 24 | + tbl = HeaderTable() |
| 25 | + res = tbl[0] |
| 26 | + assert res == None # TODO HPACKException will be raised instead |
| 27 | + |
| 28 | + def test_getitem_out_of_range(self): |
| 29 | + tbl = HeaderTable() |
| 30 | + off = len(HeaderTable.STATIC_TABLE) |
| 31 | + tbl.add(b'TestName', b'TestValue') |
| 32 | + res = tbl[off+2] |
| 33 | + assert res == None # TODO HPACKException will be reaised instead |
| 34 | + |
| 35 | + def test_setitem(self): |
| 36 | + tbl = HeaderTable() |
| 37 | + with pytest.raises(TypeError) as einfo: |
| 38 | + tbl[1] = (b'TestName', b'TestValue') |
| 39 | + assert 'HeaderTable' in str(einfo.value) |
| 40 | + |
| 41 | + def test_repr(self): |
| 42 | + tbl = HeaderTable() |
| 43 | + tbl.add(b'TestName1', b'TestValue1') |
| 44 | + tbl.add(b'TestName2', b'TestValue2') |
| 45 | + tbl.add(b'TestName2', b'TestValue2') |
| 46 | + exp = ("HeaderTable(4096, False, [" + |
| 47 | + "('TestName2', 'TestValue2'), " + |
| 48 | + "('TestName2', 'TestValue2'), " + |
| 49 | + "('TestName1', 'TestValue1')" + |
| 50 | + "])") |
| 51 | + res = str(tbl) |
| 52 | + assert res == exp |
| 53 | + |
| 54 | + def test_add_to_large(self): |
| 55 | + tbl = HeaderTable() |
| 56 | + # Max size to small to hold the value we specify |
| 57 | + tbl.maxsize = 17 + 31 |
| 58 | + tbl.add(b'TestName', b'TestValue') |
| 59 | + # Table length should be 0 |
| 60 | + assert len(tbl) == 0 |
| 61 | + |
| 62 | + def test_search_in_static_full(self): |
| 63 | + tbl = HeaderTable() |
| 64 | + itm = HeaderTable.STATIC_TABLE[0] |
| 65 | + exp = (1, itm[0], itm[1]) |
| 66 | + res = tbl.search(itm[0], itm[1]) |
| 67 | + assert res == exp |
| 68 | + |
| 69 | + def test_search_in_static_partial(self): |
| 70 | + tbl = HeaderTable() |
| 71 | + itm = HeaderTable.STATIC_TABLE[0] |
| 72 | + exp = (1, itm[0], None) |
| 73 | + res = tbl.search(itm[0], b'NotInTable') |
| 74 | + assert res == exp |
| 75 | + |
| 76 | + def test_search_in_dynamic_full(self): |
| 77 | + tbl = HeaderTable() |
| 78 | + idx = len(HeaderTable.STATIC_TABLE)+1 |
| 79 | + tbl.add(b'TestName', b'TestValue') |
| 80 | + exp = (idx , b'TestName', b'TestValue') |
| 81 | + res = tbl.search(b'TestName', b'TestValue') |
| 82 | + assert res == exp |
| 83 | + |
| 84 | + def test_search_in_dynamic_partial(self): |
| 85 | + tbl = HeaderTable() |
| 86 | + idx = len(HeaderTable.STATIC_TABLE)+1 |
| 87 | + tbl.add(b'TestName', b'TestValue') |
| 88 | + exp = (idx , b'TestName', None) |
| 89 | + res = tbl.search(b'TestName', b'NotInTable') |
| 90 | + assert res == exp |
| 91 | + |
| 92 | + def test_search_no_match(self): |
| 93 | + tbl = HeaderTable() |
| 94 | + idx = len(HeaderTable.STATIC_TABLE) |
| 95 | + tbl.add(b'TestName', b'TestValue') |
| 96 | + exp = None |
| 97 | + res = tbl.search(b'NotInTable', b'NotInTable') |
| 98 | + assert res == exp |
| 99 | + |
| 100 | + def test_maxsize_prop_getter(self): |
| 101 | + tbl = HeaderTable() |
| 102 | + assert tbl._maxsize == HeaderTable.DEFAULT_SIZE |
| 103 | + |
| 104 | + def test_maxsize_prop_setter(self): |
| 105 | + tbl = HeaderTable() |
| 106 | + exp = int(HeaderTable.DEFAULT_SIZE / 2) |
| 107 | + tbl.maxsize = exp |
| 108 | + assert tbl.resized == True |
| 109 | + assert tbl._maxsize == exp |
| 110 | + tbl.resized = False |
| 111 | + tbl.maxsize = exp |
| 112 | + assert tbl.resized == False |
| 113 | + assert tbl._maxsize == exp |
| 114 | + |
| 115 | + def test_entry_size(self): |
| 116 | + tbl = HeaderTable() |
| 117 | + exp = 32 + len(b'TestValue') + len(b'TestName') |
| 118 | + res = tbl._entry_size(b'TestValue', b'TestName') |
| 119 | + assert res == exp |
| 120 | + |
| 121 | + def test_size(self): |
| 122 | + tbl = HeaderTable() |
| 123 | + exp = 3*(32 + len(b'TestValue') + len(b'TestName')) |
| 124 | + for i in xrange(3): |
| 125 | + tbl.add(b'TestValue', b'TestName') |
| 126 | + res = tbl._size() |
| 127 | + assert res == exp |
| 128 | + |
| 129 | + def test_shrink_maxsize_is_zero(self): |
| 130 | + tbl = HeaderTable() |
| 131 | + tbl.add(b'TestName',b'TestValue') |
| 132 | + assert len(tbl) == 1 |
| 133 | + tbl.maxsize = 0 |
| 134 | + assert len(tbl) == 0 |
0 commit comments