From f9d4500c6efc46626f3576b8347608b521910849 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 30 Jul 2024 10:58:42 +0100 Subject: [PATCH 1/5] test on 3.13 --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d62e67aa..a53ed54d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,6 +21,7 @@ jobs: - "3.10" - "3.11" - "3.12" + - "3.13" - "pypy-3.8" - "pypy-3.9" - "pypy-3.10" @@ -48,6 +49,7 @@ jobs: with: python-version: ${{ matrix.python-version }} cache: pip + allow-prereleases: true - name: Install project dependencies run: | From e2815fbc88e47c7037af8b0114e33ccf506e8edc Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 18 Sep 2024 22:31:36 +0100 Subject: [PATCH 2/5] move httbin_ssl_context fixture into the one place it's used --- tests/integration/conftest.py | 16 ---------------- tests/integration/test_aiohttp.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 16 deletions(-) delete mode 100644 tests/integration/conftest.py diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py deleted file mode 100644 index 05908e51..00000000 --- a/tests/integration/conftest.py +++ /dev/null @@ -1,16 +0,0 @@ -import os -import ssl - -import pytest - - -@pytest.fixture -def httpbin_ssl_context(): - ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] - ssl_cert_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "cert.pem") - ssl_key_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "key.pem") - - ssl_context = ssl.create_default_context(cafile=ssl_ca_location) - ssl_context.load_cert_chain(ssl_cert_location, ssl_key_location) - - return ssl_context diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index 7bf5d771..4bcf8987 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -1,4 +1,6 @@ import logging +import os +import ssl import urllib.parse import pytest @@ -12,6 +14,18 @@ from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402 +@pytest.fixture +def httpbin_ssl_context(): + ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] + ssl_cert_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "cert.pem") + ssl_key_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "key.pem") + + ssl_context = ssl.create_default_context(cafile=ssl_ca_location) + ssl_context.load_cert_chain(ssl_cert_location, ssl_key_location) + + return ssl_context + + def run_in_loop(fn): async def wrapper(): return await fn(asyncio.get_running_loop()) From d123a5e8d00c2b4ae6fe4f2cf68408691db72d48 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 18 Sep 2024 22:34:48 +0100 Subject: [PATCH 3/5] replace fixture with constant --- tests/integration/test_aiohttp.py | 33 +++++++++++++------------------ 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index 4bcf8987..fab8bdfb 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -13,17 +13,12 @@ from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402 +ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] +ssl_cert_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "cert.pem") +ssl_key_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "key.pem") -@pytest.fixture -def httpbin_ssl_context(): - ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] - ssl_cert_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "cert.pem") - ssl_key_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "key.pem") - - ssl_context = ssl.create_default_context(cafile=ssl_ca_location) - ssl_context.load_cert_chain(ssl_cert_location, ssl_key_location) - - return ssl_context +HTTPBIN_SSL_CONTEXT = ssl.create_default_context(cafile=ssl_ca_location) +HTTPBIN_SSL_CONTEXT.load_cert_chain(ssl_cert_location, ssl_key_location) def run_in_loop(fn): @@ -352,7 +347,7 @@ def test_double_requests(tmpdir, httpbin): assert cassette.play_count == 2 -def test_cookies(httpbin_both, httpbin_ssl_context, tmpdir): +def test_cookies(httpbin_both, tmpdir): async def run(loop): cookies_url = httpbin_both.url + ( "/response-headers?" @@ -367,12 +362,12 @@ async def run(loop): # ------------------------- Record -------------------------- # with vcr.use_cassette(tmp) as cassette: async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session: - cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context) + cookies_resp = await session.get(cookies_url, ssl=HTTPBIN_SSL_CONTEXT) home_resp = await session.get( home_url, cookies=req_cookies, headers=req_headers, - ssl=httpbin_ssl_context, + ssl=HTTPBIN_SSL_CONTEXT, ) assert cassette.play_count == 0 assert_responses(cookies_resp, home_resp) @@ -380,12 +375,12 @@ async def run(loop): # -------------------------- Play --------------------------- # with vcr.use_cassette(tmp, record_mode=vcr.mode.NONE) as cassette: async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session: - cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context) + cookies_resp = await session.get(cookies_url, ssl=HTTPBIN_SSL_CONTEXT) home_resp = await session.get( home_url, cookies=req_cookies, headers=req_headers, - ssl=httpbin_ssl_context, + ssl=HTTPBIN_SSL_CONTEXT, ) assert cassette.play_count == 2 assert_responses(cookies_resp, home_resp) @@ -402,7 +397,7 @@ def assert_responses(cookies_resp, home_resp): run_in_loop(run) -def test_cookies_redirect(httpbin_both, httpbin_ssl_context, tmpdir): +def test_cookies_redirect(httpbin_both, tmpdir): async def run(loop): # Sets cookie as provided by the query string and redirects cookies_url = httpbin_both.url + "/cookies/set?Cookie_1=Val_1" @@ -411,7 +406,7 @@ async def run(loop): # ------------------------- Record -------------------------- # with vcr.use_cassette(tmp) as cassette: async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session: - cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context) + cookies_resp = await session.get(cookies_url, ssl=HTTPBIN_SSL_CONTEXT) assert not cookies_resp.cookies cookies = session.cookie_jar.filter_cookies(cookies_url) assert cookies["Cookie_1"].value == "Val_1" @@ -422,7 +417,7 @@ async def run(loop): # -------------------------- Play --------------------------- # with vcr.use_cassette(tmp, record_mode=vcr.mode.NONE) as cassette: async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session: - cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context) + cookies_resp = await session.get(cookies_url, ssl=HTTPBIN_SSL_CONTEXT) assert not cookies_resp.cookies cookies = session.cookie_jar.filter_cookies(cookies_url) assert cookies["Cookie_1"].value == "Val_1" @@ -436,7 +431,7 @@ async def run(loop): "Cookie_1=Val_1; Expires=Wed, 21 Oct 2015 07:28:00 GMT", ] async with aiohttp.ClientSession(loop=loop, cookie_jar=aiohttp.CookieJar(unsafe=True)) as session: - cookies_resp = await session.get(cookies_url, ssl=httpbin_ssl_context) + cookies_resp = await session.get(cookies_url, ssl=HTTPBIN_SSL_CONTEXT) assert not cookies_resp.cookies cookies = session.cookie_jar.filter_cookies(cookies_url) assert cookies["Cookie_1"].value == "Val_1" From 2b3247b3df7fcf3e87d804a84c1fb6019a32b8b3 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 18 Sep 2024 22:35:15 +0100 Subject: [PATCH 4/5] remove redundant load_cert_chain --- tests/integration/test_aiohttp.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index fab8bdfb..ba8a7e37 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -14,11 +14,8 @@ from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402 ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] -ssl_cert_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "cert.pem") -ssl_key_location = os.environ["REQUESTS_CA_BUNDLE"].replace("cacert.pem", "key.pem") HTTPBIN_SSL_CONTEXT = ssl.create_default_context(cafile=ssl_ca_location) -HTTPBIN_SSL_CONTEXT.load_cert_chain(ssl_cert_location, ssl_key_location) def run_in_loop(fn): From f5597fa6c16db892f45d7ca4c0485eaeff2854ff Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 18 Sep 2024 22:36:17 +0100 Subject: [PATCH 5/5] use pytest_httbin.certs.where() for cafile --- tests/integration/test_aiohttp.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index ba8a7e37..97a489b8 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -1,9 +1,9 @@ import logging -import os import ssl import urllib.parse import pytest +import pytest_httpbin.certs import vcr @@ -13,9 +13,7 @@ from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402 -ssl_ca_location = os.environ["REQUESTS_CA_BUNDLE"] - -HTTPBIN_SSL_CONTEXT = ssl.create_default_context(cafile=ssl_ca_location) +HTTPBIN_SSL_CONTEXT = ssl.create_default_context(cafile=pytest_httpbin.certs.where()) def run_in_loop(fn):