Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions tests/contrib/httplib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
from urllib.request import urlopen, build_opener, Request


# socket name comes from https://english.stackexchange.com/a/44048
SOCKET = 'httpbin.org'
URL_200 = 'http://{}/status/200'.format(SOCKET)
URL_500 = 'http://{}/status/500'.format(SOCKET)
URL_404 = 'http://{}/status/404'.format(SOCKET)


# Base test mixin for shared tests between Py2 and Py3
class HTTPLibBaseMixin(object):
SPAN_NAME = 'httplib.request' if PY2 else 'http.client.request'
Expand Down Expand Up @@ -101,13 +108,13 @@ def test_should_skip_request(self):
"""
# Enabled Pin and non-internal request
self.tracer.enabled = True
request = self.get_http_connection('httpstat.us')
request = self.get_http_connection(SOCKET)
pin = Pin.get_from(request)
self.assertFalse(should_skip_request(pin, request))

# Disabled Pin and non-internal request
self.tracer.enabled = False
request = self.get_http_connection('httpstat.us')
request = self.get_http_connection(SOCKET)
pin = Pin.get_from(request)
self.assertTrue(should_skip_request(pin, request))

Expand All @@ -129,11 +136,11 @@ def test_httplib_request_get_request(self):
we return the original response
we capture a span for the request
"""
conn = self.get_http_connection('httpstat.us')
conn = self.get_http_connection(SOCKET)
with contextlib.closing(conn):
conn.request('GET', '/200')
conn.request('GET', '/status/200')
resp = conn.getresponse()
self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.status, 200)

spans = self.tracer.writer.pop()
Expand All @@ -148,7 +155,7 @@ def test_httplib_request_get_request(self):
{
'http.method': 'GET',
'http.status_code': '200',
'http.url': 'http://httpstat.us/200',
'http.url': URL_200,
}
)

Expand Down Expand Up @@ -188,11 +195,11 @@ def test_httplib_request_post_request(self):
we return the original response
we capture a span for the request
"""
conn = self.get_http_connection('httpstat.us')
conn = self.get_http_connection(SOCKET)
with contextlib.closing(conn):
conn.request('POST', '/200', body='key=value')
conn.request('POST', '/status/200', body='key=value')
resp = conn.getresponse()
self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.status, 200)

spans = self.tracer.writer.pop()
Expand All @@ -207,7 +214,7 @@ def test_httplib_request_post_request(self):
{
'http.method': 'POST',
'http.status_code': '200',
'http.url': 'http://httpstat.us/200',
'http.url': URL_200,
}
)

Expand All @@ -216,11 +223,11 @@ def test_httplib_request_get_request_query_string(self):
When making a GET request with a query string via httplib.HTTPConnection.request
we capture a the entire url in the span
"""
conn = self.get_http_connection('httpstat.us')
conn = self.get_http_connection(SOCKET)
with contextlib.closing(conn):
conn.request('GET', '/200?key=value&key2=value2')
conn.request('GET', '/status/200?key=value&key2=value2')
resp = conn.getresponse()
self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.status, 200)

spans = self.tracer.writer.pop()
Expand All @@ -235,7 +242,7 @@ def test_httplib_request_get_request_query_string(self):
{
'http.method': 'GET',
'http.status_code': '200',
'http.url': 'http://httpstat.us/200?key=value&key2=value2',
'http.url': '{}?key=value&key2=value2'.format(URL_200),
}
)

Expand All @@ -248,9 +255,9 @@ def test_httplib_request_500_request(self):
we capture the correct span tags
"""
try:
conn = self.get_http_connection('httpstat.us')
conn = self.get_http_connection(SOCKET)
with contextlib.closing(conn):
conn.request('GET', '/500')
conn.request('GET', '/status/500')
conn.getresponse()
except httplib.HTTPException:
resp = sys.exc_info()[1]
Expand All @@ -266,7 +273,7 @@ def test_httplib_request_500_request(self):
self.assertEqual(span.error, 1)
self.assertEqual(span.get_tag('http.method'), 'GET')
self.assertEqual(span.get_tag('http.status_code'), '500')
self.assertEqual(span.get_tag('http.url'), 'http://httpstat.us/500')
self.assertEqual(span.get_tag('http.url'), URL_500)

def test_httplib_request_non_200_request(self):
"""
Expand All @@ -277,9 +284,9 @@ def test_httplib_request_non_200_request(self):
we capture the correct span tags
"""
try:
conn = self.get_http_connection('httpstat.us')
conn = self.get_http_connection(SOCKET)
with contextlib.closing(conn):
conn.request('GET', '/404')
conn.request('GET', '/status/404')
conn.getresponse()
except httplib.HTTPException:
resp = sys.exc_info()[1]
Expand All @@ -295,7 +302,7 @@ def test_httplib_request_non_200_request(self):
self.assertEqual(span.error, 0)
self.assertEqual(span.get_tag('http.method'), 'GET')
self.assertEqual(span.get_tag('http.status_code'), '404')
self.assertEqual(span.get_tag('http.url'), 'http://httpstat.us/404')
self.assertEqual(span.get_tag('http.url'), URL_404)

def test_httplib_request_get_request_disabled(self):
"""
Expand All @@ -304,11 +311,11 @@ def test_httplib_request_get_request_disabled(self):
we do not capture any spans
"""
self.tracer.enabled = False
conn = self.get_http_connection('httpstat.us')
conn = self.get_http_connection(SOCKET)
with contextlib.closing(conn):
conn.request('GET', '/200')
conn.request('GET', '/status/200')
resp = conn.getresponse()
self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.status, 200)

spans = self.tracer.writer.pop()
Expand All @@ -321,9 +328,9 @@ def test_urllib_request(self):
we capture a span for the request
"""
with override_global_tracer(self.tracer):
resp = urlopen('http://httpstat.us/200')
resp = urlopen(URL_200)

