1515import asyncio
1616import threading
1717import time
18- from urllib .request import HTTPError , urlopen
1918
19+ import niquests
2020import pytest
21+ from testing_support .certs import CERT_PATH
2122from testing_support .fixtures import (
2223 override_application_settings ,
2324 raise_background_exceptions ,
2425 wait_for_background_threads ,
2526)
27+ from testing_support .http_23_testing import make_request
2628from testing_support .sample_asgi_applications import (
2729 AppWithCall ,
2830 AppWithCallRaw ,
@@ -97,7 +99,9 @@ async def shutdown_trigger():
9799
98100 config = hypercorn .config .Config .from_mapping (
99101 {
100- "bind" : [f"127.0.0.1:{ port } " ],
102+ "bind" : [f"localhost:{ port } " ],
103+ "certfile" : CERT_PATH ,
104+ "keyfile" : CERT_PATH ,
101105 }
102106 )
103107
@@ -123,7 +127,7 @@ def wait_for_port(port, retries=10):
123127 status = None
124128 for _ in range (retries ):
125129 try :
126- status = urlopen ( f"http:// localhost: { port } /ignored" , timeout = 1 ).status # nosec
130+ status = make_request ( host = " localhost" , port = port , path = " /ignored" , timeout = 1 ).status_code
127131 assert status == 200
128132 return
129133 except Exception as e :
@@ -134,8 +138,9 @@ def wait_for_port(port, retries=10):
134138 raise RuntimeError (f"Failed to wait for port { port } . Got status { status } " )
135139
136140
141+ @pytest .mark .parametrize ("http_version" , [1 , 2 ], ids = ["HTTP/1" , "HTTP/2" ])
137142@override_application_settings ({"transaction_name.naming_scheme" : "framework" })
138- def test_hypercorn_200 (port , app ):
143+ def test_hypercorn_200 (port , app , http_version ):
139144 hypercorn_version = get_package_version ("hypercorn" )
140145
141146 @validate_transaction_metrics (
@@ -147,19 +152,20 @@ def test_hypercorn_200(port, app):
147152 @raise_background_exceptions ()
148153 @wait_for_background_threads ()
149154 def response ():
150- return urlopen ( f"http:// localhost: { port } " , timeout = 10 ) # nosec
155+ return make_request ( host = " localhost" , port = port , path = "/" , http_version = http_version , timeout = 10 )
151156
152- assert response ().status == 200
157+ response ().raise_for_status ()
153158
154159
160+ @pytest .mark .parametrize ("http_version" , [1 , 2 ], ids = ["HTTP/1" , "HTTP/2" ])
155161@override_application_settings ({"transaction_name.naming_scheme" : "framework" })
156- def test_hypercorn_500 (port , app ):
162+ def test_hypercorn_500 (port , app , http_version ):
157163 @validate_transaction_errors (["builtins:ValueError" ])
158164 @validate_transaction_metrics (callable_name (app ))
159165 @raise_background_exceptions ()
160166 @wait_for_background_threads ()
161167 def _test ():
162- with pytest .raises (HTTPError ):
163- urlopen ( f"http:// localhost: { port } /exc") # nosec
168+ with pytest .raises (niquests . exceptions . HTTPError ):
169+ make_request ( host = " localhost" , port = port , path = " /exc", http_version = http_version , timeout = 10 )
164170
165171 _test ()
0 commit comments