|
28 | 28 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
|
| 31 | +import mock |
31 | 32 | import pytest |
32 | 33 |
|
33 | 34 | from elasticapm.conf import constants |
|
39 | 40 | try: |
40 | 41 | from urllib.request import urlopen |
41 | 42 | from urllib.error import URLError |
| 43 | + |
| 44 | + request_method = "http.client.HTTPConnection.request" |
| 45 | + getresponse_method = "http.client.HTTPConnection.getresponse" |
42 | 46 | except ImportError: |
43 | 47 | from urllib2 import urlopen |
44 | 48 | from urllib2 import URLError |
45 | 49 |
|
| 50 | + request_method = "httplib.HTTPConnection.request" |
| 51 | + getresponse_method = "httplib.HTTPConnection.getresponse" |
| 52 | + |
46 | 53 |
|
47 | 54 | def test_urllib(instrument, elasticapm_client, waiting_httpserver): |
48 | 55 | waiting_httpserver.serve_content("") |
@@ -77,6 +84,30 @@ def test_urllib(instrument, elasticapm_client, waiting_httpserver): |
77 | 84 | assert spans[1]["parent_id"] == transactions[0]["id"] |
78 | 85 |
|
79 | 86 |
|
| 87 | +@mock.patch(request_method) |
| 88 | +@mock.patch(getresponse_method) |
| 89 | +def test_urllib_standard_port(mock_getresponse, mock_request, instrument, elasticapm_client): |
| 90 | + # "code" is needed for Python 3, "status" for Python 2 |
| 91 | + mock_getresponse.return_value = mock.Mock(code=200, status=200) |
| 92 | + |
| 93 | + url = "http://example.com/" |
| 94 | + parsed_url = urlparse.urlparse(url) |
| 95 | + elasticapm_client.begin_transaction("transaction") |
| 96 | + expected_sig = "GET {0}".format(parsed_url.netloc) |
| 97 | + r = urlopen(url) |
| 98 | + |
| 99 | + elasticapm_client.end_transaction("MyView") |
| 100 | + |
| 101 | + transactions = elasticapm_client.events[TRANSACTION] |
| 102 | + spans = elasticapm_client.spans_for_transaction(transactions[0]) |
| 103 | + |
| 104 | + assert spans[0]["name"] == expected_sig |
| 105 | + assert spans[0]["type"] == "external" |
| 106 | + assert spans[0]["subtype"] == "http" |
| 107 | + assert spans[0]["context"]["http"]["url"] == url |
| 108 | + assert spans[0]["parent_id"] == transactions[0]["id"] |
| 109 | + |
| 110 | + |
80 | 111 | def test_trace_parent_propagation_sampled(instrument, elasticapm_client, waiting_httpserver): |
81 | 112 | waiting_httpserver.serve_content("") |
82 | 113 | url = waiting_httpserver.url + "/hello_world" |
|
0 commit comments