code: def test_fastapi_features(serve_instance):
      app = FastAPI(openapi_url="/my_api.json")
  
      @app.on_event("startup")
      def inject_state():
          app.state.state_one = "app.state"
  
      @app.middleware("http")
      async def add_process_time_header(request: Request, call_next):
          start_time = time.time()
          response = await call_next(request)
          process_time = time.time() - start_time
          response.headers["X-Process-Time"] = str(process_time)
          return response
  
      async def yield_db():
          yield "db"
  
      async def common_parameters(q: Optional[str] = None):
          return {"q": q}
  
      @app.exception_handler(ValueError)
      async def custom_handler(_: Request, exc: ValueError):
          return JSONResponse(
              status_code=500,
              content={"custom_error": "true", "message": str(exc)},
          )
  
      def run_background(background_tasks: BackgroundTasks):
          _, path = tempfile.mkstemp()
  
          def write_to_file(p):
              with open(p, "w") as f:
                  f.write("hello")
  
          background_tasks.add_task(write_to_file, path)
          return path
  
      app.add_middleware(CORSMiddleware, allow_origins="*")
  
      @app.get("/{path_arg}", response_model=RespModel, status_code=201)
      async def func(
          path_arg: str,
          query_arg: str,
          body_val: BodyType,
          backgrounds_tasks: BackgroundTasks,
          do_error: bool = False,
          query_arg_valid: Optional[str] = Query(None, min_length=3),
          cookie_arg: Optional[str] = Cookie(None),
          user_agent: Optional[str] = Header(None),
          commons: dict = Depends(common_parameters),
          db=Depends(yield_db),
      ):
          if do_error:
              raise ValueError("bad input")
  
          path = run_background(backgrounds_tasks)
  
          return RespModel(
              ok=True,
              vals=[
                  path_arg,
                  query_arg,
                  body_val.price,
                  body_val.nests.val,
                  do_error,
                  query_arg_valid,
                  cookie_arg,
                  user_agent.split("/")[0],  # returns python-requests
                  commons,
                  db,
                  app.state.state_one,
              ],
              file_path=path,
          )
  
      router = APIRouter(prefix="/prefix")
  
      @router.get("/subpath")
      def router_path():
          return "ok"
  
      app.include_router(router)
  
      @serve.deployment
      @serve.ingress(app)
      class Worker:
          pass
  
      serve.run(Worker.bind())
  
      url = "http://localhost:8000"
      resp = requests.get(f"{url}/")
      assert resp.status_code == 404
      assert "x-process-time" in resp.headers
  
      resp = requests.get(f"{url}/my_api.json")
      assert resp.status_code == 200
      assert resp.json()  # it returns a well-formed json.
  
      resp = requests.get(f"{url}/docs")
      assert resp.status_code == 200
      assert "<!DOCTYPE html>" in resp.text
  
      resp = requests.get(f"{url}/redoc")
      assert resp.status_code == 200
      assert "<!DOCTYPE html>" in resp.text
  
      resp = requests.get(f"{url}/path_arg")
      assert resp.status_code == 422  # Malformed input
  
      resp = requests.get(
          f"{url}/path_arg",
          json={"name": "serve", "price": 12, "nests": {"val": 1}},
          params={
              "query_arg": "query_arg",
              "query_arg_valid": "at-least-three-chars",
              "q": "common_arg",
          },
      )
      assert resp.status_code == 201, resp.text
      assert resp.json()["ok"]
      assert resp.json()["vals"] == [
          "path_arg",
          "query_arg",
          12.0,
          1,
          False,
          "at-least-three-chars",
          None,
          "python-requests",
          {"q": "common_arg"},
          "db",
          "app.state",
      ]
      assert open(resp.json()["file_path"]).read() == "hello"
  
      resp = requests.get(
          f"{url}/path_arg",
          json={"name": "serve", "price": 12, "nests": {"val": 1}},
          params={
              "query_arg": "query_arg",
              "query_arg_valid": "at-least-three-chars",
              "q": "common_arg",
              "do_error": "true",
          },
      )
      assert resp.status_code == 500
      assert resp.json()["custom_error"] == "true"
  
      resp = requests.get(f"{url}/prefix/subpath")
      assert resp.status_code == 200
  
      resp = requests.get(
          f"{url}/docs",
          headers={
              "Access-Control-Request-Method": "GET",
              "Origin": "https://googlebot.com",
          },
      )
      assert (
          resp.headers["access-control-allow-origin"] == "*"
      ), resp.headers
  
imports: import inspect
  import tempfile
  import time
  from typing import Any, List, Optional
  import pytest
  import requests
  import starlette.responses
  from fastapi import APIRouter, BackgroundTasks, Cookie, Depends, FastAPI, Header, Query, Request, Response
  from fastapi.middleware.cors import CORSMiddleware
  from fastapi.responses import JSONResponse
  from pydantic import BaseModel, Field
  from starlette.applications import Starlette
  from starlette.routing import Route
  import ray
  from ray import serve
  from ray._private.test_utils import SignalActor, wait_for_condition
  from ray.exceptions import GetTimeoutError
  from ray.serve._private.client import ServeControllerClient
  from ray.serve._private.constants import SERVE_DEFAULT_APP_NAME
  from ray.serve._private.http_util import make_fastapi_class_based_view
  from ray.serve._private.utils import DEFAULT
  from ray.serve.exceptions import RayServeException
  from ray.serve.handle import DeploymentHandle
label_desc: Use image node_id 5 for calling a global function with example usage: test_fastapi_features(serve_instance) and returns: response, dict, JSONResponse, path, RespModel, str