@@ -116,14 +116,17 @@ def async_identity(use_mtls, request, event_loop):
116116
117117
118118base_dir = os .path .dirname (__file__ )
119- with open (os .path .join (base_dir , "../cert/mtls.crt" ), "rb" ) as fh :
119+ CERT_PATH = os .path .join (base_dir , "../cert/mtls.crt" )
120+ KEY_PATH = os .path .join (base_dir , "../cert/mtls.key" )
121+ with open (CERT_PATH , "rb" ) as fh :
120122 cert = fh .read ()
121- with open (os . path . join ( base_dir , "../cert/mtls.key" ) , "rb" ) as fh :
123+ with open (KEY_PATH , "rb" ) as fh :
122124 key = fh .read ()
123125
124126ssl_credentials = grpc .ssl_channel_credentials (
125127 root_certificates = cert , certificate_chain = cert , private_key = key
126128)
129+ tls_credentials = grpc .ssl_channel_credentials (root_certificates = cert )
127130
128131
129132def callback ():
@@ -138,6 +141,9 @@ def pytest_addoption(parser):
138141 parser .addoption (
139142 "--mtls" , action = "store_true" , help = "Run system test with mutual TLS channel"
140143 )
144+ parser .addoption (
145+ "--tls" , action = "store_true" , help = "Run system test with standard one-way TLS channel"
146+ )
141147
142148
143149# TODO: Need to test without passing in a transport class
@@ -191,6 +197,11 @@ def use_mtls(request):
191197 return request .config .getoption ("--mtls" )
192198
193199
200+ @pytest .fixture
201+ def use_tls (request ):
202+ return request .config .getoption ("--tls" )
203+
204+
194205@pytest .fixture
195206def parametrized_echo (
196207 use_mtls ,
@@ -414,7 +425,7 @@ async def intercept_stream_stream(
414425
415426
416427@pytest .fixture
417- def intercepted_echo_grpc (use_mtls ):
428+ def intercepted_echo_grpc (use_mtls , use_tls ):
418429 # The interceptor adds 'showcase-trailer' client metadata. Showcase server
419430 # echoes any metadata with key 'showcase-trailer', so the same metadata
420431 # should appear as trailing metadata in the response.
@@ -423,11 +434,12 @@ def intercepted_echo_grpc(use_mtls):
423434 "intercepted" ,
424435 )
425436 host = "localhost:7469"
426- channel = (
427- grpc .secure_channel (host , ssl_credentials )
428- if use_mtls
429- else grpc .insecure_channel (host )
430- )
437+ if use_mtls :
438+ channel = grpc .secure_channel (host , ssl_credentials )
439+ elif use_tls :
440+ channel = grpc .secure_channel (host , tls_credentials )
441+ else :
442+ channel = grpc .insecure_channel (host )
431443 intercept_channel = grpc .intercept_channel (channel , interceptor )
432444 transport = EchoClient .get_transport_class ("grpc" )(
433445 credentials = ga_credentials .AnonymousCredentials (),
@@ -468,51 +480,41 @@ def init_poolmanager(self, connections, maxsize, block=False, **pool_kwargs):
468480
469481
470482@pytest .fixture
471- def intercepted_echo_rest (use_mtls ):
483+ def intercepted_echo_rest (use_mtls , use_tls ):
472484 transport_name = "rest"
473485 transport_cls = EchoClient .get_transport_class (transport_name )
474486 interceptor = EchoMetadataClientRestInterceptor ()
475487
476- url_scheme = "https" if use_mtls else "http"
488+ url_scheme = "https" if ( use_mtls or use_tls ) else "http"
477489 transport = transport_cls (
478490 credentials = ga_credentials .AnonymousCredentials (),
479491 host = "localhost:7469" ,
480492 url_scheme = url_scheme ,
481493 interceptor = interceptor ,
482494 )
483- if use_mtls :
484- base_dir = os .path .dirname (__file__ )
485- cert_path = os .path .join (base_dir , "../cert/mtls.crt" )
486- key_path = os .path .join (base_dir , "../cert/mtls.key" )
487- transport ._session .verify = cert_path
488- transport ._session .cert = (cert_path , key_path )
495+ if use_mtls or use_tls :
496+ transport ._session .verify = CERT_PATH
489497 transport ._session .mount ("https://" , HostNameIgnoringAdapter ())
498+ if use_mtls :
499+ transport ._session .cert = (CERT_PATH , KEY_PATH )
490500
491501 return EchoClient (transport = transport ), interceptor
492502
493503
494504@pytest .fixture
495- def intercepted_echo_rest_async (use_mtls ):
505+ def intercepted_echo_rest_async ():
496506 if not HAS_ASYNC_REST_ECHO_TRANSPORT :
497507 pytest .skip ("Skipping test with async rest." )
498508
499509 transport_name = "rest_asyncio"
500510 transport_cls = EchoAsyncClient .get_transport_class (transport_name )
501511 interceptor = EchoMetadataClientRestAsyncInterceptor ()
502512
503- url_scheme = "https" if use_mtls else "http"
504513 transport = transport_cls (
505514 credentials = async_anonymous_credentials (),
506515 host = "localhost:7469" ,
507- url_scheme = url_scheme ,
516+ url_scheme = "http" ,
508517 interceptor = interceptor ,
509518 )
510- if use_mtls :
511- base_dir = os .path .dirname (__file__ )
512- cert_path = os .path .join (base_dir , "../cert/mtls.crt" )
513- key_path = os .path .join (base_dir , "../cert/mtls.key" )
514- transport ._session .verify = cert_path
515- transport ._session .cert = (cert_path , key_path )
516- transport ._session .mount ("https://" , HostNameIgnoringAdapter ())
517519
518520 return EchoAsyncClient (transport = transport ), interceptor
0 commit comments