1111from ddtrace .pin import Pin
1212from tests .opentracer .utils import init_tracer
1313from tests .utils import TracerTestCase
14+ from tests .utils import snapshot
1415
1516
16- # socket name comes from https://english.stackexchange.com/a/44048
17- SOCKET = "httpbin.org"
17+ # host:port of httpbin_local container
18+ HOST = "localhost"
19+ PORT = 8001
20+ SOCKET = "{}:{}" .format (HOST , PORT )
1821URL_200 = "http://{}/status/200" .format (SOCKET )
1922URL_500 = "http://{}/status/500" .format (SOCKET )
2023
@@ -37,7 +40,7 @@ def tearDown(self):
3740class TestUrllib3 (BaseUrllib3TestCase ):
3841 def test_HTTPConnectionPool_traced (self ):
3942 """Tests that requests made from the HTTPConnectionPool are traced"""
40- pool = urllib3 .connectionpool .HTTPConnectionPool (SOCKET )
43+ pool = urllib3 .connectionpool .HTTPConnectionPool (HOST , PORT )
4144 # Test a relative URL
4245 r = pool .request ("GET" , "/status/200" )
4346 assert r .status == 200
@@ -98,9 +101,9 @@ def test_args_kwargs(self):
98101
99102 for args , kwargs in inputs :
100103
101- with self .override_config ("urllib3" , {}):
104+ with self .override_http_config ("urllib3" , {"_whitelist_headers" : set () }):
102105 config .urllib3 .http .trace_headers (["accept" ])
103- pool = urllib3 .connectionpool .HTTPConnectionPool (SOCKET )
106+ pool = urllib3 .connectionpool .HTTPConnectionPool (HOST , PORT )
104107 out = pool .urlopen (* args , ** kwargs )
105108 assert out .status == 200
106109 spans = self .pop_spans ()
@@ -124,7 +127,7 @@ def test_untraced_request(self):
124127 def test_double_patch (self ):
125128 """Ensure that double patch doesn't duplicate instrumentation"""
126129 patch ()
127- connpool = urllib3 .connectionpool .HTTPConnectionPool (SOCKET )
130+ connpool = urllib3 .connectionpool .HTTPConnectionPool (HOST , PORT )
128131 setattr (connpool , "datadog_tracer" , self .tracer )
129132
130133 out = connpool .urlopen ("GET" , URL_200 )
@@ -209,8 +212,7 @@ def test_default_service_name(self):
209212
210213 def test_user_set_service_name (self ):
211214 """Test the user-set service name is set on the span"""
212- with self .override_config ("urllib3" , dict (split_by_domain = False )):
213- config .urllib3 ["service_name" ] = "clients"
215+ with self .override_config ("urllib3" , dict (split_by_domain = False , service_name = "clients" )):
214216 out = self .http .request ("GET" , URL_200 )
215217 assert out .status == 200
216218 spans = self .pop_spans ()
@@ -312,7 +314,7 @@ def test_request_and_response_headers(self):
312314 assert s .get_tag ("http.response.headers.access-control-allow-origin" ) is None
313315
314316 # Enabled when explicitly configured
315- with self .override_config ("urllib3" , {}):
317+ with self .override_http_config ("urllib3" , {"_whitelist_headers" : set () }):
316318 config .urllib3 .http .trace_headers (["my-header" , "access-control-allow-origin" ])
317319 self .http .request ("GET" , URL_200 , headers = {"my-header" : "my_value" })
318320 spans = self .pop_spans ()
@@ -384,3 +386,24 @@ def test_distributed_tracing_disabled(self):
384386 m_make_request .assert_called_with (
385387 mock .ANY , "GET" , "/status/200" , body = None , chunked = mock .ANY , headers = {}, timeout = mock .ANY
386388 )
389+
390+
391+ @pytest .fixture ()
392+ def patch_urllib3 ():
393+ patch ()
394+ try :
395+ yield
396+ finally :
397+ unpatch ()
398+
399+
400+ @snapshot ()
401+ def test_urllib3_poolmanager_snapshot (patch_urllib3 ):
402+ pool = urllib3 .PoolManager ()
403+ pool .request ("GET" , URL_200 )
404+
405+
406+ @snapshot ()
407+ def test_urllib3_connectionpool_snapshot (patch_urllib3 ):
408+ pool = urllib3 .connectionpool .HTTPConnectionPool (HOST , PORT )
409+ pool .request ("GET" , "/status/200" )
0 commit comments