1818import os
1919import pytest
2020import pytest_asyncio
21- from requests .adapters import HTTPAdapter
2221
2322from typing import Sequence , Tuple
2423
@@ -114,18 +113,15 @@ def async_identity(use_mtls, request, event_loop):
114113 )
115114
116115
117- base_dir = os .path .dirname (__file__ )
118- CERT_PATH = os .path .join (base_dir , "../cert/mtls.crt" )
119- KEY_PATH = os .path .join (base_dir , "../cert/mtls.key" )
120- with open (CERT_PATH , "rb" ) as fh :
116+ dir = os .path .dirname (__file__ )
117+ with open (os .path .join (dir , "../cert/mtls.crt" ), "rb" ) as fh :
121118 cert = fh .read ()
122- with open (KEY_PATH , "rb" ) as fh :
119+ with open (os . path . join ( dir , "../cert/mtls.key" ) , "rb" ) as fh :
123120 key = fh .read ()
124121
125122ssl_credentials = grpc .ssl_channel_credentials (
126123 root_certificates = cert , certificate_chain = cert , private_key = key
127124)
128- tls_credentials = grpc .ssl_channel_credentials (root_certificates = cert )
129125
130126
131127def callback ():
@@ -140,9 +136,6 @@ def pytest_addoption(parser):
140136 parser .addoption (
141137 "--mtls" , action = "store_true" , help = "Run system test with mutual TLS channel"
142138 )
143- parser .addoption (
144- "--tls" , action = "store_true" , help = "Run system test with standard one-way TLS channel"
145- )
146139
147140
148141# TODO: Need to test without passing in a transport class
@@ -196,11 +189,6 @@ def use_mtls(request):
196189 return request .config .getoption ("--mtls" )
197190
198191
199- @pytest .fixture
200- def use_tls (request ):
201- return request .config .getoption ("--tls" ) or request .config .getoption ("--mtls" )
202-
203-
204192@pytest .fixture
205193def parametrized_echo (
206194 use_mtls ,
@@ -340,7 +328,7 @@ def _read_response_metadata_stream(self):
340328 def intercept_unary_unary (self , continuation , client_call_details , request ):
341329 self ._add_request_metadata (client_call_details )
342330 response = continuation (client_call_details , request )
343- metadata = [(k , str (v )) for k , v in response .initial_metadata ()] + [( k , str ( v )) for k , v in response . trailing_metadata ()]
331+ metadata = [(k , str (v )) for k , v in response .trailing_metadata ()]
344332 self .response_metadata = metadata
345333 return response
346334
@@ -399,7 +387,7 @@ async def _add_request_metadata(self, client_call_details):
399387 async def intercept_unary_unary (self , continuation , client_call_details , request ):
400388 await self ._add_request_metadata (client_call_details )
401389 response = await continuation (client_call_details , request )
402- metadata = [(k , str (v )) for k , v in await response .initial_metadata ()] + [( k , str ( v )) for k , v in await response . trailing_metadata ()]
390+ metadata = [(k , str (v )) for k , v in await response .trailing_metadata ()]
403391 self .response_metadata = metadata
404392 return response
405393
@@ -424,7 +412,7 @@ async def intercept_stream_stream(
424412
425413
426414@pytest .fixture
427- def intercepted_echo_grpc (use_mtls , use_tls ):
415+ def intercepted_echo_grpc (use_mtls ):
428416 # The interceptor adds 'showcase-trailer' client metadata. Showcase server
429417 # echoes any metadata with key 'showcase-trailer', so the same metadata
430418 # should appear as trailing metadata in the response.
@@ -433,12 +421,11 @@ def intercepted_echo_grpc(use_mtls, use_tls):
433421 "intercepted" ,
434422 )
435423 host = "localhost:7469"
436- if use_mtls :
437- channel = grpc .secure_channel (host , ssl_credentials )
438- elif use_tls :
439- channel = grpc .secure_channel (host , tls_credentials )
440- else :
441- channel = grpc .insecure_channel (host )
424+ channel = (
425+ grpc .secure_channel (host , ssl_credentials )
426+ if use_mtls
427+ else grpc .insecure_channel (host )
428+ )
442429 intercept_channel = grpc .intercept_channel (channel , interceptor )
443430 transport = EchoClient .get_transport_class ("grpc" )(
444431 credentials = ga_credentials .AnonymousCredentials (),
@@ -448,7 +435,7 @@ def intercepted_echo_grpc(use_mtls, use_tls):
448435
449436
450437@pytest_asyncio .fixture
451- async def intercepted_echo_grpc_async (use_mtls , use_tls ):
438+ async def intercepted_echo_grpc_async ():
452439 # The interceptor adds 'showcase-trailer' client metadata. Showcase server
453440 # echoes any metadata with key 'showcase-trailer', so the same metadata
454441 # should appear as trailing metadata in the response.
@@ -457,45 +444,28 @@ async def intercepted_echo_grpc_async(use_mtls, use_tls):
457444 "intercepted" ,
458445 )
459446 host = "localhost:7469"
460- if use_mtls :
461- channel = grpc .aio .secure_channel (host , ssl_credentials , interceptors = [interceptor ])
462- elif use_tls :
463- channel = grpc .aio .secure_channel (host , tls_credentials , interceptors = [interceptor ])
464- else :
465- channel = grpc .aio .insecure_channel (host , interceptors = [interceptor ])
447+ channel = grpc .aio .insecure_channel (host , interceptors = [interceptor ])
448+ # intercept_channel = grpc.aio.intercept_channel(channel, interceptor)
466449 transport = EchoAsyncClient .get_transport_class ("grpc_asyncio" )(
467450 credentials = ga_credentials .AnonymousCredentials (),
468451 channel = channel ,
469452 )
470453 return EchoAsyncClient (transport = transport ), interceptor
471454
472455
473- class HostNameIgnoringAdapter (HTTPAdapter ):
474- """Custom HTTPAdapter that disables hostname verification for local self-signed certs."""
475- def cert_verify (self , conn , url , verify , cert ):
476- super ().cert_verify (conn , url , verify , cert )
477- conn .assert_hostname = False
478-
479-
480456@pytest .fixture
481- def intercepted_echo_rest (use_mtls , use_tls ):
457+ def intercepted_echo_rest ():
482458 transport_name = "rest"
483459 transport_cls = EchoClient .get_transport_class (transport_name )
484460 interceptor = EchoMetadataClientRestInterceptor ()
485461
486- url_scheme = "https" if ( use_mtls or use_tls ) else "http"
462+ # The custom host explicitly bypasses https.
487463 transport = transport_cls (
488464 credentials = ga_credentials .AnonymousCredentials (),
489465 host = "localhost:7469" ,
490- url_scheme = url_scheme ,
466+ url_scheme = "http" ,
491467 interceptor = interceptor ,
492468 )
493- if use_mtls or use_tls :
494- transport ._session .verify = CERT_PATH
495- transport ._session .mount ("https://" , HostNameIgnoringAdapter ())
496- if use_mtls :
497- transport ._session .cert = (CERT_PATH , KEY_PATH )
498-
499469 return EchoClient (transport = transport ), interceptor
500470
501471
@@ -508,11 +478,11 @@ def intercepted_echo_rest_async():
508478 transport_cls = EchoAsyncClient .get_transport_class (transport_name )
509479 interceptor = EchoMetadataClientRestAsyncInterceptor ()
510480
481+ # The custom host explicitly bypasses https.
511482 transport = transport_cls (
512483 credentials = async_anonymous_credentials (),
513484 host = "localhost:7469" ,
514485 url_scheme = "http" ,
515486 interceptor = interceptor ,
516487 )
517-
518488 return EchoAsyncClient (transport = transport ), interceptor
0 commit comments