async def test_background_jobs(tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope(path="/background-jobs/")) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() response_body = await communicator.receive_output() await communicator.wait() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert response_body["body"] == b"Triggering background jobs" assert len(tracked_requests) == 3 sync_tracked_request = tracked_requests[1] assert len(sync_tracked_request.complete_spans) == 1 sync_span = sync_tracked_request.complete_spans[0] assert sync_span.operation == ( "Job/tests.integration.test_starlette_py36plus." + "app_with_scout.<locals>.background_jobs.<locals>.sync_noop") async_tracked_request = tracked_requests[2] assert len(async_tracked_request.complete_spans) == 1 async_span = async_tracked_request.complete_spans[0] assert async_span.operation == ( "Job/tests.integration.test_starlette_py36plus." + "app_with_scout.<locals>.background_jobs.<locals>.async_noop")
async def test_user_ip_collection_disabled(tracked_requests): with app_with_scout(scout_config={"collect_remote_ip": False}) as app: communicator = ApplicationCommunicator( app, asgi_http_scope(path="/", client=("1.1.1.1", None))) await communicator.send_input({"type": "http.request"}) await communicator.receive_output() await communicator.receive_output() tracked_request = tracked_requests[0] assert "user_ip" not in tracked_request.tags
async def test_no_monitor(tracked_requests): with app_with_scout(scout_config={"monitor": False}) as app: communicator = ApplicationCommunicator(app, asgi_http_scope(path="/")) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert tracked_requests == []
async def test_not_found(tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope(path="/not-found/")) await communicator.send_input({"type": "http.request"}) # Read the response. response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 404 assert tracked_requests == []
async def test_home_ignored(tracked_requests): with app_with_scout(scout_config={"ignore": ["/"]}) as app: communicator = ApplicationCommunicator(app, asgi_http_scope(path="/")) await communicator.send_input({"type": "http.request"}) # Read the response. response_start = await communicator.receive_output() response_body = await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert response_body["type"] == "http.response.body" assert response_body["body"] == b"Welcome home." assert tracked_requests == []
async def test_return_error(tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope(path="/return-error/")) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 503 tracked_request = tracked_requests[0] assert len(tracked_request.complete_spans) == 1 assert tracked_request.tags["path"] == "/return-error/" assert tracked_request.tags["error"] == "true"
async def test_filtered_params(params, expected_path, tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope(path="/", query_string=urlencode(params).encode("utf-8")), ) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert tracked_requests[0].tags["path"] == expected_path
async def test_user_ip(headers, client_address, expected, tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope(path="/", headers=headers, client=(client_address, None)), ) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert tracked_requests[0].tags["user_ip"] == expected
async def test_queue_time(header_name, tracked_requests): # Not testing floats due to Python 2/3 rounding differences queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2 with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope( path="/", headers={header_name: str("t=") + str(queue_start)}), ) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 queue_time_ns = tracked_requests[0].tags["scout.queue_time_ns"] assert isinstance(queue_time_ns, int) and queue_time_ns > 0
async def test_username(tracked_requests): class DummyBackend(AuthenticationBackend): async def authenticate(self, request): return AuthCredentials(), SimpleUser("dummy") middleware = [Middleware(AuthenticationMiddleware, backend=DummyBackend())] with app_with_scout(middleware=middleware) as app: communicator = ApplicationCommunicator(app, asgi_http_scope(path="/")) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert len(tracked_requests) == 1 tracked_request = tracked_requests[0] assert tracked_request.tags["username"] == "dummy"
async def test_server_error(tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator(app, asgi_http_scope(path="/crash/")) await communicator.send_input({"type": "http.request"}) with pytest.raises(ValueError) as excinfo: await communicator.receive_output() assert excinfo.value.args == ("BØØM!", ) assert len(tracked_requests) == 1 tracked_request = tracked_requests[0] assert len(tracked_request.complete_spans) == 1 assert tracked_request.tags["path"] == "/crash/" assert tracked_request.tags["error"] == "true" span = tracked_request.complete_spans[0] assert span.operation == ( "Controller/tests.integration.test_starlette_py36plus." + "app_with_scout.<locals>.crash")
async def test_home(tracked_requests): with app_with_scout() as app: communicator = ApplicationCommunicator(app, asgi_http_scope(path="/")) await communicator.send_input({"type": "http.request"}) # Read the response. response_start = await communicator.receive_output() response_body = await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 assert response_body["type"] == "http.response.body" assert response_body["body"] == b"Welcome home." assert len(tracked_requests) == 1 tracked_request = tracked_requests[0] assert len(tracked_request.complete_spans) == 1 assert tracked_request.tags["path"] == "/" span = tracked_request.complete_spans[0] assert span.operation == ( "Controller/tests.integration.test_starlette_py36plus." + "app_with_scout.<locals>.home")
async def test_amazon_queue_time(tracked_requests): queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2 with app_with_scout() as app: communicator = ApplicationCommunicator( app, asgi_http_scope( path="/", headers={ "X-Amzn-Trace-Id": "Self=1-{}-12456789abcdef012345678".format(queue_start) }, ), ) await communicator.send_input({"type": "http.request"}) response_start = await communicator.receive_output() await communicator.receive_output() assert response_start["type"] == "http.response.start" assert response_start["status"] == 200 queue_time_ns = tracked_requests[0].tags["scout.queue_time_ns"] assert isinstance(queue_time_ns, int) and queue_time_ns > 0