self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.getcode(), 200)

spans = self.tracer.writer.pop()
Expand All @@ -335,7 +342,7 @@ def test_urllib_request(self):
self.assertEqual(span.error, 0)
self.assertEqual(span.get_tag('http.method'), 'GET')
self.assertEqual(span.get_tag('http.status_code'), '200')
self.assertEqual(span.get_tag('http.url'), 'http://httpstat.us/200')
self.assertEqual(span.get_tag('http.url'), URL_200)

def test_urllib_request_https(self):
"""
Expand Down Expand Up @@ -368,11 +375,11 @@ def test_urllib_request_object(self):
we return the original response
we capture a span for the request
"""
req = Request('http://httpstat.us/200')
req = Request(URL_200)
with override_global_tracer(self.tracer):
resp = urlopen(req)

self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.getcode(), 200)

spans = self.tracer.writer.pop()
Expand All @@ -384,7 +391,7 @@ def test_urllib_request_object(self):
self.assertEqual(span.error, 0)
self.assertEqual(span.get_tag('http.method'), 'GET')
self.assertEqual(span.get_tag('http.status_code'), '200')
self.assertEqual(span.get_tag('http.url'), 'http://httpstat.us/200')
self.assertEqual(span.get_tag('http.url'), URL_200)

def test_urllib_request_opener(self):
"""
Expand All @@ -394,9 +401,9 @@ def test_urllib_request_opener(self):
"""
opener = build_opener()
with override_global_tracer(self.tracer):
resp = opener.open('http://httpstat.us/200')
resp = opener.open(URL_200)

self.assertEqual(self.to_str(resp.read()), '200 OK')
self.assertEqual(self.to_str(resp.read()), '')
self.assertEqual(resp.getcode(), 200)

spans = self.tracer.writer.pop()
Expand All @@ -408,7 +415,7 @@ def test_urllib_request_opener(self):
self.assertEqual(span.error, 0)
self.assertEqual(span.get_tag('http.method'), 'GET')
self.assertEqual(span.get_tag('http.status_code'), '200')
self.assertEqual(span.get_tag('http.url'), 'http://httpstat.us/200')
self.assertEqual(span.get_tag('http.url'), URL_200)


# Additional Python2 test cases for urllib
Expand All @@ -423,9 +430,9 @@ def test_urllib_request(self):
we capture a span for the request
"""
with override_global_tracer(self.tracer):
resp = urllib.urlopen('http://httpstat.us/200')
resp = urllib.urlopen(URL_200)

self.assertEqual(resp.read(), '200 OK')
self.assertEqual(resp.read(), '')
self.assertEqual(resp.getcode(), 200)

spans = self.tracer.writer.pop()
Expand All @@ -437,7 +444,7 @@ def test_urllib_request(self):
self.assertEqual(span.error, 0)
self.assertEqual(span.get_tag('http.method'), 'GET')
self.assertEqual(span.get_tag('http.status_code'), '200')
self.assertEqual(span.get_tag('http.url'), 'http://httpstat.us/200')
self.assertEqual(span.get_tag('http.url'), URL_200)

def test_urllib_request_https(self):
"""
Expand Down
18 changes: 11 additions & 7 deletions tests/contrib/requests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
from ddtrace.ext import http, errors
from tests.test_tracer import get_dummy_tracer

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

class TestRequests(object):

@staticmethod
def test_resource_path():
tracer, session = get_traced_session()
out = session.get('http://httpstat.us/200')
out = session.get(URL_200)
eq_(out.status_code, 200)
spans = tracer.writer.pop()
eq_(len(spans), 1)
s = spans[0]
eq_(s.get_tag("http.url"), "http://httpstat.us/200")
eq_(s.get_tag("http.url"), URL_200)

@staticmethod
def test_tracer_disabled():
# ensure all valid combinations of args / kwargs work
tracer, session = get_traced_session()
tracer.enabled = False
out = session.get('http://httpstat.us/200')
out = session.get(URL_200)
eq_(out.status_code, 200)
spans = tracer.writer.pop()
eq_(len(spans), 0)
Expand All @@ -35,7 +39,7 @@ def test_tracer_disabled():
def test_args_kwargs():
# ensure all valid combinations of args / kwargs work
tracer, session = get_traced_session()
url = 'http://httpstat.us/200'
url = URL_200
method = 'GET'
inputs = [
([], {'method': method, 'url': url}),
Expand All @@ -60,7 +64,7 @@ def test_args_kwargs():
@staticmethod
def test_200():
tracer, session = get_traced_session()
out = session.get('http://httpstat.us/200')
out = session.get(URL_200)
eq_(out.status_code, 200)
# validation
spans = tracer.writer.pop()
Expand All @@ -74,7 +78,7 @@ def test_200():
@staticmethod
def test_post_500():
tracer, session = get_traced_session()
out = session.post('http://httpstat.us/500')
out = session.post(URL_500)
# validation
eq_(out.status_code, 500)
spans = tracer.writer.pop()
Expand Down Expand Up @@ -109,7 +113,7 @@ def test_non_existant_url():
@staticmethod
def test_500():
tracer, session = get_traced_session()
out = session.get('http://httpstat.us/500')
out = session.get(URL_500)
eq_(out.status_code, 500)

spans = tracer.writer.pop()
Expand Down