Skip to content

Commit

Permalink
tests: linting++
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed Jan 22, 2025
1 parent 9bfb877 commit 0b98b24
Show file tree
Hide file tree
Showing 29 changed files with 1,701 additions and 1,712 deletions.
2 changes: 1 addition & 1 deletion src/h2/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RequestReceived(Event):
The RequestReceived event is fired whenever all of a request's headers
are received. This event carries the HTTP headers for the given request
and the stream ID of the new stream.
In HTTP/2, headers may be sent as a HEADERS frame followed by zero or more
CONTINUATION frames with the final frame setting the END_HEADERS flag.
This event is fired after the entire sequence is received.
Expand Down
4 changes: 3 additions & 1 deletion src/h2/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
from string import whitespace
from typing import TYPE_CHECKING, Any, NamedTuple

from hpack.struct import Header, HeaderTuple, HeaderWeaklyTyped, NeverIndexedHeaderTuple
from hpack.struct import HeaderTuple, NeverIndexedHeaderTuple

from .exceptions import FlowControlError, ProtocolError

if TYPE_CHECKING: # pragma: no cover
from collections.abc import Generator, Iterable

from hpack.struct import Header, HeaderWeaklyTyped

UPPER_RE = re.compile(b"[A-Z]")
SIGIL = ord(b":")
INFORMATIONAL_START = ord(b"1")
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from . import helpers
Expand Down
14 changes: 7 additions & 7 deletions tests/coroutine_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""
coroutine_tests
~~~~~~~~~~~~~~~
This file gives access to a coroutine-based test class. This allows each test
case to be defined as a pair of interacting coroutines, sending data to each
other by yielding the flow of control.
Expand All @@ -12,13 +9,15 @@
makes them behave identically on all platforms, as well as ensuring they both
succeed and fail quickly.
"""
import itertools
from __future__ import annotations

import functools
import itertools

import pytest


class CoroutineTestCase(object):
class CoroutineTestCase:
"""
A base class for tests that use interacting coroutines.
Expand All @@ -28,7 +27,8 @@ class CoroutineTestCase(object):
its first action is to receive data), the calling code should prime it by
using the 'server' decorator on this class.
"""
def run_until_complete(self, *coroutines):

def run_until_complete(self, *coroutines) -> None:
"""
Executes a set of coroutines that communicate between each other. Each
one is, in order, passed the output of the previous coroutine until
Expand All @@ -55,7 +55,7 @@ def run_until_complete(self, *coroutines):
except StopIteration:
continue
else:
pytest.fail("Coroutine %s not exhausted" % coro)
pytest.fail(f"Coroutine {coro} not exhausted")

def server(self, func):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/h2spectest.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash

# A test script that runs the example Python Twisted server and then runs
# h2spec against it. Prints the output of h2spec. This script does not expect
# to be run directly, but instead via `tox -e h2spec`.
Expand Down
59 changes: 36 additions & 23 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""
helpers
~~~~~~~
This module contains helpers for the h2 tests.
Helper module for the h2 tests.
"""
from __future__ import annotations

from hpack.hpack import Encoder
from hyperframe.frame import (
HeadersFrame, DataFrame, SettingsFrame, WindowUpdateFrame, PingFrame,
GoAwayFrame, RstStreamFrame, PushPromiseFrame, PriorityFrame,
ContinuationFrame, AltSvcFrame
AltSvcFrame,
ContinuationFrame,
DataFrame,
GoAwayFrame,
HeadersFrame,
PingFrame,
PriorityFrame,
PushPromiseFrame,
RstStreamFrame,
SettingsFrame,
WindowUpdateFrame,
)
from hpack.hpack import Encoder


SAMPLE_SETTINGS = {
SettingsFrame.HEADER_TABLE_SIZE: 4096,
Expand All @@ -19,32 +25,35 @@
}


class FrameFactory(object):
class FrameFactory:
"""
A class containing lots of helper methods and state to build frames. This
allows test cases to easily build correct HTTP/2 frames to feed to
hyper-h2.
"""
def __init__(self):

def __init__(self) -> None:
self.encoder = Encoder()

def refresh_encoder(self):
def refresh_encoder(self) -> None:
self.encoder = Encoder()

def preamble(self):
return b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'
def preamble(self) -> bytes:
return b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"

def build_headers_frame(self,
headers,
flags=[],
flags=None,
stream_id=1,
**priority_kwargs):
"""
Builds a single valid headers frame out of the contained headers.
"""
if flags is None:
flags = []
f = HeadersFrame(stream_id)
f.data = self.encoder.encode(headers)
f.flags.add('END_HEADERS')
f.flags.add("END_HEADERS")
for flag in flags:
f.flags.add(flag)

Expand All @@ -53,10 +62,12 @@ def build_headers_frame(self,

return f

def build_continuation_frame(self, header_block, flags=[], stream_id=1):
def build_continuation_frame(self, header_block, flags=None, stream_id=1):
"""
Builds a single continuation frame out of the binary header block.
"""
if flags is None:
flags = []
f = ContinuationFrame(stream_id)
f.data = header_block
f.flags = set(flags)
Expand All @@ -73,7 +84,7 @@ def build_data_frame(self, data, flags=None, stream_id=1, padding_len=0):
f.flags = flags

if padding_len:
flags.add('PADDED')
flags.add("PADDED")
f.pad_length = padding_len

return f
Expand All @@ -84,7 +95,7 @@ def build_settings_frame(self, settings, ack=False):
"""
f = SettingsFrame(0)
if ack:
f.flags.add('ACK')
f.flags.add("ACK")

f.settings = settings
return f
Expand All @@ -111,7 +122,7 @@ def build_ping_frame(self, ping_data, flags=None):
def build_goaway_frame(self,
last_stream_id,
error_code=0,
additional_data=b''):
additional_data=b""):
"""
Builds a single GOAWAY frame.
"""
Expand All @@ -133,15 +144,17 @@ def build_push_promise_frame(self,
stream_id,
promised_stream_id,
headers,
flags=[]):
flags=None):
"""
Builds a single PUSH_PROMISE frame.
"""
if flags is None:
flags = []
f = PushPromiseFrame(stream_id)
f.promised_stream_id = promised_stream_id
f.data = self.encoder.encode(headers)
f.flags = set(flags)
f.flags.add('END_HEADERS')
f.flags.add("END_HEADERS")
return f

def build_priority_frame(self,
Expand All @@ -167,7 +180,7 @@ def build_alt_svc_frame(self, stream_id, origin, field):
f.field = field
return f

def change_table_size(self, new_size):
def change_table_size(self, new_size) -> None:
"""
Causes the encoder to send a dynamic size update in the next header
block it sends.
Expand Down
Loading

0 comments on commit 0b98b24

Please sign in to comment.