Issue 1:
First Check

I added a very descriptive title to this issue.

I used the GitHub search to find a similar issue and didn't find it.

I searched the FastAPI documentation, with the integrated search.

I already searched in Google "How to X in FastAPI" and didn't find any information.

I already read and followed all the tutorial in the docs and didn't find an answer.

I already checked if it is not related to FastAPI but to Pydantic.

I already checked if it is not related to FastAPI but to Swagger UI.

I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

I commit to help with one of those options 👆

Example Code

# currently:

@app.on_event("startup")
async def startup():
load_defaults()
await DB.start_db(settings=settings)
return

# what I'd like
@app.on_event("startup")
async def startup(defaults_retriever=None):
if not defaults_retriever:
defaults_retriever = RealRetriever()
load_defaults(defaults_retriever)
await DB.start_db(settings=settings)
return

# and then in tests I wouldnt need to mock the remote because I can do something like
client = TestClient(app, startup_args=[MockDefaultsRetriever])

Description

Is it possible to pass an argument to the startup function of the TestClient?

https://fastapi.tiangolo.com/advanced/testing-events/

The docs suggest using TestClient to ensure the startup function gets invoked, but what if the startup function has an argument that is a class that hits a remote and Id like to mock it. Since patching module can be finicky dependency injection is a clean way to accomplish the goal of mocking an object and if something like I suggested in the example code was implemented it would make this very easy.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.70.0

Python Version

Python 3.9.7

Additional Context

No response

Issue 2:
First Check

I added a very descriptive title to this issue.

I used the GitHub search to find a similar issue and didn't find it.

I searched the FastAPI documentation, with the integrated search.

I already searched in Google "How to X in FastAPI" and didn't find any information.

I already read and followed all the tutorial in the docs and didn't find an answer.

I already checked if it is not related to FastAPI but to Pydantic.

I already checked if it is not related to FastAPI but to Swagger UI.

I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

I commit to help with one of those options 👆

Example Code

# main.py
from random import randint
from fastapi import FastAPI, WebSocket

app = FastAPI()

@app.get("/", tags=["Test"])
async def index():
return {
"response": "Index"
}

@app.websocket("/wsx")
async def test_socket(websocket: WebSocket, start: int = 1, stop: int = 100):
await websocket.accept()
while True:
try:
number = await websocket.receive_text()
rndm = randint(start, stop)
response = int(number) + rndm
await websocket.send_json(
{"number": response}
)
except Exception as error:
break

# router.py
from random import randint
from fastapi import APIRouter, WebSocket

socket_router = APIRouter(
prefix="/wsx",
tags=["Socket"]
)

@socket_router.websocket("/")
async def test_socket(websocket: WebSocket, start: int = 1, stop: int = 100):
await websocket.accept()
while True:
try:
number = await websocket.receive_text()
rndm = randint(start, stop)
response = int(number) + rndm
await websocket.send_json(
{"number": response}
)
except Exception as error:
break

Description

When I use this WebSocket inside of "mani.py", everything is Ok.

But when I move this WebSocket from "main.py" to "router.py" and I use APIRouter in "router.py" and then I include this router inside of "main.py", WebSocket doesn't work!

what's the solution

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.73.0

Python Version

3.7.9

Additional Context

No response