Description: Fix unittest to run automatically while building
 Multiple issue were spotted here:
 - Embedded web-server were running on static port 8000 and 8001
   that can conflicts with system apps
 - Pytest was outputting a lots of warning because asyncio marker
   was not documented in setup.cfg
 - Some test had hardcoded text response so they failed if the test
   webserver is running on a different port
 .
Author: Adam Cecile <acecile@le-vert.net>

---
Origin: vendor
Forwarded: no
Last-Update: 2021-08-25

--- a/setup.cfg
+++ b/setup.cfg
@@ -21,6 +21,7 @@ filterwarnings =
   default:::uvicorn
 markers =
   copied_from(source, changes=None): mark test as copied from somewhere else, along with a description of changes made to accodomate e.g. our test setup
+  asyncio: asyncio related tests
   network: marks tests which require network connection. Used in 3rd-party build environments that have network disabled.
 
 [coverage:run]
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -30,7 +30,6 @@ ENVIRONMENT_VARIABLES = {
     "SSLKEYLOGFILE",
 }
 
-
 @pytest.fixture(
     params=[
         pytest.param("asyncio", marks=pytest.mark.asyncio),
@@ -270,7 +269,13 @@ def serve_in_thread(server: Server):
 
 @pytest.fixture(scope="session")
 def server():
-    config = Config(app=app, lifespan="off", loop="asyncio")
+    config = Config(
+        app=app,
+        lifespan="off",
+        loop="asyncio",
+        host="localhost",
+        port=int(os.getenv("HTTPX_TEST_HTTP_PORT", "8000")),
+    )
     server = TestServer(config=config)
     yield from serve_in_thread(server)
 
@@ -282,7 +287,7 @@ def https_server(cert_pem_file, cert_pri
         lifespan="off",
         ssl_certfile=cert_pem_file,
         ssl_keyfile=cert_private_key_file,
-        port=8001,
+        port=int(os.getenv("HTTPX_TEST_HTTPS_PORT", "8001")),
         loop="asyncio",
     )
     server = TestServer(config=config)
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -104,7 +104,7 @@ async def test_logs_debug(server, capsys
             response = await client.get(server.url)
             assert response.status_code == 200
     stderr = capsys.readouterr().err
-    assert 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"' in stderr
+    assert f'HTTP Request: GET {server.url} "HTTP/1.1 200 OK"' in stderr
 
 
 @pytest.mark.asyncio
@@ -114,7 +114,7 @@ async def test_logs_trace(server, capsys
             response = await client.get(server.url)
             assert response.status_code == 200
     stderr = capsys.readouterr().err
-    assert 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"' in stderr
+    assert f'HTTP Request: GET {server.url} "HTTP/1.1 200 OK"' in stderr
 
 
 @pytest.mark.asyncio
@@ -127,11 +127,11 @@ async def test_logs_redirect_chain(serve
     stderr = capsys.readouterr().err.strip()
     redirected_request_line, ok_request_line = stderr.split("\n")
     assert redirected_request_line.endswith(
-        "HTTP Request: GET http://127.0.0.1:8000/redirect_301 "
+        f"HTTP Request: GET {server.url}redirect_301 "
         '"HTTP/1.1 301 Moved Permanently"'
     )
     assert ok_request_line.endswith(
-        'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"'
+        f'HTTP Request: GET {server.url} "HTTP/1.1 200 OK"'
     )
 
 
