1414
1515import asyncio
1616import threading
17- from urllib .request import HTTPError , urlopen
1817
1918import daphne .server
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 ,
3840)
3941
4042from newrelic .common .object_names import callable_name
43+ from newrelic .common .package_version_utils import (
44+ get_package_version ,
45+ get_package_version_tuple ,
46+ )
4147
42- DAPHNE_VERSION = tuple ( int ( v ) for v in daphne . __version__ . split ( "." )[: 2 ] )
48+ DAPHNE_VERSION = get_package_version_tuple ( " daphne" )
4349skip_asgi_3_unsupported = pytest .mark .skipif (DAPHNE_VERSION < (3 , 0 ), reason = "ASGI3 unsupported" )
4450skip_asgi_2_unsupported = pytest .mark .skipif (DAPHNE_VERSION >= (3 , 0 ), reason = "ASGI2 unsupported" )
4551
@@ -98,7 +104,7 @@ async def fake_app(*args, **kwargs):
98104
99105 server = daphne .server .Server (
100106 fake_app ,
101- endpoints = [f"tcp :{ port } :interface=127.0.0.1" ],
107+ endpoints = [f"ssl :{ port } :privateKey= { CERT_PATH } :certKey= { CERT_PATH } :interface=127.0.0.1" ],
102108 ready_callable = on_ready ,
103109 signal_handlers = False ,
104110 verbosity = 9 ,
@@ -119,32 +125,35 @@ async def fake_app(*args, **kwargs):
119125 raise RuntimeError ("Thread failed to exit in time." )
120126
121127
128+ @pytest .mark .parametrize ("http_version" , [1 , 2 ], ids = ["HTTP/1" , "HTTP/2" ])
122129@override_application_settings ({"transaction_name.naming_scheme" : "framework" })
123- def test_daphne_200 (port , app ):
130+ def test_daphne_200 (port , app , http_version ):
131+ daphne_version = get_package_version ("daphne" )
132+ assert daphne_version is not None
133+
124134 @validate_transaction_metrics (
125135 callable_name (app ),
126136 custom_metrics = [
127- (f"Python/Dispatcher/Daphne/{ daphne . __version__ } " , 1 ),
137+ (f"Python/Dispatcher/Daphne/{ daphne_version } " , 1 ),
128138 ],
129139 )
130140 @raise_background_exceptions ()
131141 @wait_for_background_threads ()
132142 def response ():
133- return urlopen ( f"http:// localhost: { port } " , timeout = 10 ) # nosec
143+ return make_request ( host = " localhost" , port = port , path = "/" , http_version = http_version , timeout = 10 )
134144
135- assert response ().status == 200
145+ response ().raise_for_status ()
136146
137147
148+ @pytest .mark .parametrize ("http_version" , [1 , 2 ], ids = ["HTTP/1" , "HTTP/2" ])
138149@override_application_settings ({"transaction_name.naming_scheme" : "framework" })
139- @ validate_transaction_errors ([ "builtins:ValueError" ])
140- def test_daphne_500 ( port , app ):
150+ def test_daphne_500 ( port , app , http_version ):
151+ @ validate_transaction_errors ([ "builtins:ValueError" ])
141152 @validate_transaction_metrics (callable_name (app ))
142153 @raise_background_exceptions ()
143154 @wait_for_background_threads ()
144155 def _test ():
145- try :
146- urlopen (f"http://localhost:{ port } /exc" ) # nosec
147- except HTTPError :
148- pass
156+ with pytest .raises (niquests .exceptions .HTTPError ):
157+ make_request (host = "localhost" , port = port , path = "/exc" , http_version = http_version , timeout = 10 )
149158
150159 _test ()
0 commit comments