def test_capture_post_errors_dict(client, django_elasticapm_client):
    with pytest.raises(Exception):
        client.post(reverse('elasticapm-raise-exc'), {'username': '******', 'password': '******'})
    error = django_elasticapm_client.events[0]
    if django_elasticapm_client.config.capture_body in ('errors', 'all'):
        assert error['errors'][0]['context']['request']['body'] == {'username': '******', 'password': '******'}
    else:
        assert error['errors'][0]['context']['request']['body'] == '[REDACTED]'
def test_capture_post_errors_raw(client, django_sending_elasticapm_client):
    # use "sending" client to ensure that we encode the payload correctly
    with pytest.raises(Exception):
        client.post(reverse('elasticapm-raise-exc'), json.dumps({'a': 'b'}).encode('utf8'),
                    content_type='application/json')
    error = django_sending_elasticapm_client.httpserver.payloads[0]
    if django_sending_elasticapm_client.config.capture_body in ('errors', 'all'):
        assert error['errors'][0]['context']['request']['body'] == '{"a": "b"}'
    else:
        assert error['errors'][0]['context']['request']['body'] == '[REDACTED]'
def test_capture_post_errors_multivalue_dict(client, django_elasticapm_client):
    with pytest.raises(Exception):
        client.post(reverse('elasticapm-raise-exc'), 'key=value1&key=value2&test=test&key=value3',
                    content_type='application/x-www-form-urlencoded')
    error = django_elasticapm_client.events[0]
    if django_elasticapm_client.config.capture_body in ('errors', 'all'):
        assert error['errors'][0]['context']['request']['body'] == {
            'key': ['value1', 'value2', 'value3'], 'test': 'test'
        }
    else:
        assert error['errors'][0]['context']['request']['body'] == '[REDACTED]'
def test_capture_files(client, django_elasticapm_client):
    with pytest.raises(MyException), open(os.path.abspath(__file__)) as f:
        client.post(reverse('elasticapm-raise-exc'), data={
            'a': 'b',
            'f1': compat.BytesIO(100*compat.b('1')),
            'f2': f,
        },)
    error = django_elasticapm_client.events[0]
    if django_elasticapm_client.config.capture_body in ('errors', 'all'):
        assert error['errors'][0]['context']['request']['body'] == {
            'a': 'b',
            '_files': {
                'f1': 'f1',
                'f2': 'django_tests.py',
            }
        }
    else:
        assert error['errors'][0]['context']['request']['body'] == '[REDACTED]'
def test_capture_empty_body(client, django_elasticapm_client):
    with pytest.raises(Exception):
        client.post(reverse('elasticapm-raise-exc'), data={})
    error = django_elasticapm_client.events[0]
    # body should be empty no matter if we capture it or not
    assert error['errors'][0]['context']['request']['body'] == {}