-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest_urllib3.py
146 lines (117 loc) · 4.5 KB
/
test_urllib3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# coding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
import httpretty
import pytest
import urllib3
from scout_apm.compat import urllib3_cert_pool_manager
from scout_apm.instruments.urllib3 import ensure_installed
from tests.compat import mock
from tests.tools import delete_attributes
mock_not_attempted = mock.patch(
"scout_apm.instruments.urllib3.have_patched_pool_urlopen", new=False
)
def test_ensure_installed_twice(caplog):
ensure_installed()
ensure_installed()
assert caplog.record_tuples == 2 * [
(
"scout_apm.instruments.urllib3",
logging.DEBUG,
"Instrumenting urllib3.",
)
]
def test_install_fail_no_httpconnectionpool(caplog):
mock_no_pool = mock.patch(
"scout_apm.instruments.urllib3.HTTPConnectionPool", new=None
)
with mock_not_attempted, mock_no_pool:
ensure_installed()
assert caplog.record_tuples == [
(
"scout_apm.instruments.urllib3",
logging.DEBUG,
"Instrumenting urllib3.",
),
(
"scout_apm.instruments.urllib3",
logging.DEBUG,
"Couldn't import urllib3.HTTPConnectionPool - probably not installed.",
),
]
def test_install_fail_no_urlopen_attribute(caplog):
mock_pool = mock.patch("scout_apm.instruments.urllib3.HTTPConnectionPool")
with mock_not_attempted, mock_pool as mocked_pool:
# Remove urlopen attribute
del mocked_pool.urlopen
ensure_installed()
assert len(caplog.record_tuples) == 2
assert caplog.record_tuples[0] == (
"scout_apm.instruments.urllib3",
logging.DEBUG,
"Instrumenting urllib3.",
)
logger, level, message = caplog.record_tuples[1]
assert logger == "scout_apm.instruments.urllib3"
assert level == logging.WARNING
assert message.startswith(
"Failed to instrument for Urllib3 HTTPConnectionPool.urlopen: AttributeError"
)
def test_request(tracked_request):
ensure_installed()
with httpretty.enabled(allow_net_connect=False):
httpretty.register_uri(
httpretty.GET, "https://example.com/", body="Hello World!"
)
http = urllib3_cert_pool_manager()
response = http.request("GET", "https://example.com")
assert response.status == 200
assert response.data == b"Hello World!"
assert len(tracked_request.complete_spans) == 1
span = tracked_request.complete_spans[0]
assert span.operation == "HTTP/GET"
assert span.tags["url"] == "https://example.com:443/"
def test_second_request(tracked_request):
ensure_installed()
with tracked_request.span("Test"), httpretty.enabled(allow_net_connect=False):
httpretty.register_uri(
httpretty.GET, "https://example.com/foo", body="Hello World!"
)
httpretty.register_uri(
httpretty.GET, "https://example.org/bar", body="Hello World!"
)
http = urllib3_cert_pool_manager()
http.request("GET", "https://example.com/foo")
http.request("GET", "https://example.org/bar")
assert len(tracked_request.complete_spans) == 3
assert (
tracked_request.complete_spans[0].tags["url"] == "https://example.com:443/foo"
)
assert (
tracked_request.complete_spans[1].tags["url"] == "https://example.org:443/bar"
)
def test_request_type_error(tracked_request):
ensure_installed()
with pytest.raises(TypeError):
http = urllib3_cert_pool_manager()
connection = http.connection_from_host("example.com", scheme="https")
connection.urlopen()
assert len(tracked_request.complete_spans) == 1
span = tracked_request.complete_spans[0]
assert span.operation == "HTTP/Unknown"
assert span.tags["url"] == "Unknown"
def test_request_no_absolute_url(caplog, tracked_request):
ensure_installed()
delete_absolute_url = delete_attributes(urllib3.HTTPConnectionPool, "_absolute_url")
with httpretty.enabled(allow_net_connect=False), delete_absolute_url:
httpretty.register_uri(
httpretty.GET, "https://example.com/", body="Hello World!"
)
http = urllib3_cert_pool_manager()
response = http.request("GET", "https://example.com")
assert response.status == 200
assert response.data == b"Hello World!"
assert len(tracked_request.complete_spans) == 1
span = tracked_request.complete_spans[0]
assert span.operation == "HTTP/GET"
assert span.tags["url"] == "Unknown"