@@ -741,6 +741,10 @@ def create_client(
741741 def create_proxy_transport (self , url : str ):
742742 pass
743743
744+ @abc .abstractmethod
745+ def get_transport_handler (self , transport ):
746+ pass
747+
744748 def setUp (self ):
745749 super ().setUp ()
746750 self .client = self .create_client ()
@@ -763,17 +767,15 @@ def assert_proxy_mounts(self, mounts, num_mounts, transport_type=None):
763767 self .assertEqual (len (mounts ), num_mounts )
764768 for transport in mounts :
765769 with self .subTest (transport ):
770+ if transport is None :
771+ continue
766772 if transport_type :
767773 self .assertIsInstance (
768774 transport ,
769775 transport_type ,
770776 )
771777 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 )
777779 self .assertTrue (
778780 isinstance (handler , ObjectProxy )
779781 and getattr (handler , "__wrapped__" )
@@ -983,6 +985,21 @@ def test_uninstrument_new_client(self):
983985 self .assertEqual (result .text , "Hello!" )
984986 self .assert_span ()
985987
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+
9861003 def test_instrument_proxy (self ):
9871004 proxy_mounts = self .create_proxy_mounts ()
9881005 HTTPXClientInstrumentor ().instrument ()
@@ -994,6 +1011,27 @@ def test_instrument_proxy(self):
9941011 2 ,
9951012 )
9961013
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+
9971035 def test_instrument_client_with_proxy (self ):
9981036 proxy_mounts = self .create_proxy_mounts ()
9991037 client = self .create_client (mounts = proxy_mounts )
@@ -1188,6 +1226,9 @@ def perform_request(
11881226 def create_proxy_transport (self , url ):
11891227 return httpx .HTTPTransport (proxy = httpx .Proxy (url ))
11901228
1229+ def get_transport_handler (self , transport ):
1230+ return getattr (transport , "handle_request" , None )
1231+
11911232 def test_can_instrument_subclassed_client (self ):
11921233 class CustomClient (httpx .Client ):
11931234 pass
@@ -1241,6 +1282,9 @@ async def _perform_request():
12411282 def create_proxy_transport (self , url ):
12421283 return httpx .AsyncHTTPTransport (proxy = httpx .Proxy (url ))
12431284
1285+ def get_transport_handler (self , transport ):
1286+ return getattr (transport , "handle_async_request" , None )
1287+
12441288 def test_basic_multiple (self ):
12451289 # We need to create separate clients because in httpx >= 0.19,
12461290 # closing the client after "with" means the second http call fails
0 commit comments