Skip to content

Commit

Permalink
Allow testing HTTP/1.1 and HTTP/2 in the same test (#3310)
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Jan 24, 2024
1 parent 89ed0d6 commit 71e7c35
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 77 deletions.
1 change: 1 addition & 0 deletions dev-requirements.txt
@@ -1,3 +1,4 @@
h2==4.1.0
coverage==7.4.0
PySocks==1.7.1
pytest==7.4.2
Expand Down
4 changes: 2 additions & 2 deletions dummyserver/socketserver.py
Expand Up @@ -20,7 +20,7 @@
from cryptography.hazmat.primitives import serialization

from urllib3.exceptions import HTTPWarning
from urllib3.util import ALPN_PROTOCOLS, resolve_cert_reqs, resolve_ssl_version
from urllib3.util import resolve_cert_reqs, resolve_ssl_version

if typing.TYPE_CHECKING:
from typing_extensions import ParamSpec
Expand All @@ -35,7 +35,7 @@
"keyfile": os.path.join(CERTS_PATH, "server.key"),
"cert_reqs": ssl.CERT_OPTIONAL,
"ca_certs": os.path.join(CERTS_PATH, "cacert.pem"),
"alpn_protocols": ALPN_PROTOCOLS,
"alpn_protocols": ["h2", "http/1.1"],
}
DEFAULT_CA = os.path.join(CERTS_PATH, "cacert.pem")
DEFAULT_CA_KEY = os.path.join(CERTS_PATH, "cacert.key")
Expand Down
12 changes: 12 additions & 0 deletions test/conftest.py
Expand Up @@ -10,6 +10,7 @@
import pytest
import trustme

import urllib3.http2
from dummyserver.app import hypercorn_app
from dummyserver.asgi_proxy import ProxyApp
from dummyserver.hypercornserver import run_hypercorn_in_thread
Expand Down Expand Up @@ -369,3 +370,14 @@ def requires_tlsv1_3(supported_tls_versions: typing.AbstractSet[str]) -> None:
or "TLSv1.3" not in supported_tls_versions
):
pytest.skip("Test requires TLSv1.3")


@pytest.fixture(params=["h11", "h2"])
def http_version(request: pytest.FixtureRequest) -> typing.Generator[str, None, None]:
if request.param == "h2":
urllib3.http2.inject_into_urllib3()

yield request.param

if request.param == "h2":
urllib3.http2.extract_from_urllib3()
73 changes: 0 additions & 73 deletions test/with_dummyserver/test_http2.py

This file was deleted.

6 changes: 4 additions & 2 deletions test/with_dummyserver/test_https.py
Expand Up @@ -129,7 +129,7 @@ def teardown_class(cls) -> None:

shutil.rmtree(cls.certs_dir)

def test_simple(self) -> None:
def test_simple(self, http_version: str) -> None:
with HTTPSConnectionPool(
self.host,
self.port,
Expand All @@ -138,6 +138,7 @@ def test_simple(self) -> None:
) as https_pool:
r = https_pool.request("GET", "/")
assert r.status == 200, r.data
assert r.headers["server"] == f"hypercorn-{http_version}"

@resolvesLocalhostFQDN()
def test_dotted_fqdn(self) -> None:
Expand Down Expand Up @@ -1130,7 +1131,7 @@ def test_can_validate_ip_san(self, ipv4_san_server: ServerConfig) -> None:
class TestHTTPS_IPV6SAN:
@pytest.mark.parametrize("host", ["::1", "[::1]"])
def test_can_validate_ipv6_san(
self, ipv6_san_server: ServerConfig, host: str
self, ipv6_san_server: ServerConfig, host: str, http_version: str
) -> None:
"""Ensure that urllib3 can validate SANs with IPv6 addresses in them."""
with HTTPSConnectionPool(
Expand All @@ -1141,3 +1142,4 @@ def test_can_validate_ipv6_san(
) as https_pool:
r = https_pool.request("GET", "/")
assert r.status == 200
assert r.headers["server"] == f"hypercorn-{http_version}"

0 comments on commit 71e7c35

Please sign in to comment.