Skip to content

Commit

Permalink
Add tests for capture_headers remote config
Browse files Browse the repository at this point in the history
  • Loading branch information
basepi committed Mar 26, 2020
1 parent 7570e39 commit 7e9e0f8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
32 changes: 32 additions & 0 deletions tests/contrib/django/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,38 @@ def test_capture_body_config_is_dynamic_for_transactions(client, django_elastica
assert transaction["context"]["request"]["body"] == "[REDACTED]"


def test_capture_headers_config_is_dynamic_for_errors(client, django_elasticapm_client):
django_elasticapm_client.config.update(version="1", capture_headers=True)
with pytest.raises(MyException):
client.post(reverse("elasticapm-raise-exc"))
error = django_elasticapm_client.events[ERROR][0]
assert error["context"]["request"]["headers"]

django_elasticapm_client.config.update(version="1", capture_headers=False)
with pytest.raises(MyException):
client.post(reverse("elasticapm-raise-exc"))
error = django_elasticapm_client.events[ERROR][1]
assert "headers" not in error["context"]["request"]


def test_capture_headers_config_is_dynamic_for_transactions(client, django_elasticapm_client):
django_elasticapm_client.config.update(version="1", capture_headers=True)
with override_settings(
**middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.TracingMiddleware"])
):
client.post(reverse("elasticapm-no-error"))
transaction = django_elasticapm_client.events[TRANSACTION][0]
assert transaction["context"]["request"]["headers"]

django_elasticapm_client.config.update(version="1", capture_headers=False)
with override_settings(
**middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.TracingMiddleware"])
):
client.post(reverse("elasticapm-no-error"))
transaction = django_elasticapm_client.events[TRANSACTION][1]
assert "headers" not in transaction["context"]["request"]


@pytest.mark.parametrize(
"django_elasticapm_client",
[{"capture_body": "errors"}, {"capture_body": "transactions"}, {"capture_body": "all"}, {"capture_body": "off"}],
Expand Down
36 changes: 32 additions & 4 deletions tests/contrib/flask/flask_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,42 @@ def test_capture_body_config_is_dynamic_for_transactions(flask_apm_client):
flask_apm_client.client.config.update(version="1", capture_body="all")
resp = flask_apm_client.app.test_client().post("/users/", data={"foo": "bar"})
resp.close()
error = flask_apm_client.client.events[TRANSACTION][0]
assert error["context"]["request"]["body"] == {"foo": "bar"}
transaction = flask_apm_client.client.events[TRANSACTION][0]
assert transaction["context"]["request"]["body"] == {"foo": "bar"}

flask_apm_client.client.config.update(version="2", capture_body="off")
resp = flask_apm_client.app.test_client().post("/users/", data={"foo": "bar"})
resp.close()
error = flask_apm_client.client.events[TRANSACTION][1]
assert error["context"]["request"]["body"] == "[REDACTED]"
transaction = flask_apm_client.client.events[TRANSACTION][1]
assert transaction["context"]["request"]["body"] == "[REDACTED]"


def test_capture_headers_config_is_dynamic_for_errors(flask_apm_client):
flask_apm_client.client.config.update(version="1", capture_headers=True)
resp = flask_apm_client.app.test_client().post("/an-error/", data={"foo": "bar"})
resp.close()
error = flask_apm_client.client.events[ERROR][0]
assert error["context"]["request"]["headers"]

flask_apm_client.client.config.update(version="2", capture_headers=False)
resp = flask_apm_client.app.test_client().post("/an-error/", data={"foo": "bar"})
resp.close()
error = flask_apm_client.client.events[ERROR][1]
assert "headers" not in error["context"]["request"]


def test_capture_headers_config_is_dynamic_for_transactions(flask_apm_client):
flask_apm_client.client.config.update(version="1", capture_headers=True)
resp = flask_apm_client.app.test_client().post("/users/", data={"foo": "bar"})
resp.close()
transaction = flask_apm_client.client.events[TRANSACTION][0]
assert transaction["context"]["request"]["headers"]

flask_apm_client.client.config.update(version="2", capture_headers=False)
resp = flask_apm_client.app.test_client().post("/users/", data={"foo": "bar"})
resp.close()
transaction = flask_apm_client.client.events[TRANSACTION][1]
assert "headers" not in transaction["context"]["request"]


@pytest.mark.parametrize("elasticapm_client", [{"capture_body": "transactions"}], indirect=True)
Expand Down

0 comments on commit 7e9e0f8

Please sign in to comment.