1818# Standard
1919from copy import deepcopy
2020from dataclasses import asdict
21- from typing import Any , Dict , Optional , Union
21+ from typing import Any , Dict , Optional , Sequence , Tuple , Union
2222from unittest import mock
2323import os
2424import tempfile
@@ -97,17 +97,60 @@ def mock_tgis_fixture():
9797 mock_tgis .stop ()
9898
9999
100- class TestServicerContext :
100+ class TestServicerContext ( grpc . ServicerContext ) :
101101 """
102102 A dummy class for mimicking ServicerContext invocation metadata storage.
103103 """
104104
105105 def __init__ (self , metadata : Dict [str , Union [str , bytes ]]):
106106 self .metadata = metadata
107107
108- def invocation_metadata (self ):
108+ def invocation_metadata (self ) -> Sequence [Tuple [str , Union [str , bytes ]]]:
109+ # https://grpc.github.io/grpc/python/glossary.html#term-metadata
109110 return list (self .metadata .items ())
110111
112+ def is_active (self ):
113+ raise NotImplementedError
114+
115+ def time_remaining (self ):
116+ raise NotImplementedError
117+
118+ def cancel (self ):
119+ raise NotImplementedError
120+
121+ def add_callback (self , callback ):
122+ raise NotImplementedError
123+
124+ def peer (self ):
125+ raise NotImplementedError
126+
127+ def peer_identities (self ):
128+ raise NotImplementedError
129+
130+ def peer_identity_key (self ):
131+ raise NotImplementedError
132+
133+ def auth_context (self ):
134+ raise NotImplementedError
135+
136+ def send_initial_metadata (self , initial_metadata ):
137+ raise NotImplementedError
138+
139+ def set_trailing_metadata (self , trailing_metadata ):
140+ raise NotImplementedError
141+
142+ def abort (self , code , details ):
143+ raise NotImplementedError
144+
145+ def abort_with_status (self , status ):
146+ raise NotImplementedError
147+
148+ def set_code (self , code ):
149+ raise NotImplementedError
150+
151+ def set_details (self , details ):
152+ raise NotImplementedError
153+
111154
112155## Conn Config #################################################################
113156
@@ -927,34 +970,84 @@ def test_tgis_backend_conn_testing_enabled(tgis_mock_insecure):
927970 {
928971 "type" : "http" ,
929972 "headers" : [
930- (TGISBackend .ROUTE_INFO_HEADER_KEY .encode (), b"sometext" )
973+ (
974+ TGISBackend .ROUTE_INFO_HEADER_KEY .encode ("latin-1" ),
975+ "http exact" .encode ("latin-1" ),
976+ )
977+ ],
978+ }
979+ ),
980+ "http exact" ,
981+ ),
982+ (
983+ fastapi .Request (
984+ {
985+ "type" : "http" ,
986+ "headers" : [
987+ (
988+ TGISBackend .ROUTE_INFO_HEADER_KEY .upper ().encode ("latin-1" ),
989+ "http upper-case" .encode ("latin-1" ),
990+ )
931991 ],
932992 }
933993 ),
934- "sometext " ,
994+ "http upper-case " ,
935995 ),
936996 (
937997 fastapi .Request (
938- {"type" : "http" , "headers" : [(b"route-info" , b"sometext" )]}
998+ {
999+ "type" : "http" ,
1000+ "headers" : [
1001+ (
1002+ TGISBackend .ROUTE_INFO_HEADER_KEY .title ().encode ("latin-1" ),
1003+ "http title-case" .encode ("latin-1" ),
1004+ )
1005+ ],
1006+ }
1007+ ),
1008+ "http title-case" ,
1009+ ),
1010+ (
1011+ fastapi .Request (
1012+ {
1013+ "type" : "http" ,
1014+ "headers" : [
1015+ (
1016+ "route-info" .encode ("latin-1" ),
1017+ "http not-found" .encode ("latin-1" ),
1018+ )
1019+ ],
1020+ }
9391021 ),
9401022 None ,
9411023 ),
9421024 (
943- TestServicerContext ({TGISBackend .ROUTE_INFO_HEADER_KEY : "sometext " }),
944- "sometext " ,
1025+ TestServicerContext ({TGISBackend .ROUTE_INFO_HEADER_KEY : "grpc exact " }),
1026+ "grpc exact " ,
9451027 ),
9461028 (
947- TestServicerContext ({"route-info" : "sometext" }),
1029+ TestServicerContext (
1030+ {TGISBackend .ROUTE_INFO_HEADER_KEY .upper (): "grpc upper-case" }
1031+ ),
1032+ "grpc upper-case" ,
1033+ ),
1034+ (
1035+ TestServicerContext (
1036+ {TGISBackend .ROUTE_INFO_HEADER_KEY .title (): "grpc title-case" }
1037+ ),
1038+ "grpc title-case" ,
1039+ ),
1040+ (
1041+ TestServicerContext ({"route-info" : "grpc not found" }),
9481042 None ,
9491043 ),
950- ("should raise TypeError" , None ),
1044+ ("should raise TypeError" , TypeError () ),
9511045 (None , None ),
952- # Uncertain how to create a grpc.ServicerContext object
9531046 ],
9541047)
955- def test_get_route_info (context , route_info : Optional [str ]):
956- if not isinstance (context , ( fastapi . Request , grpc . ServicerContext , type ( None )) ):
957- with pytest .raises (TypeError ):
1048+ def test_get_route_info (context , route_info : Union [str , None , Exception ]):
1049+ if isinstance (route_info , Exception ):
1050+ with pytest .raises (type ( route_info ) ):
9581051 TGISBackend .get_route_info (context )
9591052 else :
9601053 actual_route_info = TGISBackend .get_route_info (context )
@@ -970,7 +1063,10 @@ def test_handle_runtime_context_with_route_info():
9701063 {
9711064 "type" : "http" ,
9721065 "headers" : [
973- (TGISBackend .ROUTE_INFO_HEADER_KEY .encode (), route_info .encode ("utf-8" ))
1066+ (
1067+ TGISBackend .ROUTE_INFO_HEADER_KEY .encode ("latin-1" ),
1068+ route_info .encode ("latin-1" ),
1069+ )
9741070 ],
9751071 }
9761072 )
0 commit comments