|
37 | 37 | from urllib3.exceptions import MaxRetryError, TimeoutError |
38 | 38 | from urllib3_mock import Responses |
39 | 39 |
|
| 40 | +from elasticapm.conf import constants |
40 | 41 | from elasticapm.transport.base import TransportException |
41 | 42 | from elasticapm.transport.http import Transport |
42 | 43 | from elasticapm.utils import compat |
@@ -215,8 +216,99 @@ def test_ssl_cert_pinning_fails(waiting_httpsserver): |
215 | 216 | transport = Transport( |
216 | 217 | url, server_cert=os.path.join(os.path.dirname(__file__), "wrong_cert.pem"), verify_server_cert=True |
217 | 218 | ) |
218 | | - with pytest.raises(TransportException) as exc_info: |
219 | | - transport.send(compat.b("x")) |
220 | | - transport.close() |
| 219 | + try: |
| 220 | + with pytest.raises(TransportException) as exc_info: |
| 221 | + transport.send(compat.b("x")) |
| 222 | + finally: |
| 223 | + transport.close() |
221 | 224 |
|
222 | 225 | assert "Fingerprints did not match" in exc_info.value.args[0] |
| 226 | + |
| 227 | + |
| 228 | +def test_config_url(): |
| 229 | + transport = Transport("http://example.com/" + constants.EVENTS_API_PATH) |
| 230 | + try: |
| 231 | + assert transport._config_url == "http://example.com/" + constants.AGENT_CONFIG_PATH |
| 232 | + finally: |
| 233 | + transport.close() |
| 234 | + |
| 235 | + |
| 236 | +def test_get_config(waiting_httpserver): |
| 237 | + waiting_httpserver.serve_content( |
| 238 | + code=200, content=b'{"x": "y"}', headers={"Cache-Control": "max-age=5", "Etag": "2"} |
| 239 | + ) |
| 240 | + url = waiting_httpserver.url |
| 241 | + transport = Transport(url + "/" + constants.EVENTS_API_PATH) |
| 242 | + try: |
| 243 | + version, data, max_age = transport.get_config("1", {}) |
| 244 | + assert version == "2" |
| 245 | + assert data == {"x": "y"} |
| 246 | + assert max_age == 5 |
| 247 | + finally: |
| 248 | + transport.close() |
| 249 | + |
| 250 | + |
| 251 | +@mock.patch("urllib3.poolmanager.PoolManager.urlopen") |
| 252 | +def test_get_config_handle_exception(mock_urlopen, caplog): |
| 253 | + transport = Transport("http://example.com/" + constants.EVENTS_API_PATH) |
| 254 | + mock_urlopen.side_effect = urllib3.exceptions.RequestError(transport.http, "http://example.com/", "boom") |
| 255 | + try: |
| 256 | + with caplog.at_level("DEBUG", "elasticapm.transport.http"): |
| 257 | + version, data, max_age = transport.get_config("1", {}) |
| 258 | + assert version == "1" |
| 259 | + assert max_age == 300 |
| 260 | + record = caplog.records[-1] |
| 261 | + assert "HTTP error" in record.msg |
| 262 | + finally: |
| 263 | + transport.close() |
| 264 | + |
| 265 | + |
| 266 | +def test_get_config_cache_headers_304(waiting_httpserver, caplog): |
| 267 | + waiting_httpserver.serve_content(code=304, content=b"", headers={"Cache-Control": "max-age=5"}) |
| 268 | + url = waiting_httpserver.url |
| 269 | + transport = Transport(url + "/" + constants.EVENTS_API_PATH) |
| 270 | + try: |
| 271 | + with caplog.at_level("DEBUG", "elasticapm.transport.http"): |
| 272 | + version, data, max_age = transport.get_config("1", {}) |
| 273 | + assert waiting_httpserver.requests[0].headers["If-None-Match"] == "1" |
| 274 | + assert version == "1" |
| 275 | + assert data is None |
| 276 | + assert max_age == 5 |
| 277 | + record = caplog.records[-1] |
| 278 | + assert "Configuration unchanged" in record.msg |
| 279 | + finally: |
| 280 | + transport.close() |
| 281 | + |
| 282 | + |
| 283 | +def test_get_config_bad_cache_control_header(waiting_httpserver, caplog): |
| 284 | + waiting_httpserver.serve_content( |
| 285 | + code=200, content=b'{"x": "y"}', headers={"Cache-Control": "max-age=fifty", "Etag": "2"} |
| 286 | + ) |
| 287 | + url = waiting_httpserver.url |
| 288 | + transport = Transport(url + "/" + constants.EVENTS_API_PATH) |
| 289 | + try: |
| 290 | + with caplog.at_level("DEBUG", "elasticapm.transport.http"): |
| 291 | + version, data, max_age = transport.get_config("1", {}) |
| 292 | + assert version == "2" |
| 293 | + assert data == {"x": "y"} |
| 294 | + assert max_age == 300 |
| 295 | + record = caplog.records[-1] |
| 296 | + assert record.message == "Could not parse Cache-Control header: max-age=fifty" |
| 297 | + finally: |
| 298 | + transport.close() |
| 299 | + |
| 300 | + |
| 301 | +def test_get_config_empty_response(waiting_httpserver, caplog): |
| 302 | + waiting_httpserver.serve_content(code=200, content=b"", headers={"Cache-Control": "max-age=5"}) |
| 303 | + url = waiting_httpserver.url |
| 304 | + transport = Transport(url + "/" + constants.EVENTS_API_PATH) |
| 305 | + try: |
| 306 | + with caplog.at_level("DEBUG", "elasticapm.transport.http"): |
| 307 | + version, data, max_age = transport.get_config("1", {}) |
| 308 | + assert version == "1" |
| 309 | + assert data is None |
| 310 | + assert max_age == 5 |
| 311 | + record = caplog.records[-1] |
| 312 | + assert record.message == "APM Server answered with empty body and status code 200" |
| 313 | + finally: |
| 314 | + transport.close() |
0 commit comments