diff --git a/src/hpack/__init__.py b/src/hpack/__init__.py index e03de7a..33bd903 100644 --- a/src/hpack/__init__.py +++ b/src/hpack/__init__.py @@ -8,13 +8,23 @@ from .hpack import Encoder, Decoder from .struct import HeaderTuple, NeverIndexedHeaderTuple from .exceptions import ( - HPACKError, HPACKDecodingError, InvalidTableIndex, OversizedHeaderListError, InvalidTableSizeError + HPACKError, + HPACKDecodingError, + InvalidTableIndex, + OversizedHeaderListError, + InvalidTableSizeError ) __all__ = [ - 'Encoder', 'Decoder', 'HPACKError', 'HPACKDecodingError', - 'InvalidTableIndex', 'HeaderTuple', 'NeverIndexedHeaderTuple', - 'OversizedHeaderListError', 'InvalidTableSizeError' + 'Encoder', + 'Decoder', + 'HeaderTuple', + 'NeverIndexedHeaderTuple', + 'HPACKError', + 'HPACKDecodingError', + 'InvalidTableIndex', + 'OversizedHeaderListError', + 'InvalidTableSizeError', ] __version__ = '4.0.0+dev' diff --git a/src/hpack/hpack.py b/src/hpack/hpack.py index f17a583..4d958cb 100644 --- a/src/hpack/hpack.py +++ b/src/hpack/hpack.py @@ -154,7 +154,7 @@ def _to_bytes(string): return string if isinstance(string, bytes) else string.encode('utf-8') -class Encoder(object): +class Encoder: """ An HPACK encoder object. This object takes HTTP headers and emits encoded HTTP/2 header blocks. @@ -371,7 +371,7 @@ def _encode_table_size_change(self): return block -class Decoder(object): +class Decoder: """ An HPACK decoder object. diff --git a/src/hpack/huffman.py b/src/hpack/huffman.py index 159569c..5ec99ab 100644 --- a/src/hpack/huffman.py +++ b/src/hpack/huffman.py @@ -9,7 +9,7 @@ from .compat import to_byte, decode_hex -class HuffmanEncoder(object): +class HuffmanEncoder: """ Encodes a string according to the Huffman encoding table defined in the HPACK specification. diff --git a/src/hpack/struct.py b/src/hpack/struct.py index e860cd7..fcab929 100644 --- a/src/hpack/struct.py +++ b/src/hpack/struct.py @@ -25,8 +25,8 @@ class HeaderTuple(tuple): indexable = True - def __new__(_cls, *args): - return tuple.__new__(_cls, args) + def __new__(cls, *args): + return tuple.__new__(cls, args) class NeverIndexedHeaderTuple(HeaderTuple): diff --git a/src/hpack/table.py b/src/hpack/table.py index 08eff98..2b656f3 100644 --- a/src/hpack/table.py +++ b/src/hpack/table.py @@ -23,7 +23,7 @@ def table_entry_size(name, value): return 32 + len(name) + len(value) -class HeaderTable(object): +class HeaderTable: """ Implements the combined static and dynamic header table diff --git a/test/test_encode_decode.py b/test/test_encode_decode.py index 94820f2..6546fdd 100644 --- a/test/test_encode_decode.py +++ b/test/test_encode_decode.py @@ -7,11 +7,11 @@ from hypothesis import given from hypothesis.strategies import integers, binary, one_of +from hpack import HPACKDecodingError from hpack.hpack import encode_integer, decode_integer -from hpack.exceptions import HPACKDecodingError -class TestIntegerEncoding(object): +class TestIntegerEncoding: # These tests are stolen from the HPACK spec. def test_encoding_10_with_5_bit_prefix(self): val = encode_integer(10, 5) @@ -29,7 +29,7 @@ def test_encoding_42_with_8_bit_prefix(self): assert val == bytearray(b'\x2a') -class TestIntegerDecoding(object): +class TestIntegerDecoding: # These tests are stolen from the HPACK spec. def test_decoding_10_with_5_bit_prefix(self): val = decode_integer(b'\x0a', 5) @@ -52,7 +52,7 @@ def test_decode_insufficient_data_fails(self): decode_integer(b'\x1f', 5) -class TestEncodingProperties(object): +class TestEncodingProperties: """ Property-based tests for our integer encoder and decoder. """ diff --git a/test/test_hpack.py b/test/test_hpack.py index c3333b4..5eebcb4 100644 --- a/test/test_hpack.py +++ b/test/test_hpack.py @@ -1,23 +1,29 @@ # -*- coding: utf-8 -*- -from hpack.hpack import Encoder, Decoder, _dict_to_iterable, _to_bytes -from hpack.exceptions import ( - HPACKDecodingError, InvalidTableIndex, OversizedHeaderListError, - InvalidTableSizeError -) -from hpack.struct import HeaderTuple, NeverIndexedHeaderTuple import itertools import pytest from hypothesis import given from hypothesis.strategies import text, binary, sets, one_of +from hpack import ( + Encoder, + Decoder, + HeaderTuple, + NeverIndexedHeaderTuple, + HPACKDecodingError, + InvalidTableIndex, + OversizedHeaderListError, + InvalidTableSizeError, +) +from hpack.hpack import _dict_to_iterable, _to_bytes + try: unicode = unicode except NameError: unicode = str -class TestHPACKEncoder(object): +class TestHPACKEncoder: # These tests are stolen entirely from the IETF specification examples. def test_literal_header_field_with_indexing(self): """ @@ -338,7 +344,7 @@ def test_evicting_header_table_objects(self): assert len(e.header_table.dynamic_entries) == 1 -class TestHPACKDecoder(object): +class TestHPACKDecoder: # These tests are stolen entirely from the IETF specification examples. def test_literal_header_field_with_indexing(self): """ @@ -737,7 +743,7 @@ def test_truncated_header_value(self): d.decode(data) -class TestDictToIterable(object): +class TestDictToIterable: """ The dict_to_iterable function has some subtle requirements: validates that everything behaves as expected. diff --git a/test/test_hpack_integration.py b/test/test_hpack_integration.py index 8b8de65..5dac990 100644 --- a/test/test_hpack_integration.py +++ b/test/test_hpack_integration.py @@ -4,13 +4,13 @@ long time to run, so they're outside the main test suite, but they need to be run before every change to HPACK. """ -from hpack.hpack import Decoder, Encoder -from hpack.struct import HeaderTuple from binascii import unhexlify from pytest import skip +from hpack import Decoder, Encoder, HeaderTuple -class TestHPACKDecoderIntegration(object): + +class TestHPACKDecoderIntegration: def test_can_decode_a_story(self, story): d = Decoder() diff --git a/test/test_huffman.py b/test/test_huffman.py index 1b8c2f1..f2c3f59 100644 --- a/test/test_huffman.py +++ b/test/test_huffman.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- -from hpack.exceptions import HPACKDecodingError -from hpack.huffman_table import decode_huffman +from hpack import HPACKDecodingError from hpack.huffman import HuffmanEncoder from hpack.huffman_constants import REQUEST_CODES, REQUEST_CODES_LENGTH +from hpack.huffman_table import decode_huffman from hypothesis import given, example from hypothesis.strategies import binary -class TestHuffman(object): +class TestHuffman: def test_request_huffman_decoder(self): assert ( @@ -34,7 +34,7 @@ def test_request_huffman_encode(self): ) -class TestHuffmanDecoder(object): +class TestHuffmanDecoder: @given(data=binary()) @example(b'\xff') @example(b'\x5f\xff\xff\xff\xff') diff --git a/test/test_struct.py b/test/test_struct.py index 613b8c6..a27d2ad 100644 --- a/test/test_struct.py +++ b/test/test_struct.py @@ -7,10 +7,10 @@ """ import pytest -from hpack.struct import HeaderTuple, NeverIndexedHeaderTuple +from hpack import HeaderTuple, NeverIndexedHeaderTuple -class TestHeaderTuple(object): +class TestHeaderTuple: def test_is_tuple(self): """ HeaderTuple objects are tuples. diff --git a/test/test_table.py b/test/test_table.py index ca4729b..1e2ad27 100644 --- a/test/test_table.py +++ b/test/test_table.py @@ -1,20 +1,17 @@ # -*- coding: utf-8 -*- -from hpack.table import HeaderTable, table_entry_size -from hpack.exceptions import InvalidTableIndex import pytest -import sys -_ver = sys.version_info -is_py2 = _ver[0] == 2 -is_py3 = _ver[0] == 3 + +from hpack import InvalidTableIndex +from hpack.table import HeaderTable, table_entry_size -class TestPackageFunctions(object): +class TestPackageFunctions: def test_table_entry_size(self): res = table_entry_size(b'TestName', b'TestValue') assert res == 49 -class TestHeaderTable(object): +class TestHeaderTable: def test_get_by_index_dynamic_table(self): tbl = HeaderTable() off = len(HeaderTable.STATIC_TABLE) @@ -54,24 +51,13 @@ def test_repr(self): tbl.add(b'TestName1', b'TestValue1') tbl.add(b'TestName2', b'TestValue2') tbl.add(b'TestName2', b'TestValue2') - # Meh, I hate that I have to do this to test - # repr - if is_py3: - exp = ( - "HeaderTable(4096, False, deque([" - "(b'TestName2', b'TestValue2'), " - "(b'TestName2', b'TestValue2'), " - "(b'TestName1', b'TestValue1')" - "]))" - ) - else: - exp = ( - "HeaderTable(4096, False, deque([" - "('TestName2', 'TestValue2'), " - "('TestName2', 'TestValue2'), " - "('TestName1', 'TestValue1')" - "]))" - ) + exp = ( + "HeaderTable(4096, False, deque([" + "(b'TestName2', b'TestValue2'), " + "(b'TestName2', b'TestValue2'), " + "(b'TestName1', b'TestValue1')" + "]))" + ) res = repr(tbl) assert res == exp