Skip to content

Commit af993c3

Browse files
Decouple tests from hitting example.org (#204)
1 parent 977e88e commit af993c3

4 files changed

Lines changed: 165 additions & 109 deletions

File tree

tests/async_tests/test_interfaces.py

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import httpcore
77
from httpcore._types import URL
8-
from tests.conftest import Server
9-
from tests.utils import lookup_async_backend
8+
from tests.conftest import HTTPS_SERVER_URL, UvicornServer
9+
from tests.utils import Server, lookup_async_backend
1010

1111

1212
@pytest.fixture(params=["auto", "anyio"])
@@ -25,11 +25,11 @@ async def read_body(stream: httpcore.AsyncByteStream) -> bytes:
2525

2626

2727
@pytest.mark.anyio
28-
async def test_http_request(backend: str) -> None:
28+
async def test_http_request(backend: str, server: Server) -> None:
2929
async with httpcore.AsyncConnectionPool(backend=backend) as http:
3030
method = b"GET"
31-
url = (b"http", b"example.org", 80, b"/")
32-
headers = [(b"host", b"example.org")]
31+
url = (b"http", *server.netloc, b"/")
32+
headers = [server.host_header]
3333
status_code, headers, stream, ext = await http.arequest(method, url, headers)
3434
await read_body(stream)
3535

@@ -39,11 +39,11 @@ async def test_http_request(backend: str) -> None:
3939

4040

4141
@pytest.mark.anyio
42-
async def test_https_request(backend: str) -> None:
42+
async def test_https_request(backend: str, https_server: Server) -> None:
4343
async with httpcore.AsyncConnectionPool(backend=backend) as http:
4444
method = b"GET"
45-
url = (b"https", b"example.org", 443, b"/")
46-
headers = [(b"host", b"example.org")]
45+
url = (b"https", *https_server.netloc, b"/")
46+
headers = [https_server.host_header]
4747
status_code, headers, stream, ext = await http.arequest(method, url, headers)
4848
await read_body(stream)
4949

@@ -63,11 +63,11 @@ async def test_request_unsupported_protocol(backend: str) -> None:
6363

6464

6565
@pytest.mark.anyio
66-
async def test_http2_request(backend: str) -> None:
66+
async def test_http2_request(backend: str, https_server: Server) -> None:
6767
async with httpcore.AsyncConnectionPool(backend=backend, http2=True) as http:
6868
method = b"GET"
69-
url = (b"https", b"example.org", 443, b"/")
70-
headers = [(b"host", b"example.org")]
69+
url = (b"https", *https_server.netloc, b"/")
70+
headers = [https_server.host_header]
7171
status_code, headers, stream, ext = await http.arequest(method, url, headers)
7272
await read_body(stream)
7373

@@ -77,11 +77,11 @@ async def test_http2_request(backend: str) -> None:
7777

7878

7979
@pytest.mark.anyio
80-
async def test_closing_http_request(backend: str) -> None:
80+
async def test_closing_http_request(backend: str, server: Server) -> None:
8181
async with httpcore.AsyncConnectionPool(backend=backend) as http:
8282
method = b"GET"
83-
url = (b"http", b"example.org", 80, b"/")
84-
headers = [(b"host", b"example.org"), (b"connection", b"close")]
83+
url = (b"http", *server.netloc, b"/")
84+
headers = [server.host_header, (b"connection", b"close")]
8585
status_code, headers, stream, ext = await http.arequest(method, url, headers)
8686
await read_body(stream)
8787

@@ -91,11 +91,11 @@ async def test_closing_http_request(backend: str) -> None:
9191

9292

9393
@pytest.mark.anyio
94-
async def test_http_request_reuse_connection(backend: str) -> None:
94+
async def test_http_request_reuse_connection(backend: str, server: Server) -> None:
9595
async with httpcore.AsyncConnectionPool(backend=backend) as http:
9696
method = b"GET"
97-
url = (b"http", b"example.org", 80, b"/")
98-
headers = [(b"host", b"example.org")]
97+
url = (b"http", *server.netloc, b"/")
98+
headers = [server.host_header]
9999
status_code, headers, stream, ext = await http.arequest(method, url, headers)
100100
await read_body(stream)
101101

@@ -104,8 +104,8 @@ async def test_http_request_reuse_connection(backend: str) -> None:
104104
assert len(http._connections[url[:3]]) == 1 # type: ignore
105105

106106
method = b"GET"
107-
url = (b"http", b"example.org", 80, b"/")
108-
headers = [(b"host", b"example.org")]
107+
url = (b"http", *server.netloc, b"/")
108+
headers = [server.host_header]
109109
status_code, headers, stream, ext = await http.arequest(method, url, headers)
110110
await read_body(stream)
111111

@@ -115,11 +115,13 @@ async def test_http_request_reuse_connection(backend: str) -> None:
115115

116116

117117
@pytest.mark.anyio
118-
async def test_https_request_reuse_connection(backend: str) -> None:
118+
async def test_https_request_reuse_connection(
119+
backend: str, https_server: Server
120+
) -> None:
119121
async with httpcore.AsyncConnectionPool(backend=backend) as http:
120122
method = b"GET"
121-
url = (b"https", b"example.org", 443, b"/")
122-
headers = [(b"host", b"example.org")]
123+
url = (b"https", *https_server.netloc, b"/")
124+
headers = [https_server.host_header]
123125
status_code, headers, stream, ext = await http.arequest(method, url, headers)
124126
await read_body(stream)
125127

@@ -128,8 +130,8 @@ async def test_https_request_reuse_connection(backend: str) -> None:
128130
assert len(http._connections[url[:3]]) == 1 # type: ignore
129131

130132
method = b"GET"
131-
url = (b"https", b"example.org", 443, b"/")
132-
headers = [(b"host", b"example.org")]
133+
url = (b"https", *https_server.netloc, b"/")
134+
headers = [https_server.host_header]
133135
status_code, headers, stream, ext = await http.arequest(method, url, headers)
134136
await read_body(stream)
135137

@@ -139,11 +141,13 @@ async def test_https_request_reuse_connection(backend: str) -> None:
139141

140142

141143
@pytest.mark.anyio
142-
async def test_http_request_cannot_reuse_dropped_connection(backend: str) -> None:
144+
async def test_http_request_cannot_reuse_dropped_connection(
145+
backend: str, server: Server
146+
) -> None:
143147
async with httpcore.AsyncConnectionPool(backend=backend) as http:
144148
method = b"GET"
145-
url = (b"http", b"example.org", 80, b"/")
146-
headers = [(b"host", b"example.org")]
149+
url = (b"http", *server.netloc, b"/")
150+
headers = [server.host_header]
147151
status_code, headers, stream, ext = await http.arequest(method, url, headers)
148152
await read_body(stream)
149153

@@ -156,8 +160,8 @@ async def test_http_request_cannot_reuse_dropped_connection(backend: str) -> Non
156160
connection.is_connection_dropped = lambda: True # type: ignore
157161

158162
method = b"GET"
159-
url = (b"http", b"example.org", 80, b"/")
160-
headers = [(b"host", b"example.org")]
163+
url = (b"http", *server.netloc, b"/")
164+
headers = [server.host_header]
161165
status_code, headers, stream, ext = await http.arequest(method, url, headers)
162166
await read_body(stream)
163167

@@ -168,10 +172,12 @@ async def test_http_request_cannot_reuse_dropped_connection(backend: str) -> Non
168172

169173
@pytest.mark.parametrize("proxy_mode", ["DEFAULT", "FORWARD_ONLY", "TUNNEL_ONLY"])
170174
@pytest.mark.anyio
171-
async def test_http_proxy(proxy_server: URL, proxy_mode: str, backend: str) -> None:
175+
async def test_http_proxy(
176+
proxy_server: URL, proxy_mode: str, backend: str, server: Server
177+
) -> None:
172178
method = b"GET"
173-
url = (b"http", b"example.org", 80, b"/")
174-
headers = [(b"host", b"example.org")]
179+
url = (b"http", *server.netloc, b"/")
180+
headers = [server.host_header]
175181
max_connections = 1
176182
async with httpcore.AsyncHTTPProxy(
177183
proxy_server,
@@ -187,16 +193,16 @@ async def test_http_proxy(proxy_server: URL, proxy_mode: str, backend: str) -> N
187193

188194

189195
@pytest.mark.anyio
190-
async def test_http_request_local_address(backend: str) -> None:
196+
async def test_http_request_local_address(backend: str, server: Server) -> None:
191197
if backend == "auto" and lookup_async_backend() == "trio":
192198
pytest.skip("The trio backend does not support local_address")
193199

194200
async with httpcore.AsyncConnectionPool(
195201
backend=backend, local_address="0.0.0.0"
196202
) as http:
197203
method = b"GET"
198-
url = (b"http", b"example.org", 80, b"/")
199-
headers = [(b"host", b"example.org")]
204+
url = (b"http", *server.netloc, b"/")
205+
headers = [server.host_header]
200206
status_code, headers, stream, ext = await http.arequest(method, url, headers)
201207
await read_body(stream)
202208

@@ -210,11 +216,15 @@ async def test_http_request_local_address(backend: str) -> None:
210216
@pytest.mark.parametrize("http2", [False, True])
211217
@pytest.mark.anyio
212218
async def test_proxy_https_requests(
213-
proxy_server: URL, ca_ssl_context: ssl.SSLContext, proxy_mode: str, http2: bool
219+
proxy_server: URL,
220+
ca_ssl_context: ssl.SSLContext,
221+
proxy_mode: str,
222+
http2: bool,
223+
https_server: Server,
214224
) -> None:
215225
method = b"GET"
216-
url = (b"https", b"example.org", 443, b"/")
217-
headers = [(b"host", b"example.org")]
226+
url = (b"https", *https_server.netloc, b"/")
227+
headers = [https_server.host_header]
218228
max_connections = 1
219229
async with httpcore.AsyncHTTPProxy(
220230
proxy_server,
@@ -237,25 +247,25 @@ async def test_proxy_https_requests(
237247
(
238248
False,
239249
60.0,
240-
{"https://example.org": ["HTTP/1.1, ACTIVE", "HTTP/1.1, ACTIVE"]},
241-
{"https://example.org": ["HTTP/1.1, IDLE", "HTTP/1.1, IDLE"]},
250+
{HTTPS_SERVER_URL: ["HTTP/1.1, ACTIVE", "HTTP/1.1, ACTIVE"]},
251+
{HTTPS_SERVER_URL: ["HTTP/1.1, IDLE", "HTTP/1.1, IDLE"]},
242252
),
243253
(
244254
True,
245255
60.0,
246-
{"https://example.org": ["HTTP/2, ACTIVE, 2 streams"]},
247-
{"https://example.org": ["HTTP/2, IDLE, 0 streams"]},
256+
{HTTPS_SERVER_URL: ["HTTP/2, ACTIVE, 2 streams"]},
257+
{HTTPS_SERVER_URL: ["HTTP/2, IDLE, 0 streams"]},
248258
),
249259
(
250260
False,
251261
0.0,
252-
{"https://example.org": ["HTTP/1.1, ACTIVE", "HTTP/1.1, ACTIVE"]},
262+
{HTTPS_SERVER_URL: ["HTTP/1.1, ACTIVE", "HTTP/1.1, ACTIVE"]},
253263
{},
254264
),
255265
(
256266
True,
257267
0.0,
258-
{"https://example.org": ["HTTP/2, ACTIVE, 2 streams"]},
268+
{HTTPS_SERVER_URL: ["HTTP/2, ACTIVE, 2 streams"]},
259269
{},
260270
),
261271
],
@@ -267,13 +277,14 @@ async def test_connection_pool_get_connection_info(
267277
expected_during_active: dict,
268278
expected_during_idle: dict,
269279
backend: str,
280+
https_server: Server,
270281
) -> None:
271282
async with httpcore.AsyncConnectionPool(
272283
http2=http2, keepalive_expiry=keepalive_expiry, backend=backend
273284
) as http:
274285
method = b"GET"
275-
url = (b"https", b"example.org", 443, b"/")
276-
headers = [(b"host", b"example.org")]
286+
url = (b"https", *https_server.netloc, b"/")
287+
headers = [https_server.host_header]
277288

278289
_, _, stream_1, _ = await http.arequest(method, url, headers)
279290
_, _, stream_2, _ = await http.arequest(method, url, headers)
@@ -298,7 +309,7 @@ async def test_connection_pool_get_connection_info(
298309
)
299310
@pytest.mark.anyio
300311
async def test_http_request_unix_domain_socket(
301-
uds_server: Server, backend: str
312+
uds_server: UvicornServer, backend: str
302313
) -> None:
303314
uds = uds_server.config.uds
304315
assert uds is not None
@@ -317,14 +328,14 @@ async def test_http_request_unix_domain_socket(
317328
@pytest.mark.parametrize("connections_number", [4])
318329
@pytest.mark.anyio
319330
async def test_max_keepalive_connections_handled_correctly(
320-
max_keepalive: int, connections_number: int, backend: str
331+
max_keepalive: int, connections_number: int, backend: str, server: Server
321332
) -> None:
322333
async with httpcore.AsyncConnectionPool(
323334
max_keepalive_connections=max_keepalive, keepalive_expiry=60, backend=backend
324335
) as http:
325336
method = b"GET"
326-
url = (b"http", b"example.org", 80, b"/")
327-
headers = [(b"host", b"example.org")]
337+
url = (b"http", *server.netloc, b"/")
338+
headers = [server.host_header]
328339

329340
connections_streams = []
330341
for _ in range(connections_number):
@@ -342,11 +353,11 @@ async def test_max_keepalive_connections_handled_correctly(
342353

343354

344355
@pytest.mark.anyio
345-
async def test_explicit_backend_name() -> None:
356+
async def test_explicit_backend_name(server: Server) -> None:
346357
async with httpcore.AsyncConnectionPool(backend=lookup_async_backend()) as http:
347358
method = b"GET"
348-
url = (b"http", b"example.org", 80, b"/")
349-
headers = [(b"host", b"example.org")]
359+
url = (b"http", *server.netloc, b"/")
360+
headers = [server.host_header]
350361
status_code, headers, stream, ext = await http.arequest(method, url, headers)
351362
await read_body(stream)
352363

tests/conftest.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
from httpcore._types import URL
1616

17+
from .utils import Server
18+
1719
PROXY_HOST = "127.0.0.1"
1820
PROXY_PORT = 8080
21+
SERVER_HOST = "example.org"
22+
HTTPS_SERVER_URL = "https://example.org"
1923

2024

2125
class RunNotify:
@@ -102,7 +106,7 @@ def proxy_server(example_org_cert_path: str) -> typing.Iterator[URL]:
102106
thread.join()
103107

104108

105-
class Server(uvicorn.Server):
109+
class UvicornServer(uvicorn.Server):
106110
def install_signal_handlers(self) -> None:
107111
pass
108112

@@ -132,12 +136,22 @@ async def app(scope: dict, receive: typing.Callable, send: typing.Callable) -> N
132136

133137

134138
@pytest.fixture(scope="session")
135-
def uds_server() -> typing.Iterator[Server]:
139+
def uds_server() -> typing.Iterator[UvicornServer]:
136140
uds = "test_server.sock"
137141
config = uvicorn.Config(app=app, lifespan="off", loop="asyncio", uds=uds)
138-
server = Server(config=config)
142+
server = UvicornServer(config=config)
139143
try:
140144
with server.serve_in_thread():
141145
yield server
142146
finally:
143147
os.remove(uds)
148+
149+
150+
@pytest.fixture(scope="session")
151+
def server() -> Server:
152+
return Server(SERVER_HOST, port=80)
153+
154+
155+
@pytest.fixture(scope="session")
156+
def https_server() -> Server:
157+
return Server(SERVER_HOST, port=443)

0 commit comments

Comments
 (0)