Skip to content

Commit 0beea46

Browse files
use pytest-httpbin also for SSL tests
1 parent deaf361 commit 0beea46

File tree

5 files changed

+22
-33
lines changed

5 files changed

+22
-33
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ jobs:
5656
runs-on: ${{ matrix.os }}
5757
name: test-${{ matrix.python-version }} ${{ matrix.nox-session }}
5858
continue-on-error: ${{ matrix.experimental }}
59-
services:
60-
elasticsearch:
61-
image: docker.elastic.co/elasticsearch/elasticsearch:9.0.2
62-
env:
63-
discovery.type: single-node
64-
ports:
65-
- 9200:9200
6659
steps:
6760
- name: Checkout repository
6861
uses: actions/checkout@v2

tests/conftest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,22 @@ async def perform_request(self, *args, **kwargs):
6666
return NodeApiResponse(meta, self.body)
6767

6868

69-
@pytest.fixture(scope="session", params=[True, False])
70-
def cert_fingerprint(request) -> str:
71-
"""Gets the SHA256 fingerprint of the certificate for localhost:9200"""
72-
sock = socket.create_connection(("localhost", 9200))
69+
@pytest.fixture(scope="session", params=["short-lower", "short-upper", "long-lower", "long-upper"])
70+
def cert_fingerprint(request, httpbin_secure) -> str:
71+
"""Gets the SHA256 fingerprint of the certificate for the secure httpbin"""
72+
sock = socket.create_connection((httpbin_secure.host, httpbin_secure.port))
7373
ctx = ssl.create_default_context()
7474
ctx.check_hostname = False
7575
ctx.verify_mode = ssl.CERT_NONE
7676
sock = ctx.wrap_socket(sock)
7777
digest = hashlib.sha256(sock.getpeercert(binary_form=True)).hexdigest()
7878
assert len(digest) == 64
7979
sock.close()
80-
if request.param:
80+
if "upper" in request.param:
81+
digest = digest.upper()
82+
else:
83+
digst = digest.lower()
84+
if "short" in request.param:
8185
return digest
8286
else:
8387
return ":".join([digest[i : i + 2] for i in range(0, len(digest), 2)])

tests/node/test_http_aiohttp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,19 @@ async def test_head_workaround(self, aiohttp_fixed_head_bug):
290290

291291

292292
@pytest.mark.asyncio
293-
async def test_ssl_assert_fingerprint(cert_fingerprint):
293+
async def test_ssl_assert_fingerprint(cert_fingerprint, httpbin_secure):
294294
with warnings.catch_warnings(record=True) as w:
295295
node = AiohttpHttpNode(
296296
NodeConfig(
297297
scheme="https",
298-
host="localhost",
299-
port=9200,
298+
host=httpbin_secure.host,
299+
port=httpbin_secure.port,
300300
ssl_assert_fingerprint=cert_fingerprint,
301301
)
302302
)
303303
resp, _ = await node.perform_request("GET", "/")
304304

305-
assert resp.status == 401
305+
assert resp.status == 200
306306
assert [str(x.message) for x in w if x.category != DeprecationWarning] == []
307307

308308

tests/node/test_http_httpx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ async def test_merge_headers(self):
145145
assert request.headers["h3"] == "v3"
146146

147147

148-
def test_ssl_assert_fingerprint(cert_fingerprint):
148+
def test_ssl_assert_fingerprint(cert_fingerprint, httpbin_secure):
149149
with pytest.raises(ValueError, match="httpx does not support certificate pinning"):
150150
HttpxAsyncHttpNode(
151151
NodeConfig(
152152
scheme="https",
153-
host="localhost",
154-
port=9200,
153+
host=httpbin_secure.host,
154+
port=httpbin_secure.port,
155155
ssl_assert_fingerprint=cert_fingerprint,
156156
)
157157
)

tests/node/test_urllib3_chain_certs.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,14 @@ def test_ssl_assert_fingerprint_invalid_length(node_cls):
4949

5050
@requires_ssl_assert_fingerprint_in_chain
5151
@pytest.mark.parametrize("node_cls", [Urllib3HttpNode, RequestsHttpNode])
52-
@pytest.mark.parametrize(
53-
"ssl_assert_fingerprint",
54-
[
55-
"18efbd94dda87e3598a1251f9440cd2f4fd1dbf08be007c1012e992e830ca262",
56-
"18:EF:BD:94:DD:A8:7E:35:98:A1:25:1F:94:40:CD:2F:4F:D1:DB:F0:8B:E0:07:C1:01:2E:99:2E:83:0C:A2:62",
57-
"18EFBD94DDA87E3598A1251F9440CD2F4FD1DBF08BE007C1012E992E830CA262",
58-
],
59-
)
60-
def test_assert_fingerprint_in_cert_chain(node_cls, ssl_assert_fingerprint):
52+
def test_assert_fingerprint_in_cert_chain(node_cls, cert_fingerprint, httpbin_secure):
6153
with warnings.catch_warnings(record=True) as w:
6254
node = node_cls(
6355
NodeConfig(
6456
"https",
65-
"www.elastic.co",
66-
443,
67-
ssl_assert_fingerprint=ssl_assert_fingerprint,
57+
httpbin_secure.host,
58+
httpbin_secure.port,
59+
ssl_assert_fingerprint=cert_fingerprint,
6860
)
6961
)
7062
meta, _ = node.perform_request("GET", "/")
@@ -75,7 +67,7 @@ def test_assert_fingerprint_in_cert_chain(node_cls, ssl_assert_fingerprint):
7567

7668
@requires_ssl_assert_fingerprint_in_chain
7769
@pytest.mark.parametrize("node_cls", [Urllib3HttpNode, RequestsHttpNode])
78-
def test_assert_fingerprint_in_cert_chain_failure(node_cls):
70+
def test_assert_fingerprint_in_cert_chain_failure(node_cls, httpbin_secure, cert_fingerprint):
7971
node = node_cls(
8072
NodeConfig(
8173
"https",
@@ -97,4 +89,4 @@ def test_assert_fingerprint_in_cert_chain_failure(node_cls):
9789
in err
9890
)
9991
# This is the root CA for httpbin.org with a leading comma to denote more than one cert was listed.
100-
assert '"18efbd94dda87e3598a1251f9440cd2f4fd1dbf08be007c1012e992e830ca262"' in err
92+
assert '"18efbd94dda87e3598a1251f9440cd2f4fd1dbf08be007c1012e992e830ca262",' in err

0 commit comments

Comments
 (0)