1616import threading
1717from urllib .request import HTTPError , urlopen
1818
19+ import niquests
1920import daphne .server
2021import pytest
22+ from testing_support .certs import CERT_PATH
2123from testing_support .fixtures import (
2224 override_application_settings ,
2325 raise_background_exceptions ,
2426 wait_for_background_threads ,
2527)
28+ from testing_support .http_23_testing import make_request
2629from testing_support .sample_asgi_applications import (
2730 AppWithCall ,
2831 AppWithCallRaw ,
3841)
3942
4043from newrelic .common .object_names import callable_name
44+ from newrelic .common .package_version_utils import (
45+ get_package_version ,
46+ get_package_version_tuple ,
47+ )
4148
42- DAPHNE_VERSION = tuple ( int ( v ) for v in daphne . __version__ . split ( "." )[: 2 ] )
49+ DAPHNE_VERSION = get_package_version_tuple ( " daphne" )
4350skip_asgi_3_unsupported = pytest .mark .skipif (DAPHNE_VERSION < (3 , 0 ), reason = "ASGI3 unsupported" )
4451skip_asgi_2_unsupported = pytest .mark .skipif (DAPHNE_VERSION >= (3 , 0 ), reason = "ASGI2 unsupported" )
4552
@@ -98,7 +105,7 @@ async def fake_app(*args, **kwargs):
98105
99106 server = daphne .server .Server (
100107 fake_app ,
101- endpoints = [f"tcp :{ port } :interface=127.0.0.1" ],
108+ endpoints = [f"ssl :{ port } :privateKey= { CERT_PATH } :certKey= { CERT_PATH } :interface=127.0.0.1" ],
102109 ready_callable = on_ready ,
103110 signal_handlers = False ,
104111 verbosity = 9 ,
@@ -119,32 +126,35 @@ async def fake_app(*args, **kwargs):
119126 raise RuntimeError ("Thread failed to exit in time." )
120127
121128
129+ @pytest .mark .parametrize ("http_version" , [1 , 2 ], ids = ["HTTP/1" , "HTTP/2" ])
122130@override_application_settings ({"transaction_name.naming_scheme" : "framework" })
123- def test_daphne_200 (port , app ):
131+ def test_daphne_200 (port , app , http_version ):
132+ daphne_version = get_package_version ("daphne" )
133+ assert daphne_version is not None
134+
124135 @validate_transaction_metrics (
125136 callable_name (app ),
126137 custom_metrics = [
127- (f"Python/Dispatcher/Daphne/{ daphne . __version__ } " , 1 ),
138+ (f"Python/Dispatcher/Daphne/{ daphne_version } " , 1 ),
128139 ],
129140 )
130141 @raise_background_exceptions ()
131142 @wait_for_background_threads ()
132143 def response ():
133- return urlopen ( f"http:// localhost: { port } " , timeout = 10 ) # nosec
144+ return make_request ( host = " localhost" , port = port , path = "/" , http_version = http_version , timeout = 10 )
134145
135- assert response ().status == 200
146+ response ().raise_for_status ()
136147
137148
149+ @pytest .mark .parametrize ("http_version" , [1 , 2 ], ids = ["HTTP/1" , "HTTP/2" ])
138150@override_application_settings ({"transaction_name.naming_scheme" : "framework" })
139- @ validate_transaction_errors ([ "builtins:ValueError" ])
140- def test_daphne_500 ( port , app ):
151+ def test_daphne_500 ( port , app , http_version ):
152+ @ validate_transaction_errors ([ "builtins:ValueError" ])
141153 @validate_transaction_metrics (callable_name (app ))
142154 @raise_background_exceptions ()
143155 @wait_for_background_threads ()
144156 def _test ():
145- try :
146- urlopen (f"http://localhost:{ port } /exc" ) # nosec
147- except HTTPError :
148- pass
157+ with pytest .raises (niquests .exceptions .HTTPError ):
158+ make_request (host = "localhost" , port = port , path = "/exc" , http_version = http_version , timeout = 10 )
149159
150160 _test ()
0 commit comments