Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Properly typecheck types.http #14988

Merged
merged 22 commits into from
Feb 7, 2023
Merged
28 changes: 21 additions & 7 deletions tests/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

from OpenSSL import SSL
from OpenSSL.SSL import Connection
from twisted.internet.interfaces import IOpenSSLServerConnectionCreator
from twisted.internet.interfaces import IAddress, IOpenSSLServerConnectionCreator
from twisted.internet.ssl import Certificate, trustRootFromCertificates
from twisted.protocols.tls import TLSMemoryBIOProtocol
from twisted.web.client import BrowserLikePolicyForHTTPS # noqa: F401
from twisted.web.iweb import IPolicyForHTTPS # noqa: F401


def get_test_https_policy():
def get_test_https_policy() -> BrowserLikePolicyForHTTPS:
"""Get a test IPolicyForHTTPS which trusts the test CA cert

Returns:
Expand All @@ -39,7 +40,7 @@ def get_test_https_policy():
return BrowserLikePolicyForHTTPS(trustRoot=trust_root)


def get_test_ca_cert_file():
def get_test_ca_cert_file() -> str:
"""Get the path to the test CA cert

The keypair is generated with:
Expand All @@ -51,7 +52,7 @@ def get_test_ca_cert_file():
return os.path.join(os.path.dirname(__file__), "ca.crt")


def get_test_key_file():
def get_test_key_file() -> str:
"""get the path to the test key

The key file is made with:
Expand Down Expand Up @@ -137,15 +138,28 @@ class TestServerTLSConnectionFactory:
"""An SSL connection creator which returns connections which present a certificate
signed by our test CA."""

def __init__(self, sanlist):
def __init__(self, sanlist: List[bytes]):
"""
Args:
sanlist: list[bytes]: a list of subjectAltName values for the cert
sanlist: a list of subjectAltName values for the cert
"""
self._cert_file = create_test_cert_file(sanlist)

def serverConnectionForTLS(self, tlsProtocol):
def serverConnectionForTLS(self, tlsProtocol: TLSMemoryBIOProtocol) -> Connection:
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_certificate_file(self._cert_file)
ctx.use_privatekey_file(get_test_key_file())
return Connection(ctx, None)


@implementer(IAddress)
class DummyAddress:
"""Dummy stand-in for an IAddress.

Placates mypy. Useful for tests that don't care about addresses, but
about other HTTP machinery."""

pass


dummy_address = DummyAddress()
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved