mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-30 17:50:39 -05:00
* ⬆ Bump ruff from 0.6.4 to 0.9.4 Bumps [ruff](https://github.com/astral-sh/ruff) from 0.6.4 to 0.9.4. - [Release notes](https://github.com/astral-sh/ruff/releases) - [Changelog](https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md) - [Commits](https://github.com/astral-sh/ruff/compare/0.6.4...0.9.4) --- updated-dependencies: - dependency-name: ruff dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * update pre-commit accordingly and make formatting changes * 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Alejandra <90076947+alejsdev@users.noreply.github.com> Co-authored-by: svlandeg <sofie.vanlandeghem@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
from fastapi import Depends, FastAPI, Header, status
|
|
from fastapi.testclient import TestClient
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def get_header(*, someheader: str = Header()):
|
|
return someheader
|
|
|
|
|
|
def get_something_else(*, someheader: str = Depends(get_header)):
|
|
return f"{someheader}123"
|
|
|
|
|
|
@app.get("/")
|
|
def get_deps(dep1: str = Depends(get_header), dep2: str = Depends(get_something_else)):
|
|
return {"dep1": dep1, "dep2": dep2}
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
schema = {
|
|
"components": {
|
|
"schemas": {
|
|
"HTTPValidationError": {
|
|
"properties": {
|
|
"detail": {
|
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
"title": "Detail",
|
|
"type": "array",
|
|
}
|
|
},
|
|
"title": "HTTPValidationError",
|
|
"type": "object",
|
|
},
|
|
"ValidationError": {
|
|
"properties": {
|
|
"loc": {
|
|
"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
|
|
"title": "Location",
|
|
"type": "array",
|
|
},
|
|
"msg": {"title": "Message", "type": "string"},
|
|
"type": {"title": "Error Type", "type": "string"},
|
|
},
|
|
"required": ["loc", "msg", "type"],
|
|
"title": "ValidationError",
|
|
"type": "object",
|
|
},
|
|
}
|
|
},
|
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
"openapi": "3.1.0",
|
|
"paths": {
|
|
"/": {
|
|
"get": {
|
|
"operationId": "get_deps__get",
|
|
"parameters": [
|
|
{
|
|
"in": "header",
|
|
"name": "someheader",
|
|
"required": True,
|
|
"schema": {"title": "Someheader", "type": "string"},
|
|
}
|
|
],
|
|
"responses": {
|
|
"200": {
|
|
"content": {"application/json": {"schema": {}}},
|
|
"description": "Successful Response",
|
|
},
|
|
"422": {
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"$ref": "#/components/schemas/HTTPValidationError"
|
|
}
|
|
}
|
|
},
|
|
"description": "Validation Error",
|
|
},
|
|
},
|
|
"summary": "Get Deps",
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
def test_schema():
|
|
response = client.get("/openapi.json")
|
|
assert response.status_code == status.HTTP_200_OK
|
|
actual_schema = response.json()
|
|
assert actual_schema == schema
|
|
assert (
|
|
len(actual_schema["paths"]["/"]["get"]["parameters"]) == 1
|
|
) # primary goal of this test
|
|
|
|
|
|
def test_response():
|
|
response = client.get("/", headers={"someheader": "hello"})
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json() == {"dep1": "hello", "dep2": "hello123"}
|