]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add test to https server 57/head
authorYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Sat, 11 May 2019 12:24:20 +0000 (13:24 +0100)
committerYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Mon, 13 May 2019 19:33:16 +0000 (20:33 +0100)
Add `https_server` test fixture

requirements.txt
tests/conftest.py
tests/dispatch/test_connections.py

index 59fed558c7615caa5ad06cd6abf3ddfb8958d2b9..520c773970955c51033b2bf8519f67ce137c2930 100644 (file)
@@ -17,4 +17,5 @@ mypy
 pytest
 pytest-asyncio
 pytest-cov
+trustme
 uvicorn
index aab371cf2c2ddf373df1d756d213b20bb061b0a8..cc84fbb4f7639a118b4a38d5a4dad6b0bebbb974 100644 (file)
@@ -1,6 +1,8 @@
 import asyncio
+import os
 
 import pytest
+import trustme
 from uvicorn.config import Config
 from uvicorn.main import Server
 
@@ -62,3 +64,26 @@ async def server():
     finally:
         server.should_exit = True
         await task
+
+
+@pytest.fixture
+async def https_server():
+    ca = trustme.CA()
+    server_cert = ca.issue_cert("example.org")
+    with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:
+        config = Config(
+            app=app,
+            lifespan="off",
+            ssl_certfile=cert_temp_path,
+            ssl_keyfile=key_temp_path,
+            port=8001,
+        )
+    server = Server(config=config)
+    task = asyncio.ensure_future(server.serve())
+    try:
+        while not server.started:
+            await asyncio.sleep(0.0001)
+        yield server
+    finally:
+        server.should_exit = True
+        await task
index a2bac09c4ff60db8907645c34ad4a711065811eb..2edf3ada12ed63773c9da7dc7a8fe059c2b946cd 100644 (file)
@@ -1,6 +1,6 @@
 import pytest
 
-from httpcore import HTTPConnection, Request
+from httpcore import HTTPConnection, Request, SSLConfig
 
 
 @pytest.mark.asyncio
@@ -13,6 +13,14 @@ async def test_get(server):
     assert response.content == b"Hello, world!"
 
 
+@pytest.mark.asyncio
+async def test_https_get(https_server):
+    http = HTTPConnection(origin="https://127.0.0.1:8001/", ssl=SSLConfig(verify=False))
+    response = await http.request("GET", "https://127.0.0.1:8001/")
+    assert response.status_code == 200
+    assert response.content == b"Hello, world!"
+
+
 @pytest.mark.asyncio
 async def test_post(server):
     conn = HTTPConnection(origin="http://127.0.0.1:8000/")