Skip to content

Commit 0c47c36

Browse files
committed
Add tests for checking function signatures
1 parent 4d50140 commit 0c47c36

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

tests/test_urllib3_sigv4.py

+47-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

3+
import inspect
34
import re
5+
import sys
46
from typing import Any, Mapping, NamedTuple
57
from unittest.mock import patch
68

79
import pytest
8-
from urllib3 import HTTPResponse
10+
import urllib3
911

1012
from urllib3_sigv4 import PoolManager, SigV4RequestSigner, request
1113

@@ -83,11 +85,37 @@ def pool_with_signer(signer: SigV4RequestSigner) -> PoolManager:
8385

8486

8587
class TestPoolManager:
88+
@pytest.mark.skipif(
89+
sys.version_info < (3, 10), reason="requires Python 3.10 or newer"
90+
)
91+
def test_constructor_signature(self) -> None:
92+
"""Check constructor signature matches that of urllib3.PoolManager."""
93+
sig_orig = inspect.signature(urllib3.PoolManager, eval_str=True)
94+
sig = inspect.signature(PoolManager, eval_str=True)
95+
params_orig = dict(sig_orig.parameters)
96+
params = dict(sig.parameters)
97+
params.pop("signer")
98+
assert params_orig == params
99+
100+
@pytest.mark.skipif(
101+
sys.version_info < (3, 10), reason="requires Python 3.10 or newer"
102+
)
103+
def test_request_signature(self) -> None:
104+
"""Check request() signature matches that of urllib3.PoolManager."""
105+
sig_orig = inspect.signature(
106+
urllib3.PoolManager.request, eval_str=True
107+
)
108+
sig = inspect.signature(PoolManager.request, eval_str=True)
109+
params_orig = dict(sig_orig.parameters)
110+
params = dict(sig.parameters)
111+
params.pop("signer")
112+
assert params_orig == params
113+
86114
def test_not_signed(self, pool: PoolManager) -> None:
87115
"""Check requests are not signed by default."""
88116
with patch(
89117
"urllib3.connectionpool.HTTPConnectionPool.urlopen",
90-
return_value=HTTPResponse(),
118+
return_value=urllib3.HTTPResponse(),
91119
) as urlopen:
92120
pool.request(
93121
"GET",
@@ -101,7 +129,7 @@ def test_pool_signer(self, pool_with_signer: PoolManager) -> None:
101129
"""Check requests are signed by default if pool created with signer."""
102130
with patch(
103131
"urllib3.connectionpool.HTTPConnectionPool.urlopen",
104-
return_value=HTTPResponse(),
132+
return_value=urllib3.HTTPResponse(),
105133
) as urlopen:
106134
pool_with_signer.request(
107135
"GET",
@@ -118,7 +146,7 @@ def test_request_signer(
118146
"""Check individual request is signed with provided signer."""
119147
with patch(
120148
"urllib3.connectionpool.HTTPConnectionPool.urlopen",
121-
return_value=HTTPResponse(),
149+
return_value=urllib3.HTTPResponse(),
122150
) as urlopen:
123151
pool.request(
124152
"GET",
@@ -136,7 +164,7 @@ def test_signer_precedence(
136164
"""Check request signer takes precedence over pool signer."""
137165
with patch(
138166
"urllib3.connectionpool.HTTPConnectionPool.urlopen",
139-
return_value=HTTPResponse(),
167+
return_value=urllib3.HTTPResponse(),
140168
) as urlopen:
141169
pool_with_signer.request(
142170
"GET",
@@ -153,11 +181,23 @@ def test_signer_precedence(
153181

154182

155183
class TestRequest:
184+
@pytest.mark.skipif(
185+
sys.version_info < (3, 10), reason="requires Python 3.10 or newer"
186+
)
187+
def test_signature(self) -> None:
188+
"""Check signature matches that of urllib3.request()."""
189+
sig_orig = inspect.signature(urllib3.request, eval_str=True)
190+
sig = inspect.signature(request, eval_str=True)
191+
params_orig = dict(sig_orig.parameters)
192+
params = dict(sig.parameters)
193+
params.pop("signer")
194+
assert params_orig == params
195+
156196
def test_not_signed(self) -> None:
157197
"""Check request is not signed by default."""
158198
with patch(
159199
"urllib3.connectionpool.HTTPConnectionPool.urlopen",
160-
return_value=HTTPResponse(),
200+
return_value=urllib3.HTTPResponse(),
161201
) as urlopen:
162202
request(
163203
"GET",
@@ -171,7 +211,7 @@ def test_signer(self, signer: SigV4RequestSigner) -> None:
171211
"""Check request is signed with provided signer."""
172212
with patch(
173213
"urllib3.connectionpool.HTTPConnectionPool.urlopen",
174-
return_value=HTTPResponse(),
214+
return_value=urllib3.HTTPResponse(),
175215
) as urlopen:
176216
request(
177217
"GET",

0 commit comments

Comments
 (0)