From 6efd51f66a30eeea392799257babc91b5ad7b7d9 Mon Sep 17 00:00:00 2001 From: Bertrand Mermet Date: Fri, 1 Sep 2017 15:22:04 +0200 Subject: [PATCH] Switch to httpbin for tests --- tests/contrib/httplib/test_httplib.py | 77 ++++++++++++++----------- tests/contrib/requests/test_requests.py | 18 +++--- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/tests/contrib/httplib/test_httplib.py b/tests/contrib/httplib/test_httplib.py index 987366e9159..0a215e079dd 100644 --- a/tests/contrib/httplib/test_httplib.py +++ b/tests/contrib/httplib/test_httplib.py @@ -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' @@ -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)) @@ -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() @@ -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, } ) @@ -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() @@ -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, } ) @@ -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() @@ -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), } ) @@ -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] @@ -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): """ @@ -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] @@ -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): """ @@ -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() @@ -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() @@ -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): """ @@ -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() @@ -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): """ @@ -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() @@ -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 @@ -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() @@ -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): """ diff --git a/tests/contrib/requests/test_requests.py b/tests/contrib/requests/test_requests.py index 913c6a83e5e..8de428bc846 100644 --- a/tests/contrib/requests/test_requests.py +++ b/tests/contrib/requests/test_requests.py @@ -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) @@ -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}), @@ -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() @@ -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() @@ -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()