|
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 os |
31 | 32 | import pytest # isort:skip |
32 | 33 |
|
33 | 34 | from tests.fixtures import TempStoreClient |
34 | 35 |
|
35 | 36 | starlette = pytest.importorskip("starlette") # isort:skip |
36 | 37 |
|
37 | | -import types |
38 | | - |
39 | 38 | import mock |
40 | 39 | import urllib3 |
41 | 40 | from starlette.applications import Starlette |
42 | 41 | from starlette.responses import PlainTextResponse |
| 42 | +from starlette.staticfiles import StaticFiles |
43 | 43 | from starlette.testclient import TestClient |
44 | 44 |
|
45 | 45 | import elasticapm |
|
53 | 53 |
|
54 | 54 | starlette_version_tuple = tuple(map(int, starlette.__version__.split(".")[:3])) |
55 | 55 |
|
| 56 | +file_path, file_name = os.path.split(__file__) |
| 57 | + |
56 | 58 |
|
57 | 59 | @pytest.fixture |
58 | 60 | def app(elasticapm_client): |
@@ -371,6 +373,77 @@ def test_capture_body_error(app, elasticapm_client): |
371 | 373 | response = client.post("/raise-exception", data="[0, 1]") |
372 | 374 |
|
373 | 375 |
|
| 376 | +@pytest.fixture |
| 377 | +def app_static_files_only(elasticapm_client): |
| 378 | + app = Starlette() |
| 379 | + app.add_middleware(ElasticAPM, client=elasticapm_client) |
| 380 | + app.mount("/tmp", StaticFiles(directory=file_path), name="static") |
| 381 | + |
| 382 | + yield app |
| 383 | + |
| 384 | + elasticapm.uninstrument() |
| 385 | + |
| 386 | + |
| 387 | +def test_static_files_only(app_static_files_only, elasticapm_client): |
| 388 | + client = TestClient(app_static_files_only) |
| 389 | + |
| 390 | + response = client.get( |
| 391 | + "/tmp/" + file_name, |
| 392 | + headers={ |
| 393 | + constants.TRACEPARENT_HEADER_NAME: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03", |
| 394 | + constants.TRACESTATE_HEADER_NAME: "foo=bar,bar=baz", |
| 395 | + "REMOTE_ADDR": "127.0.0.1", |
| 396 | + }, |
| 397 | + ) |
| 398 | + |
| 399 | + assert response.status_code == 200 |
| 400 | + |
| 401 | + assert len(elasticapm_client.events[constants.TRANSACTION]) == 1 |
| 402 | + transaction = elasticapm_client.events[constants.TRANSACTION][0] |
| 403 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 404 | + assert len(spans) == 0 |
| 405 | + |
| 406 | + assert transaction["name"] == "GET /tmp" |
| 407 | + assert transaction["result"] == "HTTP 2xx" |
| 408 | + assert transaction["outcome"] == "success" |
| 409 | + assert transaction["type"] == "request" |
| 410 | + assert transaction["span_count"]["started"] == 0 |
| 411 | + assert transaction["context"]["request"]["url"]["pathname"] == "/tmp/" + file_name |
| 412 | + request = transaction["context"]["request"] |
| 413 | + assert request["method"] == "GET" |
| 414 | + assert request["socket"] == {"remote_address": "127.0.0.1", "encrypted": False} |
| 415 | + |
| 416 | + |
| 417 | +def test_static_files_only_file_notfound(app_static_files_only, elasticapm_client): |
| 418 | + client = TestClient(app_static_files_only) |
| 419 | + |
| 420 | + response = client.get( |
| 421 | + "/tmp/whatever", |
| 422 | + headers={ |
| 423 | + constants.TRACEPARENT_HEADER_NAME: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03", |
| 424 | + constants.TRACESTATE_HEADER_NAME: "foo=bar,bar=baz", |
| 425 | + "REMOTE_ADDR": "127.0.0.1", |
| 426 | + }, |
| 427 | + ) |
| 428 | + |
| 429 | + assert response.status_code == 404 |
| 430 | + |
| 431 | + assert len(elasticapm_client.events[constants.TRANSACTION]) == 1 |
| 432 | + transaction = elasticapm_client.events[constants.TRANSACTION][0] |
| 433 | + spans = elasticapm_client.spans_for_transaction(transaction) |
| 434 | + assert len(spans) == 0 |
| 435 | + |
| 436 | + assert transaction["name"] == "GET /tmp" |
| 437 | + assert transaction["result"] == "HTTP 4xx" |
| 438 | + assert transaction["outcome"] == "success" |
| 439 | + assert transaction["type"] == "request" |
| 440 | + assert transaction["span_count"]["started"] == 0 |
| 441 | + assert transaction["context"]["request"]["url"]["pathname"] == "/tmp/whatever" |
| 442 | + request = transaction["context"]["request"] |
| 443 | + assert request["method"] == "GET" |
| 444 | + assert request["socket"] == {"remote_address": "127.0.0.1", "encrypted": False} |
| 445 | + |
| 446 | + |
374 | 447 | def test_make_client_with_config(): |
375 | 448 | c = make_apm_client(config={"SERVICE_NAME": "foo"}, client_cls=TempStoreClient) |
376 | 449 | c.close() |
|
0 commit comments