2323 from urllib .request import urlopen , build_opener , Request
2424
2525
26+ # socket name comes from https://english.stackexchange.com/a/44048
27+ SOCKET = 'httpbin.org'
28+ URL_200 = 'http://{}/status/200' .format (SOCKET )
29+ URL_500 = 'http://{}/status/500' .format (SOCKET )
30+ URL_404 = 'http://{}/status/404' .format (SOCKET )
31+
32+
2633# Base test mixin for shared tests between Py2 and Py3
2734class HTTPLibBaseMixin (object ):
2835 SPAN_NAME = 'httplib.request' if PY2 else 'http.client.request'
@@ -101,13 +108,13 @@ def test_should_skip_request(self):
101108 """
102109 # Enabled Pin and non-internal request
103110 self .tracer .enabled = True
104- request = self .get_http_connection ('httpstat.us' )
111+ request = self .get_http_connection (SOCKET )
105112 pin = Pin .get_from (request )
106113 self .assertFalse (should_skip_request (pin , request ))
107114
108115 # Disabled Pin and non-internal request
109116 self .tracer .enabled = False
110- request = self .get_http_connection ('httpstat.us' )
117+ request = self .get_http_connection (SOCKET )
111118 pin = Pin .get_from (request )
112119 self .assertTrue (should_skip_request (pin , request ))
113120
@@ -129,11 +136,11 @@ def test_httplib_request_get_request(self):
129136 we return the original response
130137 we capture a span for the request
131138 """
132- conn = self .get_http_connection ('httpstat.us' )
139+ conn = self .get_http_connection (SOCKET )
133140 with contextlib .closing (conn ):
134- conn .request ('GET' , '/200' )
141+ conn .request ('GET' , '/status/ 200' )
135142 resp = conn .getresponse ()
136- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
143+ self .assertEqual (self .to_str (resp .read ()), '' )
137144 self .assertEqual (resp .status , 200 )
138145
139146 spans = self .tracer .writer .pop ()
@@ -148,7 +155,7 @@ def test_httplib_request_get_request(self):
148155 {
149156 'http.method' : 'GET' ,
150157 'http.status_code' : '200' ,
151- 'http.url' : 'http://httpstat.us/200' ,
158+ 'http.url' : URL_200 ,
152159 }
153160 )
154161
@@ -188,11 +195,11 @@ def test_httplib_request_post_request(self):
188195 we return the original response
189196 we capture a span for the request
190197 """
191- conn = self .get_http_connection ('httpstat.us' )
198+ conn = self .get_http_connection (SOCKET )
192199 with contextlib .closing (conn ):
193- conn .request ('POST' , '/200' , body = 'key=value' )
200+ conn .request ('POST' , '/status/ 200' , body = 'key=value' )
194201 resp = conn .getresponse ()
195- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
202+ self .assertEqual (self .to_str (resp .read ()), '' )
196203 self .assertEqual (resp .status , 200 )
197204
198205 spans = self .tracer .writer .pop ()
@@ -207,7 +214,7 @@ def test_httplib_request_post_request(self):
207214 {
208215 'http.method' : 'POST' ,
209216 'http.status_code' : '200' ,
210- 'http.url' : 'http://httpstat.us/200' ,
217+ 'http.url' : URL_200 ,
211218 }
212219 )
213220
@@ -216,11 +223,11 @@ def test_httplib_request_get_request_query_string(self):
216223 When making a GET request with a query string via httplib.HTTPConnection.request
217224 we capture a the entire url in the span
218225 """
219- conn = self .get_http_connection ('httpstat.us' )
226+ conn = self .get_http_connection (SOCKET )
220227 with contextlib .closing (conn ):
221- conn .request ('GET' , '/200?key=value&key2=value2' )
228+ conn .request ('GET' , '/status/ 200?key=value&key2=value2' )
222229 resp = conn .getresponse ()
223- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
230+ self .assertEqual (self .to_str (resp .read ()), '' )
224231 self .assertEqual (resp .status , 200 )
225232
226233 spans = self .tracer .writer .pop ()
@@ -235,7 +242,7 @@ def test_httplib_request_get_request_query_string(self):
235242 {
236243 'http.method' : 'GET' ,
237244 'http.status_code' : '200' ,
238- 'http.url' : 'http://httpstat.us/200 ?key=value&key2=value2' ,
245+ 'http.url' : '{} ?key=value&key2=value2' . format ( URL_200 ) ,
239246 }
240247 )
241248
@@ -248,9 +255,9 @@ def test_httplib_request_500_request(self):
248255 we capture the correct span tags
249256 """
250257 try :
251- conn = self .get_http_connection ('httpstat.us' )
258+ conn = self .get_http_connection (SOCKET )
252259 with contextlib .closing (conn ):
253- conn .request ('GET' , '/500' )
260+ conn .request ('GET' , '/status/ 500' )
254261 conn .getresponse ()
255262 except httplib .HTTPException :
256263 resp = sys .exc_info ()[1 ]
@@ -266,7 +273,7 @@ def test_httplib_request_500_request(self):
266273 self .assertEqual (span .error , 1 )
267274 self .assertEqual (span .get_tag ('http.method' ), 'GET' )
268275 self .assertEqual (span .get_tag ('http.status_code' ), '500' )
269- self .assertEqual (span .get_tag ('http.url' ), 'http://httpstat.us/500' )
276+ self .assertEqual (span .get_tag ('http.url' ), URL_500 )
270277
271278 def test_httplib_request_non_200_request (self ):
272279 """
@@ -277,9 +284,9 @@ def test_httplib_request_non_200_request(self):
277284 we capture the correct span tags
278285 """
279286 try :
280- conn = self .get_http_connection ('httpstat.us' )
287+ conn = self .get_http_connection (SOCKET )
281288 with contextlib .closing (conn ):
282- conn .request ('GET' , '/404' )
289+ conn .request ('GET' , '/status/ 404' )
283290 conn .getresponse ()
284291 except httplib .HTTPException :
285292 resp = sys .exc_info ()[1 ]
@@ -295,7 +302,7 @@ def test_httplib_request_non_200_request(self):
295302 self .assertEqual (span .error , 0 )
296303 self .assertEqual (span .get_tag ('http.method' ), 'GET' )
297304 self .assertEqual (span .get_tag ('http.status_code' ), '404' )
298- self .assertEqual (span .get_tag ('http.url' ), 'http://httpstat.us/404' )
305+ self .assertEqual (span .get_tag ('http.url' ), URL_404 )
299306
300307 def test_httplib_request_get_request_disabled (self ):
301308 """
@@ -304,11 +311,11 @@ def test_httplib_request_get_request_disabled(self):
304311 we do not capture any spans
305312 """
306313 self .tracer .enabled = False
307- conn = self .get_http_connection ('httpstat.us' )
314+ conn = self .get_http_connection (SOCKET )
308315 with contextlib .closing (conn ):
309- conn .request ('GET' , '/200' )
316+ conn .request ('GET' , '/status/ 200' )
310317 resp = conn .getresponse ()
311- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
318+ self .assertEqual (self .to_str (resp .read ()), '' )
312319 self .assertEqual (resp .status , 200 )
313320
314321 spans = self .tracer .writer .pop ()
@@ -321,9 +328,9 @@ def test_urllib_request(self):
321328 we capture a span for the request
322329 """
323330 with override_global_tracer (self .tracer ):
324- resp = urlopen ('http://httpstat.us/200' )
331+ resp = urlopen (URL_200 )
325332
326- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
333+ self .assertEqual (self .to_str (resp .read ()), '' )
327334 self .assertEqual (resp .getcode (), 200 )
328335
329336 spans = self .tracer .writer .pop ()
@@ -335,7 +342,7 @@ def test_urllib_request(self):
335342 self .assertEqual (span .error , 0 )
336343 self .assertEqual (span .get_tag ('http.method' ), 'GET' )
337344 self .assertEqual (span .get_tag ('http.status_code' ), '200' )
338- self .assertEqual (span .get_tag ('http.url' ), 'http://httpstat.us/200' )
345+ self .assertEqual (span .get_tag ('http.url' ), URL_200 )
339346
340347 def test_urllib_request_https (self ):
341348 """
@@ -368,11 +375,11 @@ def test_urllib_request_object(self):
368375 we return the original response
369376 we capture a span for the request
370377 """
371- req = Request ('http://httpstat.us/200' )
378+ req = Request (URL_200 )
372379 with override_global_tracer (self .tracer ):
373380 resp = urlopen (req )
374381
375- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
382+ self .assertEqual (self .to_str (resp .read ()), '' )
376383 self .assertEqual (resp .getcode (), 200 )
377384
378385 spans = self .tracer .writer .pop ()
@@ -384,7 +391,7 @@ def test_urllib_request_object(self):
384391 self .assertEqual (span .error , 0 )
385392 self .assertEqual (span .get_tag ('http.method' ), 'GET' )
386393 self .assertEqual (span .get_tag ('http.status_code' ), '200' )
387- self .assertEqual (span .get_tag ('http.url' ), 'http://httpstat.us/200' )
394+ self .assertEqual (span .get_tag ('http.url' ), URL_200 )
388395
389396 def test_urllib_request_opener (self ):
390397 """
@@ -394,9 +401,9 @@ def test_urllib_request_opener(self):
394401 """
395402 opener = build_opener ()
396403 with override_global_tracer (self .tracer ):
397- resp = opener .open ('http://httpstat.us/200' )
404+ resp = opener .open (URL_200 )
398405
399- self .assertEqual (self .to_str (resp .read ()), '200 OK ' )
406+ self .assertEqual (self .to_str (resp .read ()), '' )
400407 self .assertEqual (resp .getcode (), 200 )
401408
402409 spans = self .tracer .writer .pop ()
@@ -408,7 +415,7 @@ def test_urllib_request_opener(self):
408415 self .assertEqual (span .error , 0 )
409416 self .assertEqual (span .get_tag ('http.method' ), 'GET' )
410417 self .assertEqual (span .get_tag ('http.status_code' ), '200' )
411- self .assertEqual (span .get_tag ('http.url' ), 'http://httpstat.us/200' )
418+ self .assertEqual (span .get_tag ('http.url' ), URL_200 )
412419
413420
414421# Additional Python2 test cases for urllib
@@ -423,9 +430,9 @@ def test_urllib_request(self):
423430 we capture a span for the request
424431 """
425432 with override_global_tracer (self .tracer ):
426- resp = urllib .urlopen ('http://httpstat.us/200' )
433+ resp = urllib .urlopen (URL_200 )
427434
428- self .assertEqual (resp .read (), '200 OK ' )
435+ self .assertEqual (resp .read (), '' )
429436 self .assertEqual (resp .getcode (), 200 )
430437
431438 spans = self .tracer .writer .pop ()
@@ -437,7 +444,7 @@ def test_urllib_request(self):
437444 self .assertEqual (span .error , 0 )
438445 self .assertEqual (span .get_tag ('http.method' ), 'GET' )
439446 self .assertEqual (span .get_tag ('http.status_code' ), '200' )
440- self .assertEqual (span .get_tag ('http.url' ), 'http://httpstat.us/200' )
447+ self .assertEqual (span .get_tag ('http.url' ), URL_200 )
441448
442449 def test_urllib_request_https (self ):
443450 """
0 commit comments