test_table.py (4426B)
1 # -*- coding: utf-8 -*- 2 import pytest 3 4 from hpack import InvalidTableIndex 5 from hpack.table import HeaderTable, table_entry_size 6 7 8 class TestPackageFunctions: 9 def test_table_entry_size(self): 10 res = table_entry_size(b'TestName', b'TestValue') 11 assert res == 49 12 13 14 class TestHeaderTable: 15 def test_get_by_index_dynamic_table(self): 16 tbl = HeaderTable() 17 off = len(HeaderTable.STATIC_TABLE) 18 val = (b'TestName', b'TestValue') 19 tbl.add(*val) 20 res = tbl.get_by_index(off + 1) 21 assert res == val 22 23 def test_get_by_index_static_table(self): 24 tbl = HeaderTable() 25 exp = (b':authority', b'') 26 res = tbl.get_by_index(1) 27 assert res == exp 28 idx = len(HeaderTable.STATIC_TABLE) 29 exp = (b'www-authenticate', b'') 30 res = tbl.get_by_index(idx) 31 assert res == exp 32 33 def test_get_by_index_zero_index(self): 34 tbl = HeaderTable() 35 with pytest.raises(InvalidTableIndex): 36 tbl.get_by_index(0) 37 38 def test_get_by_index_out_of_range(self): 39 tbl = HeaderTable() 40 off = len(HeaderTable.STATIC_TABLE) 41 tbl.add(b'TestName', b'TestValue') 42 with pytest.raises(InvalidTableIndex) as e: 43 tbl.get_by_index(off + 2) 44 45 assert ( 46 "Invalid table index %d" % (off + 2) in str(e.value) 47 ) 48 49 def test_repr(self): 50 tbl = HeaderTable() 51 tbl.add(b'TestName1', b'TestValue1') 52 tbl.add(b'TestName2', b'TestValue2') 53 tbl.add(b'TestName2', b'TestValue2') 54 exp = ( 55 "HeaderTable(4096, False, deque([" 56 "(b'TestName2', b'TestValue2'), " 57 "(b'TestName2', b'TestValue2'), " 58 "(b'TestName1', b'TestValue1')" 59 "]))" 60 ) 61 res = repr(tbl) 62 assert res == exp 63 64 def test_add_to_large(self): 65 tbl = HeaderTable() 66 # Max size to small to hold the value we specify 67 tbl.maxsize = 1 68 tbl.add(b'TestName', b'TestValue') 69 # Table length should be 0 70 assert len(tbl.dynamic_entries) == 0 71 72 def test_search_in_static_full(self): 73 tbl = HeaderTable() 74 itm = (b':authority', b'') 75 exp = (1, itm[0], itm[1]) 76 res = tbl.search(itm[0], itm[1]) 77 assert res == exp 78 79 def test_search_in_static_partial(self): 80 tbl = HeaderTable() 81 exp = (1, b':authority', None) 82 res = tbl.search(b':authority', b'NotInTable') 83 assert res == exp 84 85 def test_search_in_dynamic_full(self): 86 tbl = HeaderTable() 87 idx = len(HeaderTable.STATIC_TABLE) + 1 88 tbl.add(b'TestName', b'TestValue') 89 exp = (idx, b'TestName', b'TestValue') 90 res = tbl.search(b'TestName', b'TestValue') 91 assert res == exp 92 93 def test_search_in_dynamic_partial(self): 94 tbl = HeaderTable() 95 idx = len(HeaderTable.STATIC_TABLE) + 1 96 tbl.add(b'TestName', b'TestValue') 97 exp = (idx, b'TestName', None) 98 res = tbl.search(b'TestName', b'NotInTable') 99 assert res == exp 100 101 def test_search_no_match(self): 102 tbl = HeaderTable() 103 tbl.add(b'TestName', b'TestValue') 104 res = tbl.search(b'NotInTable', b'NotInTable') 105 assert res is None 106 107 def test_maxsize_prop_getter(self): 108 tbl = HeaderTable() 109 assert tbl.maxsize == HeaderTable.DEFAULT_SIZE 110 111 def test_maxsize_prop_setter(self): 112 tbl = HeaderTable() 113 exp = int(HeaderTable.DEFAULT_SIZE / 2) 114 tbl.maxsize = exp 115 assert tbl.resized is True 116 assert tbl.maxsize == exp 117 tbl.resized = False 118 tbl.maxsize = exp 119 assert tbl.resized is False 120 assert tbl.maxsize == exp 121 122 def test_size(self): 123 tbl = HeaderTable() 124 for i in range(3): 125 tbl.add(b'TestName', b'TestValue') 126 res = tbl._current_size 127 assert res == 147 128 129 def test_shrink_maxsize_is_zero(self): 130 tbl = HeaderTable() 131 tbl.add(b'TestName', b'TestValue') 132 assert len(tbl.dynamic_entries) == 1 133 tbl.maxsize = 0 134 assert len(tbl.dynamic_entries) == 0 135 136 def test_shrink_maxsize(self): 137 tbl = HeaderTable() 138 for i in range(3): 139 tbl.add(b'TestName', b'TestValue') 140 141 assert tbl._current_size == 147 142 tbl.maxsize = 146 143 assert len(tbl.dynamic_entries) == 2 144 assert tbl._current_size == 98