def test_post_files(flask_apm_client): with open(os.path.abspath(__file__), mode='rb') as f: response = flask_apm_client.app.test_client().post( '/an-error/', data={ 'foo': ['bar', 'baz'], 'f1': (compat.BytesIO(compat.b('1')), 'bla'), 'f2': [(f, 'flask_tests.py'), (compat.BytesIO(compat.b('1')), 'blub')], }) assert response.status_code == 500 assert len(flask_apm_client.client.events) == 1 event = flask_apm_client.client.events.pop(0)['errors'][0] print(flask_apm_client.client.config.capture_body) if flask_apm_client.client.config.capture_body in ('errors', 'all'): assert event['context']['request']['body'] == { 'foo': ['bar', 'baz'], '_files': { 'f1': 'bla', 'f2': ['flask_tests.py', 'blub'], } } else: assert event['context']['request']['body'] == '[REDACTED]'
def test_post_files(flask_apm_client): with open(os.path.abspath(__file__), mode="rb") as f: response = flask_apm_client.app.test_client().post( "/an-error/", data={ "foo": ["bar", "baz"], "f1": (compat.BytesIO(compat.b("1")), "bla"), "f2": [(f, "flask_tests.py"), (compat.BytesIO(compat.b("1")), "blub")], }, ) assert response.status_code == 500 assert len(flask_apm_client.client.events) == 1 event = flask_apm_client.client.events[ERROR][0] if flask_apm_client.client.config.capture_body in ("errors", "all"): assert event["context"]["request"]["body"] == { "foo": ["bar", "baz"], "_files": { "f1": "bla", "f2": ["flask_tests.py", "blub"] }, } else: assert event["context"]["request"]["body"] == "[REDACTED]"
def test_ssl_verify_disable(waiting_httpsserver): waiting_httpsserver.serve_content( code=202, content="", headers={"Location": "https://example.com/foo"}) transport = Transport(compat.urlparse.urlparse(waiting_httpsserver.url), verify_server_cert=False) url = transport.send(compat.b("x"), {}) assert url == "https://example.com/foo"
def test_ssl_cert_pinning_fails(waiting_httpsserver): if compat.PY3: waiting_httpsserver.serve_content( code=202, content="", headers={"Location": "https://example.com/foo"}) url = waiting_httpsserver.url else: # if we use the local test server here, execution blocks somewhere deep in OpenSSL on Python 2.7, presumably # due to a threading issue that has been fixed in later versions. To avoid that, we have to commit a minor # cardinal sin here and do an outside request to https://example.com (which will also fail the fingerprint # assertion). # # May the Testing Goat have mercy on our souls. url = "https://example.com" transport = Transport(url, server_cert=os.path.join(os.path.dirname(__file__), "wrong_cert.pem"), verify_server_cert=True) try: with pytest.raises(TransportException) as exc_info: transport.send(compat.b("x")) finally: transport.close() assert "Fingerprints did not match" in exc_info.value.args[0]
def test_non_utf8_encoding(elasticapm_client, http_test_data): broken = compat.b("broken=") + u"aéöüa".encode("latin-1") http_test_data["context"]["request"]["headers"]["cookie"] = broken result = processors.sanitize_http_request_cookies(elasticapm_client, http_test_data) assert result["context"]["request"]["headers"][ "cookie"] == u"broken=a\ufffd\ufffd\ufffda"
def test_ssl_verify_fails(waiting_httpsserver): waiting_httpsserver.serve_content( code=202, content="", headers={"Location": "http://example.com/foo"}) transport = Transport(compat.urlparse.urlparse(waiting_httpsserver.url)) with pytest.raises(TransportException) as exc_info: url = transport.send(compat.b("x"), {}) assert "CERTIFICATE_VERIFY_FAILED" in str(exc_info)
def test_send(waiting_httpserver): waiting_httpserver.serve_content(code=202, content="", headers={"Location": "http://example.com/foo"}) transport = Transport(waiting_httpserver.url) try: url = transport.send(compat.b("x")) assert url == "http://example.com/foo" finally: transport.close()
def test_ssl_verify_disable(waiting_httpsserver, elasticapm_client): waiting_httpsserver.serve_content(code=202, content="", headers={"Location": "https://example.com/foo"}) transport = Transport(waiting_httpsserver.url, verify_server_cert=False, client=elasticapm_client) transport.start_thread() try: url = transport.send(compat.b("x")) assert url == "https://example.com/foo" finally: transport.close()
def test_ssl_verify_fails(waiting_httpsserver, elasticapm_client): waiting_httpsserver.serve_content(code=202, content="", headers={"Location": "http://example.com/foo"}) transport = Transport(waiting_httpsserver.url, client=elasticapm_client) transport.start_thread() try: with pytest.raises(TransportException) as exc_info: url = transport.send(compat.b("x")) assert "certificate verify failed" in str(exc_info) finally: transport.close()
def test_ssl_cert_pinning(waiting_httpsserver): waiting_httpsserver.serve_content( code=202, content="", headers={"Location": "https://example.com/foo"}) transport = Transport(waiting_httpsserver.url, server_cert=DEFAULT_CERTIFICATE, verify_server_cert=True) try: url = transport.send(compat.b("x")) assert url == "https://example.com/foo" finally: transport.close()
def test_post_raw_data(django_elasticapm_client): request = WSGIRequest(environ={ 'wsgi.input': compat.BytesIO(compat.b('foobar')), 'wsgi.url_scheme': 'http', 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'application/json', 'ACCEPT': 'application/json', 'CONTENT_LENGTH': '6', }) django_elasticapm_client.capture('Message', message='foo', request=request) assert len(django_elasticapm_client.events) == 1 event = django_elasticapm_client.events.pop(0)['errors'][0] assert 'request' in event['context'] request = event['context']['request'] assert request['method'] == 'POST' assert request['body'] == compat.b('foobar')
def test_ssl_cert_pinning(waiting_httpsserver): waiting_httpsserver.serve_content(code=202, content="", headers={"Location": "https://example.com/foo"}) cur_dir = os.path.dirname(os.path.realpath(__file__)) transport = Transport( waiting_httpsserver.url, server_cert=os.path.join(cur_dir, "..", "ca/server.pem"), verify_server_cert=True ) try: url = transport.send(compat.b("x")) assert url == "https://example.com/foo" finally: transport.close()
def test_raw_post_data_partial_read(django_elasticapm_client): v = compat.b('{"foo": "bar"}') request = WSGIRequest(environ={ 'wsgi.input': compat.BytesIO(v + compat.b('\r\n\r\n')), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'application/json', 'CONTENT_LENGTH': len(v), 'ACCEPT': 'application/json', }) request.read(1) django_elasticapm_client.capture('Message', message='foo', request=request) assert len(django_elasticapm_client.events) == 1 event = django_elasticapm_client.events.pop(0)['errors'][0] assert 'request' in event['context'] request = event['context']['request'] assert request['method'] == 'POST' assert request['body'] == '<unavailable>'
def test_ssl_verify_disable_http(waiting_httpserver): """ Make sure that ``assert_hostname`` isn't passed in for http requests, even with verify_server_cert=False """ waiting_httpserver.serve_content( code=202, content="", headers={"Location": "http://example.com/foo"}) transport = Transport(waiting_httpserver.url, verify_server_cert=False) try: url = transport.send(compat.b("x")) assert url == "http://example.com/foo" finally: transport.close()
def test_ssl_cert_pinning_http(waiting_httpserver): """ Won't fail, as with the other cert pinning test, since certs aren't relevant for http, only https. """ waiting_httpserver.serve_content( code=202, content="", headers={"Location": "http://example.com/foo"}) transport = Transport( waiting_httpserver.url, server_cert=os.path.join(os.path.dirname(__file__), "wrong_cert.pem"), verify_server_cert=True, ) try: url = transport.send(compat.b("x")) assert url == "http://example.com/foo" finally: transport.close()
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_send(waiting_httpserver): waiting_httpserver.serve_content( code=202, content="", headers={"Location": "http://example.com/foo"}) transport = Transport(urlparse.urlparse(waiting_httpserver.url)) url = transport.send(compat.b("x"), {}) assert url == "http://example.com/foo"
def test_ssl_verify_fails(httpsserver): httpsserver.serve_content(code=202, content='', headers={'Location': 'http://example.com/foo'}) transport = Transport(compat.urlparse.urlparse(httpsserver.url)) with pytest.raises(TransportException) as exc_info: url = transport.send(compat.b('x'), {}) assert 'CERTIFICATE_VERIFY_FAILED' in str(exc_info)
def test_non_utf8_encoding(http_test_data): broken = compat.b('broken=') + u"aéöüa".encode('latin-1') http_test_data['context']['request']['url']['search'] = broken result = processors.sanitize_http_request_querystring(None, http_test_data) assert result['context']['request']['url']['search'] == u'broken=a\ufffd\ufffd\ufffda'
def test_ssl_verify_disable(httpsserver): httpsserver.serve_content(code=202, content='', headers={'Location': 'https://example.com/foo'}) transport = Transport(compat.urlparse.urlparse(httpsserver.url), verify_server_cert=False) url = transport.send(compat.b('x'), {}) assert url == 'https://example.com/foo'
def test_send(httpserver): httpserver.serve_content(code=202, content='', headers={'Location': 'http://example.com/foo'}) transport = Transport(urlparse.urlparse(httpserver.url)) url = transport.send(compat.b('x'), {}) assert url == 'http://example.com/foo'
def test_non_utf8_encoding(http_test_data): broken = compat.b("broken=") + u"aéöüa".encode("latin-1") http_test_data["context"]["request"]["url"]["search"] = broken result = processors.sanitize_http_request_querystring(None, http_test_data) assert result["context"]["request"]["url"][ "search"] == u"broken=a\ufffd\ufffd\ufffda"
def test_transform_bad_string(): x = compat.b("The following character causes problems: \xd4") result = transform(x) assert type(result) == compat.binary_type assert result == compat.b("(Error decoding value)")