Skip to content

Commit 6f5025c

Browse files
authored
Merge pull request #338 from DataDog/bmermet/switchtohttpbin
Switch to httpbin for tests
2 parents 039a372 + 6efd51f commit 6f5025c

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

tests/contrib/httplib/test_httplib.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
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
2734
class 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
"""

tests/contrib/requests/test_requests.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,29 @@
88
from ddtrace.ext import http, errors
99
from tests.test_tracer import get_dummy_tracer
1010

11+
# socket name comes from https://english.stackexchange.com/a/44048
12+
SOCKET = 'httpbin.org'
13+
URL_200 = 'http://{}/status/200'.format(SOCKET)
14+
URL_500 = 'http://{}/status/500'.format(SOCKET)
1115

1216
class TestRequests(object):
1317

1418
@staticmethod
1519
def test_resource_path():
1620
tracer, session = get_traced_session()
17-
out = session.get('http://httpstat.us/200')
21+
out = session.get(URL_200)
1822
eq_(out.status_code, 200)
1923
spans = tracer.writer.pop()
2024
eq_(len(spans), 1)
2125
s = spans[0]
22-
eq_(s.get_tag("http.url"), "http://httpstat.us/200")
26+
eq_(s.get_tag("http.url"), URL_200)
2327

2428
@staticmethod
2529
def test_tracer_disabled():
2630
# ensure all valid combinations of args / kwargs work
2731
tracer, session = get_traced_session()
2832
tracer.enabled = False
29-
out = session.get('http://httpstat.us/200')
33+
out = session.get(URL_200)
3034
eq_(out.status_code, 200)
3135
spans = tracer.writer.pop()
3236
eq_(len(spans), 0)
@@ -35,7 +39,7 @@ def test_tracer_disabled():
3539
def test_args_kwargs():
3640
# ensure all valid combinations of args / kwargs work
3741
tracer, session = get_traced_session()
38-
url = 'http://httpstat.us/200'
42+
url = URL_200
3943
method = 'GET'
4044
inputs = [
4145
([], {'method': method, 'url': url}),
@@ -60,7 +64,7 @@ def test_args_kwargs():
6064
@staticmethod
6165
def test_200():
6266
tracer, session = get_traced_session()
63-
out = session.get('http://httpstat.us/200')
67+
out = session.get(URL_200)
6468
eq_(out.status_code, 200)
6569
# validation
6670
spans = tracer.writer.pop()
@@ -74,7 +78,7 @@ def test_200():
7478
@staticmethod
7579
def test_post_500():
7680
tracer, session = get_traced_session()
77-
out = session.post('http://httpstat.us/500')
81+
out = session.post(URL_500)
7882
# validation
7983
eq_(out.status_code, 500)
8084
spans = tracer.writer.pop()
@@ -109,7 +113,7 @@ def test_non_existant_url():
109113
@staticmethod
110114
def test_500():
111115
tracer, session = get_traced_session()
112-
out = session.get('http://httpstat.us/500')
116+
out = session.get(URL_500)
113117
eq_(out.status_code, 500)
114118

115119
spans = tracer.writer.pop()

0 commit comments

Comments
 (0)