From da2e4bb8e1ff63ad49ab556207608a585f4a1676 Mon Sep 17 00:00:00 2001 From: Jim Carreer Date: Tue, 30 Jun 2015 22:56:45 -0400 Subject: [PATCH 1/2] Changed repr return value --- hpack/table.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hpack/table.py b/hpack/table.py index 0eda759..ab05087 100644 --- a/hpack/table.py +++ b/hpack/table.py @@ -117,11 +117,11 @@ def __setitem__(self, index, value): def __repr__(self): rv = "HeaderTable(" rv += str(self._maxsize) - rv += ","+str(self.resized) - rv += ",[" - for entry in self: - rv += str(entry) - return rv+"])" + rv += ", "+str(self.resized) + rv += ", [" + itm = [str(entry) for entry in self] + itm = ", ".join(itm) + return rv+itm+"])" def add(self, name, value): """ From 760e916ed978ccf048bdf474e97cccb48de3025e Mon Sep 17 00:00:00 2001 From: Jim Carreer Date: Tue, 30 Jun 2015 22:57:20 -0400 Subject: [PATCH 2/2] 100% code coverage --- test/test_table.py | 134 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 test/test_table.py diff --git a/test/test_table.py b/test/test_table.py new file mode 100644 index 0000000..16af447 --- /dev/null +++ b/test/test_table.py @@ -0,0 +1,134 @@ +from hpack.table import HeaderTable +import pytest + +class TestHeaderTable(object): + def test_getitem_dynamic_table(self): + tbl = HeaderTable() + off = len(HeaderTable.STATIC_TABLE) + val = (b'TestName', b'TestValue') + tbl.add(val[0], val[1]) + res = tbl[off + 1] + assert res == val + + def test_getitem_static_table(self): + tbl = HeaderTable() + exp = HeaderTable.STATIC_TABLE[0] + res = tbl[1] + assert res == exp + off = len(HeaderTable.STATIC_TABLE) + exp = HeaderTable.STATIC_TABLE[off - 1] + res = tbl[off] + assert res == exp + + def test_getitem_zero_index(self): + tbl = HeaderTable() + res = tbl[0] + assert res == None # TODO HPACKException will be raised instead + + def test_getitem_out_of_range(self): + tbl = HeaderTable() + off = len(HeaderTable.STATIC_TABLE) + tbl.add(b'TestName', b'TestValue') + res = tbl[off+2] + assert res == None # TODO HPACKException will be reaised instead + + def test_setitem(self): + tbl = HeaderTable() + with pytest.raises(TypeError) as einfo: + tbl[1] = (b'TestName', b'TestValue') + assert 'HeaderTable' in str(einfo.value) + + def test_repr(self): + tbl = HeaderTable() + tbl.add(b'TestName1', b'TestValue1') + tbl.add(b'TestName2', b'TestValue2') + tbl.add(b'TestName2', b'TestValue2') + exp = ("HeaderTable(4096, False, [" + + "('TestName2', 'TestValue2'), " + + "('TestName2', 'TestValue2'), " + + "('TestName1', 'TestValue1')" + + "])") + res = str(tbl) + assert res == exp + + def test_add_to_large(self): + tbl = HeaderTable() + # Max size to small to hold the value we specify + tbl.maxsize = 17 + 31 + tbl.add(b'TestName', b'TestValue') + # Table length should be 0 + assert len(tbl) == 0 + + def test_search_in_static_full(self): + tbl = HeaderTable() + itm = HeaderTable.STATIC_TABLE[0] + exp = (1, itm[0], itm[1]) + res = tbl.search(itm[0], itm[1]) + assert res == exp + + def test_search_in_static_partial(self): + tbl = HeaderTable() + itm = HeaderTable.STATIC_TABLE[0] + exp = (1, itm[0], None) + res = tbl.search(itm[0], b'NotInTable') + assert res == exp + + def test_search_in_dynamic_full(self): + tbl = HeaderTable() + idx = len(HeaderTable.STATIC_TABLE)+1 + tbl.add(b'TestName', b'TestValue') + exp = (idx , b'TestName', b'TestValue') + res = tbl.search(b'TestName', b'TestValue') + assert res == exp + + def test_search_in_dynamic_partial(self): + tbl = HeaderTable() + idx = len(HeaderTable.STATIC_TABLE)+1 + tbl.add(b'TestName', b'TestValue') + exp = (idx , b'TestName', None) + res = tbl.search(b'TestName', b'NotInTable') + assert res == exp + + def test_search_no_match(self): + tbl = HeaderTable() + idx = len(HeaderTable.STATIC_TABLE) + tbl.add(b'TestName', b'TestValue') + exp = None + res = tbl.search(b'NotInTable', b'NotInTable') + assert res == exp + + def test_maxsize_prop_getter(self): + tbl = HeaderTable() + assert tbl._maxsize == HeaderTable.DEFAULT_SIZE + + def test_maxsize_prop_setter(self): + tbl = HeaderTable() + exp = int(HeaderTable.DEFAULT_SIZE / 2) + tbl.maxsize = exp + assert tbl.resized == True + assert tbl._maxsize == exp + tbl.resized = False + tbl.maxsize = exp + assert tbl.resized == False + assert tbl._maxsize == exp + + def test_entry_size(self): + tbl = HeaderTable() + exp = 32 + len(b'TestValue') + len(b'TestName') + res = tbl._entry_size(b'TestValue', b'TestName') + assert res == exp + + def test_size(self): + tbl = HeaderTable() + exp = 3*(32 + len(b'TestValue') + len(b'TestName')) + for i in xrange(3): + tbl.add(b'TestValue', b'TestName') + res = tbl._size() + assert res == exp + + def test_shrink_maxsize_is_zero(self): + tbl = HeaderTable() + tbl.add(b'TestName',b'TestValue') + assert len(tbl) == 1 + tbl.maxsize = 0 + assert len(tbl) == 0