@@ -741,6 +741,10 @@ def create_client(
741
741
def create_proxy_transport (self , url : str ):
742
742
pass
743
743
744
+ @abc .abstractmethod
745
+ def get_transport_handler (self , transport ):
746
+ pass
747
+
744
748
def setUp (self ):
745
749
super ().setUp ()
746
750
self .client = self .create_client ()
@@ -763,17 +767,15 @@ def assert_proxy_mounts(self, mounts, num_mounts, transport_type=None):
763
767
self .assertEqual (len (mounts ), num_mounts )
764
768
for transport in mounts :
765
769
with self .subTest (transport ):
770
+ if transport is None :
771
+ continue
766
772
if transport_type :
767
773
self .assertIsInstance (
768
774
transport ,
769
775
transport_type ,
770
776
)
771
777
else :
772
- handler = getattr (transport , "handle_request" , None )
773
- if not handler :
774
- handler = getattr (
775
- transport , "handle_async_request"
776
- )
778
+ handler = self .get_transport_handler (transport )
777
779
self .assertTrue (
778
780
isinstance (handler , ObjectProxy )
779
781
and getattr (handler , "__wrapped__" )
@@ -983,6 +985,21 @@ def test_uninstrument_new_client(self):
983
985
self .assertEqual (result .text , "Hello!" )
984
986
self .assert_span ()
985
987
988
+ @mock .patch .dict (
989
+ "os.environ" , {"NO_PROXY" : "http://mock/status/200" }, clear = True
990
+ )
991
+ def test_instrument_with_no_proxy (self ):
992
+ proxy_mounts = self .create_proxy_mounts ()
993
+ HTTPXClientInstrumentor ().instrument ()
994
+ client = self .create_client (mounts = proxy_mounts )
995
+ result = self .perform_request (self .URL , client = client )
996
+ self .assert_span (num_spans = 1 )
997
+ self .assertEqual (result .text , "Hello!" )
998
+ self .assert_proxy_mounts (
999
+ client ._mounts .values (),
1000
+ 3 ,
1001
+ )
1002
+
986
1003
def test_instrument_proxy (self ):
987
1004
proxy_mounts = self .create_proxy_mounts ()
988
1005
HTTPXClientInstrumentor ().instrument ()
@@ -994,6 +1011,27 @@ def test_instrument_proxy(self):
994
1011
2 ,
995
1012
)
996
1013
1014
+ @mock .patch .dict (
1015
+ "os.environ" , {"NO_PROXY" : "http://mock/status/200" }, clear = True
1016
+ )
1017
+ def test_instrument_client_with_no_proxy (self ):
1018
+ proxy_mounts = self .create_proxy_mounts ()
1019
+ client = self .create_client (mounts = proxy_mounts )
1020
+ self .assert_proxy_mounts (
1021
+ client ._mounts .values (),
1022
+ 3 ,
1023
+ (httpx .HTTPTransport , httpx .AsyncHTTPTransport ),
1024
+ )
1025
+ HTTPXClientInstrumentor .instrument_client (client )
1026
+ result = self .perform_request (self .URL , client = client )
1027
+ self .assertEqual (result .text , "Hello!" )
1028
+ self .assert_span (num_spans = 1 )
1029
+ self .assert_proxy_mounts (
1030
+ client ._mounts .values (),
1031
+ 3 ,
1032
+ )
1033
+ HTTPXClientInstrumentor .uninstrument_client (client )
1034
+
997
1035
def test_instrument_client_with_proxy (self ):
998
1036
proxy_mounts = self .create_proxy_mounts ()
999
1037
client = self .create_client (mounts = proxy_mounts )
@@ -1188,6 +1226,9 @@ def perform_request(
1188
1226
def create_proxy_transport (self , url ):
1189
1227
return httpx .HTTPTransport (proxy = httpx .Proxy (url ))
1190
1228
1229
+ def get_transport_handler (self , transport ):
1230
+ return getattr (transport , "handle_request" , None )
1231
+
1191
1232
def test_can_instrument_subclassed_client (self ):
1192
1233
class CustomClient (httpx .Client ):
1193
1234
pass
@@ -1241,6 +1282,9 @@ async def _perform_request():
1241
1282
def create_proxy_transport (self , url ):
1242
1283
return httpx .AsyncHTTPTransport (proxy = httpx .Proxy (url ))
1243
1284
1285
+ def get_transport_handler (self , transport ):
1286
+ return getattr (transport , "handle_async_request" , None )
1287
+
1244
1288
def test_basic_multiple (self ):
1245
1289
# We need to create separate clients because in httpx >= 0.19,
1246
1290
# closing the client after "with" means the second http call fails
0 commit comments