repo
stringlengths
7
90
file_url
stringlengths
81
315
file_path
stringlengths
4
228
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:38:15
2026-01-05 02:33:18
truncated
bool
2 classes
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_dependencies/test_tutorial012.py
tests/test_tutorial/test_dependencies/test_tutorial012.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial012_py39"), pytest.param("tutorial012_an_py39"), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.dependencies.{request.param}") client = TestClient(mod.app) return client def test_get_no_headers_items(client: TestClient): response = client.get("/items/") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "type": "missing", "loc": ["header", "x-token"], "msg": "Field required", "input": None, }, { "type": "missing", "loc": ["header", "x-key"], "msg": "Field required", "input": None, }, ] } def test_get_no_headers_users(client: TestClient): response = client.get("/users/") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "type": "missing", "loc": ["header", "x-token"], "msg": "Field required", "input": None, }, { "type": "missing", "loc": ["header", "x-key"], "msg": "Field required", "input": None, }, ] } def test_get_invalid_one_header_items(client: TestClient): response = client.get("/items/", headers={"X-Token": "invalid"}) assert response.status_code == 400, response.text assert response.json() == {"detail": "X-Token header invalid"} def test_get_invalid_one_users(client: TestClient): response = client.get("/users/", headers={"X-Token": "invalid"}) assert response.status_code == 400, response.text assert response.json() == {"detail": "X-Token header invalid"} def test_get_invalid_second_header_items(client: TestClient): response = client.get( "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} ) assert response.status_code == 400, response.text assert response.json() == {"detail": "X-Key header invalid"} def test_get_invalid_second_header_users(client: TestClient): response = client.get( "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} ) assert response.status_code == 400, response.text assert response.json() == {"detail": "X-Key header invalid"} def test_get_valid_headers_items(client: TestClient): response = client.get( "/items/", headers={ "X-Token": "fake-super-secret-token", "X-Key": "fake-super-secret-key", }, ) assert response.status_code == 200, response.text assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] def test_get_valid_headers_users(client: TestClient): response = client.get( "/users/", headers={ "X-Token": "fake-super-secret-token", "X-Key": "fake-super-secret-key", }, ) assert response.status_code == 200, response.text assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": True, "schema": {"title": "X-Token", "type": "string"}, "name": "x-token", "in": "header", }, { "required": True, "schema": {"title": "X-Key", "type": "string"}, "name": "x-key", "in": "header", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, "/users/": { "get": { "summary": "Read Users", "operationId": "read_users_users__get", "parameters": [ { "required": True, "schema": {"title": "X-Token", "type": "string"}, "name": "x-token", "in": "header", }, { "required": True, "schema": {"title": "X-Key", "type": "string"}, "name": "x-key", "in": "header", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, }, "components": { "schemas": { "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py
tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py
import os from pathlib import Path import pytest from fastapi.testclient import TestClient @pytest.fixture(scope="module") def client(): static_dir: Path = Path(os.getcwd()) / "static" print(static_dir) static_dir.mkdir(exist_ok=True) from docs_src.custom_docs_ui.tutorial002_py39 import app with TestClient(app) as client: yield client static_dir.rmdir() def test_swagger_ui_html(client: TestClient): response = client.get("/docs") assert response.status_code == 200, response.text assert "/static/swagger-ui-bundle.js" in response.text assert "/static/swagger-ui.css" in response.text def test_swagger_ui_oauth2_redirect_html(client: TestClient): response = client.get("/docs/oauth2-redirect") assert response.status_code == 200, response.text assert "window.opener.swaggerUIRedirectOauth2" in response.text def test_redoc_html(client: TestClient): response = client.get("/redoc") assert response.status_code == 200, response.text assert "/static/redoc.standalone.js" in response.text def test_api(client: TestClient): response = client.get("/users/john") assert response.status_code == 200, response.text assert response.json()["message"] == "Hello john"
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py
tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py
import os from pathlib import Path import pytest from fastapi.testclient import TestClient @pytest.fixture(scope="module") def client(): static_dir: Path = Path(os.getcwd()) / "static" print(static_dir) static_dir.mkdir(exist_ok=True) from docs_src.custom_docs_ui.tutorial001_py39 import app with TestClient(app) as client: yield client static_dir.rmdir() def test_swagger_ui_html(client: TestClient): response = client.get("/docs") assert response.status_code == 200, response.text assert "https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js" in response.text assert "https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" in response.text def test_swagger_ui_oauth2_redirect_html(client: TestClient): response = client.get("/docs/oauth2-redirect") assert response.status_code == 200, response.text assert "window.opener.swaggerUIRedirectOauth2" in response.text def test_redoc_html(client: TestClient): response = client.get("/redoc") assert response.status_code == 200, response.text assert "https://unpkg.com/redoc@2/bundles/redoc.standalone.js" in response.text def test_api(client: TestClient): response = client.get("/users/john") assert response.status_code == 200, response.text assert response.json()["message"] == "Hello john"
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_docs_ui/__init__.py
tests/test_tutorial/test_custom_docs_ui/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_async_tests/__init__.py
tests/test_tutorial/test_async_tests/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_async_tests/test_main_a.py
tests/test_tutorial/test_async_tests/test_main_a.py
import pytest from docs_src.async_tests.app_a_py39.test_main import test_root @pytest.mark.anyio async def test_async_testing(): await test_root()
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_bigger_applications/test_main.py
tests/test_tutorial/test_bigger_applications/test_main.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ "app_py39.main", "app_an_py39.main", ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.bigger_applications.{request.param}") client = TestClient(mod.app) return client def test_users_token_jessica(client: TestClient): response = client.get("/users?token=jessica") assert response.status_code == 200 assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] def test_users_with_no_token(client: TestClient): response = client.get("/users") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, } ] } def test_users_foo_token_jessica(client: TestClient): response = client.get("/users/foo?token=jessica") assert response.status_code == 200 assert response.json() == {"username": "foo"} def test_users_foo_with_no_token(client: TestClient): response = client.get("/users/foo") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, } ] } def test_users_me_token_jessica(client: TestClient): response = client.get("/users/me?token=jessica") assert response.status_code == 200 assert response.json() == {"username": "fakecurrentuser"} def test_users_me_with_no_token(client: TestClient): response = client.get("/users/me") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, } ] } def test_users_token_monica_with_no_jessica(client: TestClient): response = client.get("/users?token=monica") assert response.status_code == 400 assert response.json() == {"detail": "No Jessica token provided"} def test_items_token_jessica(client: TestClient): response = client.get( "/items?token=jessica", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 200 assert response.json() == { "plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}, } def test_items_with_no_token_jessica(client: TestClient): response = client.get("/items", headers={"X-Token": "fake-super-secret-token"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, } ] } def test_items_plumbus_token_jessica(client: TestClient): response = client.get( "/items/plumbus?token=jessica", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 200 assert response.json() == {"name": "Plumbus", "item_id": "plumbus"} def test_items_bar_token_jessica(client: TestClient): response = client.get( "/items/bar?token=jessica", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 404 assert response.json() == {"detail": "Item not found"} def test_items_plumbus_with_no_token(client: TestClient): response = client.get( "/items/plumbus", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, } ] } def test_items_with_invalid_token(client: TestClient): response = client.get("/items?token=jessica", headers={"X-Token": "invalid"}) assert response.status_code == 400 assert response.json() == {"detail": "X-Token header invalid"} def test_items_bar_with_invalid_token(client: TestClient): response = client.get("/items/bar?token=jessica", headers={"X-Token": "invalid"}) assert response.status_code == 400 assert response.json() == {"detail": "X-Token header invalid"} def test_items_with_missing_x_token_header(client: TestClient): response = client.get("/items?token=jessica") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["header", "x-token"], "msg": "Field required", "input": None, } ] } def test_items_plumbus_with_missing_x_token_header(client: TestClient): response = client.get("/items/plumbus?token=jessica") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["header", "x-token"], "msg": "Field required", "input": None, } ] } def test_root_token_jessica(client: TestClient): response = client.get("/?token=jessica") assert response.status_code == 200 assert response.json() == {"message": "Hello Bigger Applications!"} def test_root_with_no_token(client: TestClient): response = client.get("/") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, } ] } def test_put_no_header(client: TestClient): response = client.put("/items/foo") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "type": "missing", "loc": ["query", "token"], "msg": "Field required", "input": None, }, { "type": "missing", "loc": ["header", "x-token"], "msg": "Field required", "input": None, }, ] } def test_put_invalid_header(client: TestClient): response = client.put("/items/foo", headers={"X-Token": "invalid"}) assert response.status_code == 400, response.text assert response.json() == {"detail": "X-Token header invalid"} def test_put(client: TestClient): response = client.put( "/items/plumbus?token=jessica", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 200, response.text assert response.json() == {"item_id": "plumbus", "name": "The great Plumbus"} def test_put_forbidden(client: TestClient): response = client.put( "/items/bar?token=jessica", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 403, response.text assert response.json() == {"detail": "You can only update the item: plumbus"} def test_admin(client: TestClient): response = client.post( "/admin/?token=jessica", headers={"X-Token": "fake-super-secret-token"} ) assert response.status_code == 200, response.text assert response.json() == {"message": "Admin getting schwifty"} def test_admin_invalid_header(client: TestClient): response = client.post("/admin/", headers={"X-Token": "invalid"}) assert response.status_code == 400, response.text assert response.json() == {"detail": "X-Token header invalid"} def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/users/": { "get": { "tags": ["users"], "summary": "Read Users", "operationId": "read_users_users__get", "parameters": [ { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", } ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, "/users/me": { "get": { "tags": ["users"], "summary": "Read User Me", "operationId": "read_user_me_users_me_get", "parameters": [ { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", } ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, "/users/{username}": { "get": { "tags": ["users"], "summary": "Read User", "operationId": "read_user_users__username__get", "parameters": [ { "required": True, "schema": {"title": "Username", "type": "string"}, "name": "username", "in": "path", }, { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, "/items/": { "get": { "tags": ["items"], "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", }, { "required": True, "schema": {"title": "X-Token", "type": "string"}, "name": "x-token", "in": "header", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "404": {"description": "Not found"}, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, "/items/{item_id}": { "get": { "tags": ["items"], "summary": "Read Item", "operationId": "read_item_items__item_id__get", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", }, { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", }, { "required": True, "schema": {"title": "X-Token", "type": "string"}, "name": "x-token", "in": "header", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "404": {"description": "Not found"}, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, }, "put": { "tags": ["items", "custom"], "summary": "Update Item", "operationId": "update_item_items__item_id__put", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", }, { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", }, { "required": True, "schema": {"title": "X-Token", "type": "string"}, "name": "x-token", "in": "header", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "404": {"description": "Not found"}, "403": {"description": "Operation forbidden"}, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, }, }, "/admin/": { "post": { "tags": ["admin"], "summary": "Update Admin", "operationId": "update_admin_admin__post", "parameters": [ { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", }, { "required": True, "schema": {"title": "X-Token", "type": "string"}, "name": "x-token", "in": "header", }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "418": {"description": "I'm a teapot"}, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, "/": { "get": { "summary": "Root", "operationId": "root__get", "parameters": [ { "required": True, "schema": {"title": "Token", "type": "string"}, "name": "token", "in": "query", } ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } }, }, "components": { "schemas": { "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_bigger_applications/__init__.py
tests/test_tutorial/test_bigger_applications/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_conditional_openapi/test_tutorial001.py
tests/test_tutorial/test_conditional_openapi/test_tutorial001.py
import importlib from fastapi.testclient import TestClient def get_client() -> TestClient: from docs_src.conditional_openapi import tutorial001_py39 importlib.reload(tutorial001_py39) client = TestClient(tutorial001_py39.app) return client def test_disable_openapi(monkeypatch): monkeypatch.setenv("OPENAPI_URL", "") # Load the client after setting the env var client = get_client() response = client.get("/openapi.json") assert response.status_code == 404, response.text response = client.get("/docs") assert response.status_code == 404, response.text response = client.get("/redoc") assert response.status_code == 404, response.text def test_root(): client = get_client() response = client.get("/") assert response.status_code == 200 assert response.json() == {"message": "Hello World"} def test_default_openapi(): client = get_client() response = client.get("/docs") assert response.status_code == 200, response.text response = client.get("/redoc") assert response.status_code == 200, response.text response = client.get("/openapi.json") assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/": { "get": { "summary": "Root", "operationId": "root__get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_conditional_openapi/__init__.py
tests/test_tutorial/test_conditional_openapi/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_websockets/test_tutorial002.py
tests/test_tutorial/test_websockets/test_tutorial002.py
import importlib import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from fastapi.websockets import WebSocketDisconnect from ...utils import needs_py310 @pytest.fixture( name="app", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=needs_py310), pytest.param("tutorial002_an_py39"), pytest.param("tutorial002_an_py310", marks=needs_py310), ], ) def get_app(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.websockets.{request.param}") return mod.app def test_main(app: FastAPI): client = TestClient(app) response = client.get("/") assert response.status_code == 200, response.text assert b"<!DOCTYPE html>" in response.content def test_websocket_with_cookie(app: FastAPI): client = TestClient(app, cookies={"session": "fakesession"}) with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/items/foo/ws") as websocket: message = "Message one" websocket.send_text(message) data = websocket.receive_text() assert data == "Session cookie or query token value is: fakesession" data = websocket.receive_text() assert data == f"Message text was: {message}, for item ID: foo" message = "Message two" websocket.send_text(message) data = websocket.receive_text() assert data == "Session cookie or query token value is: fakesession" data = websocket.receive_text() assert data == f"Message text was: {message}, for item ID: foo" def test_websocket_with_header(app: FastAPI): client = TestClient(app) with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/items/bar/ws?token=some-token") as websocket: message = "Message one" websocket.send_text(message) data = websocket.receive_text() assert data == "Session cookie or query token value is: some-token" data = websocket.receive_text() assert data == f"Message text was: {message}, for item ID: bar" message = "Message two" websocket.send_text(message) data = websocket.receive_text() assert data == "Session cookie or query token value is: some-token" data = websocket.receive_text() assert data == f"Message text was: {message}, for item ID: bar" def test_websocket_with_header_and_query(app: FastAPI): client = TestClient(app) with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/items/2/ws?q=3&token=some-token") as websocket: message = "Message one" websocket.send_text(message) data = websocket.receive_text() assert data == "Session cookie or query token value is: some-token" data = websocket.receive_text() assert data == "Query parameter q is: 3" data = websocket.receive_text() assert data == f"Message text was: {message}, for item ID: 2" message = "Message two" websocket.send_text(message) data = websocket.receive_text() assert data == "Session cookie or query token value is: some-token" data = websocket.receive_text() assert data == "Query parameter q is: 3" data = websocket.receive_text() assert data == f"Message text was: {message}, for item ID: 2" def test_websocket_no_credentials(app: FastAPI): client = TestClient(app) with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/items/foo/ws"): pytest.fail( "did not raise WebSocketDisconnect on __enter__" ) # pragma: no cover def test_websocket_invalid_data(app: FastAPI): client = TestClient(app) with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/items/foo/ws?q=bar&token=some-token"): pytest.fail( "did not raise WebSocketDisconnect on __enter__" ) # pragma: no cover
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_websockets/test_tutorial001.py
tests/test_tutorial/test_websockets/test_tutorial001.py
import pytest from fastapi.testclient import TestClient from fastapi.websockets import WebSocketDisconnect from docs_src.websockets.tutorial001_py39 import app client = TestClient(app) def test_main(): response = client.get("/") assert response.status_code == 200, response.text assert b"<!DOCTYPE html>" in response.content def test_websocket(): with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/ws") as websocket: message = "Message one" websocket.send_text(message) data = websocket.receive_text() assert data == f"Message text was: {message}" message = "Message two" websocket.send_text(message) data = websocket.receive_text() assert data == f"Message text was: {message}"
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_websockets/__init__.py
tests/test_tutorial/test_websockets/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_websockets/test_tutorial003.py
tests/test_tutorial/test_websockets/test_tutorial003.py
import importlib from types import ModuleType import pytest from fastapi.testclient import TestClient @pytest.fixture( name="mod", params=[ pytest.param("tutorial003_py39"), ], ) def get_mod(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.websockets.{request.param}") return mod @pytest.fixture(name="html") def get_html(mod: ModuleType): return mod.html @pytest.fixture(name="client") def get_client(mod: ModuleType): client = TestClient(mod.app) return client def test_get(client: TestClient, html: str): response = client.get("/") assert response.text == html def test_websocket_handle_disconnection(client: TestClient): with ( client.websocket_connect("/ws/1234") as connection, client.websocket_connect("/ws/5678") as connection_two, ): connection.send_text("Hello from 1234") data1 = connection.receive_text() assert data1 == "You wrote: Hello from 1234" data2 = connection_two.receive_text() client1_says = "Client #1234 says: Hello from 1234" assert data2 == client1_says data1 = connection.receive_text() assert data1 == client1_says connection_two.close() data1 = connection.receive_text() assert data1 == "Client #5678 left the chat"
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_debugging/test_tutorial001.py
tests/test_tutorial/test_debugging/test_tutorial001.py
import importlib import runpy import sys import unittest import pytest from fastapi.testclient import TestClient MOD_NAME = "docs_src.debugging.tutorial001_py39" @pytest.fixture(name="client") def get_client(): mod = importlib.import_module(MOD_NAME) client = TestClient(mod.app) return client def test_uvicorn_run_is_not_called_on_import(): if sys.modules.get(MOD_NAME): del sys.modules[MOD_NAME] # pragma: no cover with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock: importlib.import_module(MOD_NAME) uvicorn_run_mock.assert_not_called() def test_get_root(client: TestClient): response = client.get("/") assert response.status_code == 200 assert response.json() == {"hello world": "ba"} def test_uvicorn_run_called_when_run_as_main(): # Just for coverage if sys.modules.get(MOD_NAME): del sys.modules[MOD_NAME] with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock: runpy.run_module(MOD_NAME, run_name="__main__") uvicorn_run_mock.assert_called_once_with( unittest.mock.ANY, host="0.0.0.0", port=8000 ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/": { "get": { "summary": "Root", "operationId": "root__get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_debugging/__init__.py
tests/test_tutorial/test_debugging/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_params/test_tutorial002.py
tests/test_tutorial/test_header_params/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=needs_py310), pytest.param("tutorial002_an_py39"), pytest.param("tutorial002_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.header_params.{request.param}") client = TestClient(mod.app) return client @pytest.mark.parametrize( "path,headers,expected_status,expected_response", [ ("/items", None, 200, {"strange_header": None}), ("/items", {"X-Header": "notvalid"}, 200, {"strange_header": None}), ( "/items", {"strange_header": "FastAPI test"}, 200, {"strange_header": "FastAPI test"}, ), ( "/items", {"strange-header": "Not really underscore"}, 200, {"strange_header": None}, ), ], ) def test(path, headers, expected_status, expected_response, client: TestClient): response = client.get(path, headers=headers) assert response.status_code == expected_status assert response.json() == expected_response def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Strange Header", }, "name": "strange_header", "in": "header", } ], } } }, "components": { "schemas": { "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_params/test_tutorial001.py
tests/test_tutorial/test_header_params/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), pytest.param("tutorial001_an_py39"), pytest.param("tutorial001_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.header_params.{request.param}") client = TestClient(mod.app) return client @pytest.mark.parametrize( "path,headers,expected_status,expected_response", [ ("/items", None, 200, {"User-Agent": "testclient"}), ("/items", {"X-Header": "notvalid"}, 200, {"User-Agent": "testclient"}), ("/items", {"User-Agent": "FastAPI test"}, 200, {"User-Agent": "FastAPI test"}), ], ) def test(path, headers, expected_status, expected_response, client: TestClient): response = client.get(path, headers=headers) assert response.status_code == expected_status assert response.json() == expected_response def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "User-Agent", }, "name": "user-agent", "in": "header", } ], } } }, "components": { "schemas": { "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_params/__init__.py
tests/test_tutorial/test_header_params/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_params/test_tutorial003.py
tests/test_tutorial/test_header_params/test_tutorial003.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial003_py39"), pytest.param("tutorial003_py310", marks=needs_py310), pytest.param("tutorial003_an_py39"), pytest.param("tutorial003_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.header_params.{request.param}") client = TestClient(mod.app) return client @pytest.mark.parametrize( "path,headers,expected_status,expected_response", [ ("/items", None, 200, {"X-Token values": None}), ("/items", {"x-token": "foo"}, 200, {"X-Token values": ["foo"]}), ( "/items", [("x-token", "foo"), ("x-token", "bar")], 200, {"X-Token values": ["foo", "bar"]}, ), ], ) def test(path, headers, expected_status, expected_response, client: TestClient): response = client.get(path, headers=headers) assert response.status_code == expected_status assert response.json() == expected_response def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": False, "schema": { "title": "X-Token", "anyOf": [ {"type": "array", "items": {"type": "string"}}, {"type": "null"}, ], }, "name": "x-token", "in": "header", } ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial007.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial007.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial007_py39"), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.path_operation_advanced_configuration.{request.param}" ) client = TestClient(mod.app) return client def test_post(client: TestClient): yaml_data = """ name: Deadpoolio tags: - x-force - x-men - x-avengers """ response = client.post("/items/", content=yaml_data) assert response.status_code == 200, response.text assert response.json() == { "name": "Deadpoolio", "tags": ["x-force", "x-men", "x-avengers"], } def test_post_broken_yaml(client: TestClient): yaml_data = """ name: Deadpoolio tags: x - x-force x - x-men x - x-avengers """ response = client.post("/items/", content=yaml_data) assert response.status_code == 422, response.text assert response.json() == {"detail": "Invalid YAML"} def test_post_invalid(client: TestClient): yaml_data = """ name: Deadpoolio tags: - x-force - x-men - x-avengers - sneaky: object """ response = client.post("/items/", content=yaml_data) assert response.status_code == 422, response.text # insert_assert(response.json()) assert response.json() == { "detail": [ { "type": "string_type", "loc": ["tags", 3], "msg": "Input should be a valid string", "input": {"sneaky": "object"}, } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "post": { "summary": "Create Item", "operationId": "create_item_items__post", "requestBody": { "content": { "application/x-yaml": { "schema": { "title": "Item", "required": ["name", "tags"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "tags": { "title": "Tags", "type": "array", "items": {"type": "string"}, }, }, } } }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial002.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial002.py
from fastapi.testclient import TestClient from docs_src.path_operation_advanced_configuration.tutorial002_py39 import app client = TestClient(app) def test_get(): response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [{"item_id": "Foo"}] def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Items", "operationId": "read_items", } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial001.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial001.py
from fastapi.testclient import TestClient from docs_src.path_operation_advanced_configuration.tutorial001_py39 import app client = TestClient(app) def test_get(): response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [{"item_id": "Foo"}] def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Items", "operationId": "some_specific_id_you_define", } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial005.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial005.py
from fastapi.testclient import TestClient from docs_src.path_operation_advanced_configuration.tutorial005_py39 import app client = TestClient(app) def test_get(): response = client.get("/items/") assert response.status_code == 200, response.text def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Items", "operationId": "read_items_items__get", "x-aperture-labs-portal": "blue", } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial006.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial006.py
from fastapi.testclient import TestClient from docs_src.path_operation_advanced_configuration.tutorial006_py39 import app client = TestClient(app) def test_post(): response = client.post("/items/", content=b"this is actually not validated") assert response.status_code == 200, response.text assert response.json() == { "size": 30, "content": { "name": "Maaaagic", "price": 42, "description": "Just kiddin', no magic here. ✨", }, } def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "post": { "summary": "Create Item", "operationId": "create_item_items__post", "requestBody": { "content": { "application/json": { "schema": { "required": ["name", "price"], "type": "object", "properties": { "name": {"type": "string"}, "price": {"type": "number"}, "description": {"type": "string"}, }, } } }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/__init__.py
tests/test_tutorial/test_path_operation_advanced_configurations/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial004.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial004.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial004_py39"), pytest.param("tutorial004_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.path_operation_advanced_configuration.{request.param}" ) client = TestClient(mod.app) client.headers.clear() return client def test_query_params_str_validations(client: TestClient): response = client.post("/items/", json={"name": "Foo", "price": 42}) assert response.status_code == 200, response.text assert response.json() == { "name": "Foo", "price": 42, "description": None, "tax": None, "tags": [], } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "post": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Create an item", "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item", "operationId": "create_item_items__post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, "required": True, }, } } }, "components": { "schemas": { "Item": { "title": "Item", "required": ["name", "price"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "description": { "title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}], }, "price": {"title": "Price", "type": "number"}, "tax": { "title": "Tax", "anyOf": [{"type": "number"}, {"type": "null"}], }, "tags": { "title": "Tags", "uniqueItems": True, "type": "array", "items": {"type": "string"}, "default": [], }, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial003.py
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial003.py
from fastapi.testclient import TestClient from docs_src.path_operation_advanced_configuration.tutorial003_py39 import app client = TestClient(app) def test_get(): response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [{"item_id": "Foo"}] def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": {}, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_params_numeric_validations/test_tutorial001.py
tests/test_tutorial/test_path_params_numeric_validations/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), pytest.param("tutorial001_an_py39"), pytest.param("tutorial001_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module( f"docs_src.path_params_numeric_validations.{request.param}" ) return TestClient(mod.app) @pytest.mark.parametrize( "path,expected_response", [ ("/items/42", {"item_id": 42}), ("/items/123?item-query=somequery", {"item_id": 123, "q": "somequery"}), ], ) def test_read_items(client: TestClient, path, expected_response): response = client.get(path) assert response.status_code == 200, response.text assert response.json() == expected_response def test_read_items_invalid_item_id(client: TestClient): response = client.get("/items/invalid_id") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "invalid_id", "msg": "Input should be a valid integer, unable to parse string as an integer", "type": "int_parsing", } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "get": { "summary": "Read Items", "operationId": "read_items_items__item_id__get", "parameters": [ { "required": True, "schema": { "title": "The ID of the item to get", "type": "integer", }, "name": "item_id", "in": "path", }, { "required": False, "schema": { "anyOf": [ { "type": "string", }, { "type": "null", }, ], "title": "Item-Query", }, "name": "item-query", "in": "query", }, ], "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {}, } }, }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError", }, }, }, "description": "Validation Error", }, }, } } }, "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", }, }, }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_params_numeric_validations/test_tutorial002_tutorial003.py
tests/test_tutorial/test_path_params_numeric_validations/test_tutorial002_tutorial003.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_an_py39"), pytest.param("tutorial003_py39"), pytest.param("tutorial003_an_py39"), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module( f"docs_src.path_params_numeric_validations.{request.param}" ) return TestClient(mod.app) @pytest.mark.parametrize( "path,expected_response", [ ("/items/42?q=", {"item_id": 42}), ("/items/123?q=somequery", {"item_id": 123, "q": "somequery"}), ], ) def test_read_items(client: TestClient, path, expected_response): response = client.get(path) assert response.status_code == 200, response.text assert response.json() == expected_response def test_read_items_invalid_item_id(client: TestClient): response = client.get("/items/invalid_id?q=somequery") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "invalid_id", "msg": "Input should be a valid integer, unable to parse string as an integer", "type": "int_parsing", } ] } def test_read_items_missing_q(client: TestClient): response = client.get("/items/42") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["query", "q"], "input": None, "msg": "Field required", "type": "missing", } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "get": { "summary": "Read Items", "operationId": "read_items_items__item_id__get", "parameters": [ { "required": True, "schema": { "title": "The ID of the item to get", "type": "integer", }, "name": "item_id", "in": "path", }, { "required": True, "schema": { "type": "string", "title": "Q", }, "name": "q", "in": "query", }, ], "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {}, } }, }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError", }, }, }, "description": "Validation Error", }, }, } } }, "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", }, }, }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_params_numeric_validations/test_tutorial005.py
tests/test_tutorial/test_path_params_numeric_validations/test_tutorial005.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial005_py39"), pytest.param("tutorial005_an_py39"), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module( f"docs_src.path_params_numeric_validations.{request.param}" ) return TestClient(mod.app) @pytest.mark.parametrize( "path,expected_response", [ ("/items/1?q=", {"item_id": 1}), ("/items/1000?q=somequery", {"item_id": 1000, "q": "somequery"}), ], ) def test_read_items(client: TestClient, path, expected_response): response = client.get(path) assert response.status_code == 200, response.text assert response.json() == expected_response def test_read_items_non_int_item_id(client: TestClient): response = client.get("/items/invalid_id?q=somequery") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "invalid_id", "msg": "Input should be a valid integer, unable to parse string as an integer", "type": "int_parsing", } ] } def test_read_items_item_id_less_than_one(client: TestClient): response = client.get("/items/0?q=somequery") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "0", "msg": "Input should be greater than 0", "type": "greater_than", "ctx": {"gt": 0}, } ] } def test_read_items_item_id_greater_than_one_thousand(client: TestClient): response = client.get("/items/1001?q=somequery") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "1001", "msg": "Input should be less than or equal to 1000", "type": "less_than_equal", "ctx": {"le": 1000}, } ] } def test_read_items_missing_q(client: TestClient): response = client.get("/items/42") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["query", "q"], "input": None, "msg": "Field required", "type": "missing", } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "get": { "summary": "Read Items", "operationId": "read_items_items__item_id__get", "parameters": [ { "required": True, "schema": { "title": "The ID of the item to get", "type": "integer", "exclusiveMinimum": 0, "maximum": 1000, }, "name": "item_id", "in": "path", }, { "required": True, "schema": { "type": "string", "title": "Q", }, "name": "q", "in": "query", }, ], "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {}, } }, }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError", }, }, }, "description": "Validation Error", }, }, } } }, "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", }, }, }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_params_numeric_validations/test_tutorial006.py
tests/test_tutorial/test_path_params_numeric_validations/test_tutorial006.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial006_py39"), pytest.param("tutorial006_an_py39"), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module( f"docs_src.path_params_numeric_validations.{request.param}" ) return TestClient(mod.app) @pytest.mark.parametrize( "path,expected_response", [ ( "/items/0?q=&size=0.1", {"item_id": 0, "size": 0.1}, ), ( "/items/1000?q=somequery&size=10.4", {"item_id": 1000, "q": "somequery", "size": 10.4}, ), ], ) def test_read_items(client: TestClient, path, expected_response): response = client.get(path) assert response.status_code == 200, response.text assert response.json() == expected_response def test_read_items_item_id_less_than_zero(client: TestClient): response = client.get("/items/-1?q=somequery&size=5") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "-1", "msg": "Input should be greater than or equal to 0", "type": "greater_than_equal", "ctx": {"ge": 0}, } ] } def test_read_items_item_id_greater_than_one_thousand(client: TestClient): response = client.get("/items/1001?q=somequery&size=5") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "1001", "msg": "Input should be less than or equal to 1000", "type": "less_than_equal", "ctx": {"le": 1000}, } ] } def test_read_items_size_too_small(client: TestClient): response = client.get("/items/1?q=somequery&size=0.0") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["query", "size"], "input": "0.0", "msg": "Input should be greater than 0", "type": "greater_than", "ctx": {"gt": 0.0}, } ] } def test_read_items_size_too_large(client: TestClient): response = client.get("/items/1?q=somequery&size=10.5") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["query", "size"], "input": "10.5", "msg": "Input should be less than 10.5", "type": "less_than", "ctx": {"lt": 10.5}, } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "get": { "summary": "Read Items", "operationId": "read_items_items__item_id__get", "parameters": [ { "required": True, "schema": { "title": "The ID of the item to get", "type": "integer", "minimum": 0, "maximum": 1000, }, "name": "item_id", "in": "path", }, { "required": True, "schema": { "type": "string", "title": "Q", }, "name": "q", "in": "query", }, { "in": "query", "name": "size", "required": True, "schema": { "exclusiveMaximum": 10.5, "exclusiveMinimum": 0, "title": "Size", "type": "number", }, }, ], "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {}, } }, }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError", }, }, }, "description": "Validation Error", }, }, } } }, "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", }, }, }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_params_numeric_validations/__init__.py
tests/test_tutorial/test_path_params_numeric_validations/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_path_params_numeric_validations/test_tutorial004.py
tests/test_tutorial/test_path_params_numeric_validations/test_tutorial004.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial004_py39"), pytest.param("tutorial004_an_py39"), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module( f"docs_src.path_params_numeric_validations.{request.param}" ) return TestClient(mod.app) @pytest.mark.parametrize( "path,expected_response", [ ("/items/42?q=", {"item_id": 42}), ("/items/1?q=somequery", {"item_id": 1, "q": "somequery"}), ], ) def test_read_items(client: TestClient, path, expected_response): response = client.get(path) assert response.status_code == 200, response.text assert response.json() == expected_response def test_read_items_non_int_item_id(client: TestClient): response = client.get("/items/invalid_id?q=somequery") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "invalid_id", "msg": "Input should be a valid integer, unable to parse string as an integer", "type": "int_parsing", } ] } def test_read_items_item_id_less_than_one(client: TestClient): response = client.get("/items/0?q=somequery") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["path", "item_id"], "input": "0", "msg": "Input should be greater than or equal to 1", "type": "greater_than_equal", "ctx": {"ge": 1}, } ] } def test_read_items_missing_q(client: TestClient): response = client.get("/items/42") assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "loc": ["query", "q"], "input": None, "msg": "Field required", "type": "missing", } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "get": { "summary": "Read Items", "operationId": "read_items_items__item_id__get", "parameters": [ { "required": True, "schema": { "title": "The ID of the item to get", "type": "integer", "minimum": 1, }, "name": "item_id", "in": "path", }, { "required": True, "schema": { "type": "string", "title": "Q", }, "name": "q", "in": "query", }, ], "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {}, } }, }, "422": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError", }, }, }, "description": "Validation Error", }, }, } } }, "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", }, }, }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_graphql/test_tutorial001.py
tests/test_tutorial/test_graphql/test_tutorial001.py
import warnings import pytest from starlette.testclient import TestClient warnings.filterwarnings( "ignore", message=r"The 'lia' package has been renamed to 'cross_web'\..*", category=DeprecationWarning, ) from docs_src.graphql_.tutorial001_py39 import app # noqa: E402 @pytest.fixture(name="client") def get_client() -> TestClient: return TestClient(app) def test_query(client: TestClient): response = client.post("/graphql", json={"query": "{ user { name, age } }"}) assert response.status_code == 200 assert response.json() == {"data": {"user": {"name": "Patrick", "age": 100}}} def test_openapi(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "info": { "title": "FastAPI", "version": "0.1.0", }, "openapi": "3.1.0", "paths": { "/graphql": { "get": { "operationId": "handle_http_get_graphql_get", "responses": { "200": { "content": { "application/json": { "schema": {}, }, }, "description": "The GraphiQL integrated development environment.", }, "404": { "description": "Not found if GraphiQL or query via GET are not enabled.", }, }, "summary": "Handle Http Get", }, "post": { "operationId": "handle_http_post_graphql_post", "responses": { "200": { "content": { "application/json": { "schema": {}, }, }, "description": "Successful Response", }, }, "summary": "Handle Http Post", }, }, }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_graphql/__init__.py
tests/test_tutorial/test_graphql/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_body_fields/test_tutorial001.py
tests/test_tutorial/test_body_fields/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ "tutorial001_py39", pytest.param("tutorial001_py310", marks=needs_py310), "tutorial001_an_py39", pytest.param("tutorial001_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.body_fields.{request.param}") client = TestClient(mod.app) return client def test_items_5(client: TestClient): response = client.put("/items/5", json={"item": {"name": "Foo", "price": 3.0}}) assert response.status_code == 200 assert response.json() == { "item_id": 5, "item": {"name": "Foo", "price": 3.0, "description": None, "tax": None}, } def test_items_6(client: TestClient): response = client.put( "/items/6", json={ "item": { "name": "Bar", "price": 0.2, "description": "Some bar", "tax": "5.4", } }, ) assert response.status_code == 200 assert response.json() == { "item_id": 6, "item": { "name": "Bar", "price": 0.2, "description": "Some bar", "tax": 5.4, }, } def test_invalid_price(client: TestClient): response = client.put("/items/5", json={"item": {"name": "Foo", "price": -3.0}}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "greater_than", "loc": ["body", "item", "price"], "msg": "Input should be greater than 0", "input": -3.0, "ctx": {"gt": 0.0}, } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "put": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Update Item", "operationId": "update_item_items__item_id__put", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "integer"}, "name": "item_id", "in": "path", } ], "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Body_update_item_items__item_id__put" } } }, "required": True, }, } } }, "components": { "schemas": { "Item": { "title": "Item", "required": ["name", "price"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "description": { "title": "The description of the item", "anyOf": [ {"maxLength": 300, "type": "string"}, {"type": "null"}, ], }, "price": { "title": "Price", "exclusiveMinimum": 0.0, "type": "number", "description": "The price must be greater than zero", }, "tax": { "title": "Tax", "anyOf": [{"type": "number"}, {"type": "null"}], }, }, }, "Body_update_item_items__item_id__put": { "title": "Body_update_item_items__item_id__put", "required": ["item"], "type": "object", "properties": {"item": {"$ref": "#/components/schemas/Item"}}, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_body_fields/__init__.py
tests/test_tutorial/test_body_fields/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_change_status_code/test_tutorial001.py
tests/test_tutorial/test_response_change_status_code/test_tutorial001.py
from fastapi.testclient import TestClient from docs_src.response_change_status_code.tutorial001_py39 import app client = TestClient(app) def test_path_operation(): response = client.put("/get-or-create-task/foo") print(response.content) assert response.status_code == 200, response.text assert response.json() == "Listen to the Bar Fighters" response = client.put("/get-or-create-task/bar") assert response.status_code == 201, response.text assert response.json() == "This didn't exist before"
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_change_status_code/__init__.py
tests/test_tutorial/test_response_change_status_code/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_behind_a_proxy/test_tutorial002.py
tests/test_tutorial/test_behind_a_proxy/test_tutorial002.py
from fastapi.testclient import TestClient from docs_src.behind_a_proxy.tutorial002_py39 import app client = TestClient(app) def test_main(): response = client.get("/app") assert response.status_code == 200 assert response.json() == {"message": "Hello World", "root_path": "/api/v1"} def test_openapi(): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/app": { "get": { "summary": "Read Main", "operationId": "read_main_app_get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, "servers": [{"url": "/api/v1"}], }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_behind_a_proxy/test_tutorial001.py
tests/test_tutorial/test_behind_a_proxy/test_tutorial001.py
from fastapi.testclient import TestClient from docs_src.behind_a_proxy.tutorial001_py39 import app client = TestClient(app, root_path="/api/v1") def test_main(): response = client.get("/app") assert response.status_code == 200 assert response.json() == {"message": "Hello World", "root_path": "/api/v1"} def test_openapi(): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/app": { "get": { "summary": "Read Main", "operationId": "read_main_app_get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, "servers": [{"url": "/api/v1"}], }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_behind_a_proxy/test_tutorial001_01.py
tests/test_tutorial/test_behind_a_proxy/test_tutorial001_01.py
from fastapi.testclient import TestClient from docs_src.behind_a_proxy.tutorial001_01_py39 import app client = TestClient( app, base_url="https://example.com", follow_redirects=False, ) def test_redirect() -> None: response = client.get("/items") assert response.status_code == 307 assert response.headers["location"] == "https://example.com/items/" def test_no_redirect() -> None: response = client.get("/items/") assert response.status_code == 200 assert response.json() == ["plumbus", "portal gun"]
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_behind_a_proxy/__init__.py
tests/test_tutorial/test_behind_a_proxy/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_behind_a_proxy/test_tutorial004.py
tests/test_tutorial/test_behind_a_proxy/test_tutorial004.py
from fastapi.testclient import TestClient from inline_snapshot import snapshot from docs_src.behind_a_proxy.tutorial004_py39 import app client = TestClient(app) def test_main(): response = client.get("/app") assert response.status_code == 200 assert response.json() == {"message": "Hello World", "root_path": "/api/v1"} def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "servers": [ { "url": "https://stag.example.com", "description": "Staging environment", }, { "url": "https://prod.example.com", "description": "Production environment", }, ], "paths": { "/app": { "get": { "summary": "Read Main", "operationId": "read_main_app_get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_behind_a_proxy/test_tutorial003.py
tests/test_tutorial/test_behind_a_proxy/test_tutorial003.py
from fastapi.testclient import TestClient from inline_snapshot import snapshot from docs_src.behind_a_proxy.tutorial003_py39 import app client = TestClient(app) def test_main(): response = client.get("/app") assert response.status_code == 200 assert response.json() == {"message": "Hello World", "root_path": "/api/v1"} def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200 assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "servers": [ {"url": "/api/v1"}, { "url": "https://stag.example.com", "description": "Staging environment", }, { "url": "https://prod.example.com", "description": "Production environment", }, ], "paths": { "/app": { "get": { "summary": "Read Main", "operationId": "read_main_app_get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module(f"docs_src.separate_openapi_schemas.{request.param}") client = TestClient(mod.app) return client def test_create_item(client: TestClient) -> None: response = client.post("/items/", json={"name": "Foo"}) assert response.status_code == 200, response.text assert response.json() == {"name": "Foo", "description": None} def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [ { "name": "Portal Gun", "description": "Device to travel through the multi-rick-verse", }, {"name": "Plumbus", "description": None}, ] def test_openapi_schema(client: TestClient) -> None: response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "items": {"$ref": "#/components/schemas/Item"}, "type": "array", "title": "Response Read Items Items Get", } } }, } }, }, "post": { "summary": "Create Item", "operationId": "create_item_items__post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, }, } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "Item": { "properties": { "name": {"type": "string", "title": "Name"}, "description": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Description", }, }, "type": "object", "required": ["name"], "title": "Item", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest) -> TestClient: mod = importlib.import_module(f"docs_src.separate_openapi_schemas.{request.param}") client = TestClient(mod.app) return client def test_create_item(client: TestClient) -> None: response = client.post("/items/", json={"name": "Foo"}) assert response.status_code == 200, response.text assert response.json() == {"name": "Foo", "description": None} def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [ { "name": "Portal Gun", "description": "Device to travel through the multi-rick-verse", }, {"name": "Plumbus", "description": None}, ] def test_openapi_schema(client: TestClient) -> None: response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "items": {"$ref": "#/components/schemas/Item"}, "type": "array", "title": "Response Read Items Items Get", } } }, } }, }, "post": { "summary": "Create Item", "operationId": "create_item_items__post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, }, } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "Item": { "properties": { "name": {"type": "string", "title": "Name"}, "description": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Description", }, }, "type": "object", "required": ["name"], "title": "Item", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_separate_openapi_schemas/__init__.py
tests/test_tutorial/test_separate_openapi_schemas/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_cookie_param_models/test_tutorial002.py
tests/test_tutorial/test_cookie_param_models/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=[needs_py310]), pytest.param("tutorial002_an_py39"), pytest.param("tutorial002_an_py310", marks=[needs_py310]), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}") client = TestClient(mod.app) return client def test_cookie_param_model(client: TestClient): with client as c: c.cookies.set("session_id", "123") c.cookies.set("fatebook_tracker", "456") c.cookies.set("googall_tracker", "789") response = c.get("/items/") assert response.status_code == 200 assert response.json() == { "session_id": "123", "fatebook_tracker": "456", "googall_tracker": "789", } def test_cookie_param_model_defaults(client: TestClient): with client as c: c.cookies.set("session_id", "123") response = c.get("/items/") assert response.status_code == 200 assert response.json() == { "session_id": "123", "fatebook_tracker": None, "googall_tracker": None, } def test_cookie_param_model_invalid(client: TestClient): response = client.get("/items/") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["cookie", "session_id"], "msg": "Field required", "input": {}, } ] } def test_cookie_param_model_extra(client: TestClient): with client as c: c.cookies.set("session_id", "123") c.cookies.set("extra", "track-me-here-too") response = c.get("/items/") assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "extra_forbidden", "loc": ["cookie", "extra"], "msg": "Extra inputs are not permitted", "input": "track-me-here-too", } ] } ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "session_id", "in": "cookie", "required": True, "schema": {"type": "string", "title": "Session Id"}, }, { "name": "fatebook_tracker", "in": "cookie", "required": False, "schema": { "anyOf": [ {"type": "string"}, {"type": "null"}, ], "title": "Fatebook Tracker", }, }, { "name": "googall_tracker", "in": "cookie", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Googall Tracker", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_cookie_param_models/test_tutorial001.py
tests/test_tutorial/test_cookie_param_models/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ "tutorial001_py39", pytest.param("tutorial001_py310", marks=needs_py310), "tutorial001_an_py39", pytest.param("tutorial001_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}") client = TestClient(mod.app) return client def test_cookie_param_model(client: TestClient): with client as c: c.cookies.set("session_id", "123") c.cookies.set("fatebook_tracker", "456") c.cookies.set("googall_tracker", "789") response = c.get("/items/") assert response.status_code == 200 assert response.json() == { "session_id": "123", "fatebook_tracker": "456", "googall_tracker": "789", } def test_cookie_param_model_defaults(client: TestClient): with client as c: c.cookies.set("session_id", "123") response = c.get("/items/") assert response.status_code == 200 assert response.json() == { "session_id": "123", "fatebook_tracker": None, "googall_tracker": None, } def test_cookie_param_model_invalid(client: TestClient): response = client.get("/items/") assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "missing", "loc": ["cookie", "session_id"], "msg": "Field required", "input": {}, } ] } ) def test_cookie_param_model_extra(client: TestClient): with client as c: c.cookies.set("session_id", "123") c.cookies.set("extra", "track-me-here-too") response = c.get("/items/") assert response.status_code == 200 assert response.json() == snapshot( {"session_id": "123", "fatebook_tracker": None, "googall_tracker": None} ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "session_id", "in": "cookie", "required": True, "schema": {"type": "string", "title": "Session Id"}, }, { "name": "fatebook_tracker", "in": "cookie", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Fatebook Tracker", }, }, { "name": "googall_tracker", "in": "cookie", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Googall Tracker", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_cookie_param_models/__init__.py
tests/test_tutorial/test_cookie_param_models/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_advanced_middleware/test_tutorial002.py
tests/test_tutorial/test_advanced_middleware/test_tutorial002.py
from fastapi.testclient import TestClient from docs_src.advanced_middleware.tutorial002_py39 import app def test_middleware(): client = TestClient(app, base_url="http://example.com") response = client.get("/") assert response.status_code == 200, response.text client = TestClient(app, base_url="http://subdomain.example.com") response = client.get("/") assert response.status_code == 200, response.text client = TestClient(app, base_url="http://invalidhost") response = client.get("/") assert response.status_code == 400, response.text
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_advanced_middleware/test_tutorial001.py
tests/test_tutorial/test_advanced_middleware/test_tutorial001.py
from fastapi.testclient import TestClient from docs_src.advanced_middleware.tutorial001_py39 import app def test_middleware(): client = TestClient(app, base_url="https://testserver") response = client.get("/") assert response.status_code == 200, response.text client = TestClient(app) response = client.get("/", follow_redirects=False) assert response.status_code == 307, response.text assert response.headers["location"] == "https://testserver/"
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_advanced_middleware/__init__.py
tests/test_tutorial/test_advanced_middleware/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_advanced_middleware/test_tutorial003.py
tests/test_tutorial/test_advanced_middleware/test_tutorial003.py
from fastapi.responses import PlainTextResponse from fastapi.testclient import TestClient from docs_src.advanced_middleware.tutorial003_py39 import app @app.get("/large") async def large(): return PlainTextResponse("x" * 4000, status_code=200) client = TestClient(app) def test_middleware(): response = client.get("/large", headers={"accept-encoding": "gzip"}) assert response.status_code == 200, response.text assert response.text == "x" * 4000 assert response.headers["Content-Encoding"] == "gzip" assert int(response.headers["Content-Length"]) < 4000 response = client.get("/") assert response.status_code == 200, response.text
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial007.py
tests/test_tutorial/test_custom_response/test_tutorial007.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial007_py39 import app client = TestClient(app) def test_get(): fake_content = b"some fake video bytes" response = client.get("/") assert response.content == fake_content * 10
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial001b.py
tests/test_tutorial/test_custom_response/test_tutorial001b.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial001b_py39 import app client = TestClient(app) def test_get_custom_response(): response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [{"item_id": "Foo"}] def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Items", "operationId": "read_items_items__get", } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial001.py
tests/test_tutorial/test_custom_response/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial010_py39"), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.custom_response.{request.param}") client = TestClient(mod.app) return client def test_get_custom_response(client: TestClient): response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [{"item_id": "Foo"}] def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Items", "operationId": "read_items_items__get", } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial006c.py
tests/test_tutorial/test_custom_response/test_tutorial006c.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial006c_py39 import app client = TestClient(app) def test_redirect_status_code(): response = client.get("/pydantic", follow_redirects=False) assert response.status_code == 302 assert response.headers["location"] == "https://docs.pydantic.dev/" def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/pydantic": { "get": { "summary": "Redirect Pydantic", "operationId": "redirect_pydantic_pydantic_get", "responses": {"302": {"description": "Successful Response"}}, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial005.py
tests/test_tutorial/test_custom_response/test_tutorial005.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial005_py39 import app client = TestClient(app) def test_get(): response = client.get("/") assert response.status_code == 200, response.text assert response.text == "Hello World" def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/": { "get": { "summary": "Main", "operationId": "main__get", "responses": { "200": { "description": "Successful Response", "content": {"text/plain": {"schema": {"type": "string"}}}, } }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial006.py
tests/test_tutorial/test_custom_response/test_tutorial006.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial006_py39 import app client = TestClient(app) def test_get(): response = client.get("/typer", follow_redirects=False) assert response.status_code == 307, response.text assert response.headers["location"] == "https://typer.tiangolo.com" def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/typer": { "get": { "summary": "Redirect Typer", "operationId": "redirect_typer_typer_get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial009c.py
tests/test_tutorial/test_custom_response/test_tutorial009c.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial009c_py39 import app client = TestClient(app) def test_get(): response = client.get("/") assert response.content == b'{\n "message": "Hello World"\n}'
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial009.py
tests/test_tutorial/test_custom_response/test_tutorial009.py
from pathlib import Path from fastapi.testclient import TestClient from docs_src.custom_response import tutorial009_py39 from docs_src.custom_response.tutorial009_py39 import app client = TestClient(app) def test_get(tmp_path: Path): file_path: Path = tmp_path / "large-video-file.mp4" tutorial009_py39.some_file_path = str(file_path) test_content = b"Fake video bytes" file_path.write_bytes(test_content) response = client.get("/") assert response.content == test_content
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial006b.py
tests/test_tutorial/test_custom_response/test_tutorial006b.py
from fastapi.testclient import TestClient from docs_src.custom_response.tutorial006b_py39 import app client = TestClient(app) def test_redirect_response_class(): response = client.get("/fastapi", follow_redirects=False) assert response.status_code == 307 assert response.headers["location"] == "https://fastapi.tiangolo.com" def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/fastapi": { "get": { "summary": "Redirect Fastapi", "operationId": "redirect_fastapi_fastapi_get", "responses": {"307": {"description": "Successful Response"}}, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/__init__.py
tests/test_tutorial/test_custom_response/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial008.py
tests/test_tutorial/test_custom_response/test_tutorial008.py
from pathlib import Path from fastapi.testclient import TestClient from docs_src.custom_response import tutorial008_py39 from docs_src.custom_response.tutorial008_py39 import app client = TestClient(app) def test_get(tmp_path: Path): file_path: Path = tmp_path / "large-video-file.mp4" tutorial008_py39.some_file_path = str(file_path) test_content = b"Fake video bytes" file_path.write_bytes(test_content) response = client.get("/") assert response.content == test_content
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial002_tutorial003_tutorial004.py
tests/test_tutorial/test_custom_response/test_tutorial002_tutorial003_tutorial004.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="mod_name", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial003_py39"), pytest.param("tutorial004_py39"), ], ) def get_mod_name(request: pytest.FixtureRequest) -> str: return request.param @pytest.fixture(name="client") def get_client(mod_name: str) -> TestClient: mod = importlib.import_module(f"docs_src.custom_response.{mod_name}") return TestClient(mod.app) html_contents = """ <html> <head> <title>Some HTML in here</title> </head> <body> <h1>Look ma! HTML!</h1> </body> </html> """ def test_get_custom_response(client: TestClient): response = client.get("/items/") assert response.status_code == 200, response.text assert response.text == html_contents def test_openapi_schema(client: TestClient, mod_name: str): if mod_name.startswith("tutorial003"): response_content = {"application/json": {"schema": {}}} else: response_content = {"text/html": {"schema": {"type": "string"}}} response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": response_content, } }, "summary": "Read Items", "operationId": "read_items_items__get", } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_custom_response/test_tutorial009b.py
tests/test_tutorial/test_custom_response/test_tutorial009b.py
from pathlib import Path from fastapi.testclient import TestClient from docs_src.custom_response import tutorial009b_py39 from docs_src.custom_response.tutorial009b_py39 import app client = TestClient(app) def test_get(tmp_path: Path): file_path: Path = tmp_path / "large-video-file.mp4" tutorial009b_py39.some_file_path = str(file_path) test_content = b"Fake video bytes" file_path.write_bytes(test_content) response = client.get("/") assert response.content == test_content
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial003_04.py
tests/test_tutorial/test_response_model/test_tutorial003_04.py
import importlib import pytest from fastapi.exceptions import FastAPIError from ...utils import needs_py310 @pytest.mark.parametrize( "module_name", [ pytest.param("tutorial003_04_py39"), pytest.param("tutorial003_04_py310", marks=needs_py310), ], ) def test_invalid_response_model(module_name: str) -> None: with pytest.raises(FastAPIError): importlib.import_module(f"docs_src.response_model.{module_name}")
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial003_02.py
tests/test_tutorial/test_response_model/test_tutorial003_02.py
from fastapi.testclient import TestClient from docs_src.response_model.tutorial003_02_py39 import app client = TestClient(app) def test_get_portal(): response = client.get("/portal") assert response.status_code == 200, response.text assert response.json() == {"message": "Here's your interdimensional portal."} def test_get_redirect(): response = client.get("/portal", params={"teleport": True}, follow_redirects=False) assert response.status_code == 307, response.text assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ" def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/portal": { "get": { "summary": "Get Portal", "operationId": "get_portal_portal_get", "parameters": [ { "required": False, "schema": { "title": "Teleport", "type": "boolean", "default": False, }, "name": "teleport", "in": "query", } ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial002.py
tests/test_tutorial/test_response_model/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_post_user(client: TestClient): user_data = { "username": "foo", "password": "fighter", "email": "foo@example.com", "full_name": "Grave Dohl", } response = client.post( "/user/", json=user_data, ) assert response.status_code == 200, response.text assert response.json() == user_data def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/user/": { "post": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/UserIn"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Create User", "operationId": "create_user_user__post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/UserIn"} } }, "required": True, }, } } }, "components": { "schemas": { "UserIn": { "title": "UserIn", "required": ["username", "password", "email"], "type": "object", "properties": { "username": {"title": "Username", "type": "string"}, "password": {"title": "Password", "type": "string"}, "email": { "title": "Email", "type": "string", "format": "email", }, "full_name": { "title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}], }, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial003_03.py
tests/test_tutorial/test_response_model/test_tutorial003_03.py
from fastapi.testclient import TestClient from docs_src.response_model.tutorial003_03_py39 import app client = TestClient(app) def test_get_portal(): response = client.get("/teleport", follow_redirects=False) assert response.status_code == 307, response.text assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ" def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/teleport": { "get": { "summary": "Get Teleport", "operationId": "get_teleport_teleport_get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial005.py
tests/test_tutorial/test_response_model/test_tutorial005.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial005_py39"), pytest.param("tutorial005_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_read_item_name(client: TestClient): response = client.get("/items/bar/name") assert response.status_code == 200, response.text assert response.json() == {"name": "Bar", "description": "The Bar fighters"} def test_read_item_public_data(client: TestClient): response = client.get("/items/bar/public") assert response.status_code == 200, response.text assert response.json() == { "name": "Bar", "description": "The Bar fighters", "price": 62, } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}/name": { "get": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Item Name", "operationId": "read_item_name_items__item_id__name_get", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", } ], } }, "/items/{item_id}/public": { "get": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Item Public Data", "operationId": "read_item_public_data_items__item_id__public_get", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", } ], } }, }, "components": { "schemas": { "Item": { "title": "Item", "required": ["name", "price"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"}, "description": { "title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}], }, "tax": {"title": "Tax", "type": "number", "default": 10.5}, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": { "$ref": "#/components/schemas/ValidationError" }, } }, }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial006.py
tests/test_tutorial/test_response_model/test_tutorial006.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial006_py39"), pytest.param("tutorial006_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_read_item_name(client: TestClient): response = client.get("/items/bar/name") assert response.status_code == 200, response.text assert response.json() == {"name": "Bar", "description": "The Bar fighters"} def test_read_item_public_data(client: TestClient): response = client.get("/items/bar/public") assert response.status_code == 200, response.text assert response.json() == { "name": "Bar", "description": "The Bar fighters", "price": 62, } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}/name": { "get": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Item Name", "operationId": "read_item_name_items__item_id__name_get", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", } ], } }, "/items/{item_id}/public": { "get": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Item Public Data", "operationId": "read_item_public_data_items__item_id__public_get", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", } ], } }, }, "components": { "schemas": { "Item": { "title": "Item", "required": ["name", "price"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"}, "description": { "title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}], }, "tax": {"title": "Tax", "type": "number", "default": 10.5}, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": { "$ref": "#/components/schemas/ValidationError" }, } }, }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial003_01.py
tests/test_tutorial/test_response_model/test_tutorial003_01.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial003_01_py39"), pytest.param("tutorial003_01_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_post_user(client: TestClient): response = client.post( "/user/", json={ "username": "foo", "password": "fighter", "email": "foo@example.com", "full_name": "Grave Dohl", }, ) assert response.status_code == 200, response.text assert response.json() == { "username": "foo", "email": "foo@example.com", "full_name": "Grave Dohl", } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/user/": { "post": { "summary": "Create User", "operationId": "create_user_user__post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/UserIn"} } }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/BaseUser" } } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "BaseUser": { "title": "BaseUser", "required": ["username", "email"], "type": "object", "properties": { "username": {"title": "Username", "type": "string"}, "email": { "title": "Email", "type": "string", "format": "email", }, "full_name": { "title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}], }, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": { "$ref": "#/components/schemas/ValidationError" }, } }, }, "UserIn": { "title": "UserIn", "required": ["username", "email", "password"], "type": "object", "properties": { "username": {"title": "Username", "type": "string"}, "email": { "title": "Email", "type": "string", "format": "email", }, "full_name": { "title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}], }, "password": {"title": "Password", "type": "string"}, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/__init__.py
tests/test_tutorial/test_response_model/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial004.py
tests/test_tutorial/test_response_model/test_tutorial004.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial004_py39"), pytest.param("tutorial004_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client @pytest.mark.parametrize( "url,data", [ ("/items/foo", {"name": "Foo", "price": 50.2}), ( "/items/bar", {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2}, ), ( "/items/baz", { "name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": [], }, ), ], ) def test_get(url, data, client: TestClient): response = client.get(url) assert response.status_code == 200, response.text assert response.json() == data def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/{item_id}": { "get": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"} } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Item", "operationId": "read_item_items__item_id__get", "parameters": [ { "required": True, "schema": {"title": "Item Id", "type": "string"}, "name": "item_id", "in": "path", } ], } } }, "components": { "schemas": { "Item": { "title": "Item", "required": ["name", "price"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"}, "description": { "title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}], }, "tax": {"title": "Tax", "type": "number", "default": 10.5}, "tags": { "title": "Tags", "type": "array", "items": {"type": "string"}, "default": [], }, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": { "$ref": "#/components/schemas/ValidationError" }, } }, }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial003_05.py
tests/test_tutorial/test_response_model/test_tutorial003_05.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial003_05_py39"), pytest.param("tutorial003_05_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_get_portal(client: TestClient): response = client.get("/portal") assert response.status_code == 200, response.text assert response.json() == {"message": "Here's your interdimensional portal."} def test_get_redirect(client: TestClient): response = client.get("/portal", params={"teleport": True}, follow_redirects=False) assert response.status_code == 307, response.text assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ" def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/portal": { "get": { "summary": "Get Portal", "operationId": "get_portal_portal_get", "parameters": [ { "required": False, "schema": { "title": "Teleport", "type": "boolean", "default": False, }, "name": "teleport", "in": "query", } ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial001_tutorial001_01.py
tests/test_tutorial/test_response_model/test_tutorial001_tutorial001_01.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), pytest.param("tutorial001_01_py39"), pytest.param("tutorial001_01_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_read_items(client: TestClient): response = client.get("/items/") assert response.status_code == 200, response.text assert response.json() == [ { "name": "Portal Gun", "description": None, "price": 42.0, "tags": [], "tax": None, }, { "name": "Plumbus", "description": None, "price": 32.0, "tags": [], "tax": None, }, ] def test_create_item(client: TestClient): item_data = { "name": "Test Item", "description": "A test item", "price": 10.5, "tax": 1.5, "tags": ["test", "item"], } response = client.post("/items/", json=item_data) assert response.status_code == 200, response.text assert response.json() == item_data def test_create_item_only_required(client: TestClient): response = client.post( "/items/", json={ "name": "Test Item", "price": 10.5, }, ) assert response.status_code == 200, response.text assert response.json() == { "name": "Test Item", "price": 10.5, "description": None, "tax": None, "tags": [], } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "type": "array", "items": {"$ref": "#/components/schemas/Item"}, "title": "Response Read Items Items Get", } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", }, "post": { "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Item", }, }, }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Item"}, } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Create Item", "operationId": "create_item_items__post", }, } }, "components": { "schemas": { "Item": { "title": "Item", "required": ["name", "price"], "type": "object", "properties": { "name": {"title": "Name", "type": "string"}, "price": {"title": "Price", "type": "number"}, "description": { "title": "Description", "anyOf": [{"type": "string"}, {"type": "null"}], }, "tax": { "title": "Tax", "anyOf": [{"type": "number"}, {"type": "null"}], }, "tags": { "title": "Tags", "type": "array", "items": {"type": "string"}, "default": [], }, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_response_model/test_tutorial003.py
tests/test_tutorial/test_response_model/test_tutorial003.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial003_py39"), pytest.param("tutorial003_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.response_model.{request.param}") client = TestClient(mod.app) return client def test_post_user(client: TestClient): response = client.post( "/user/", json={ "username": "foo", "password": "fighter", "email": "foo@example.com", "full_name": "Grave Dohl", }, ) assert response.status_code == 200, response.text assert response.json() == { "username": "foo", "email": "foo@example.com", "full_name": "Grave Dohl", } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/user/": { "post": { "responses": { "200": { "description": "Successful Response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/UserOut" } } }, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Create User", "operationId": "create_user_user__post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/UserIn"} } }, "required": True, }, } } }, "components": { "schemas": { "UserOut": { "title": "UserOut", "required": ["username", "email"], "type": "object", "properties": { "username": {"title": "Username", "type": "string"}, "email": { "title": "Email", "type": "string", "format": "email", }, "full_name": { "title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}], }, }, }, "UserIn": { "title": "UserIn", "required": ["username", "password", "email"], "type": "object", "properties": { "username": {"title": "Username", "type": "string"}, "password": {"title": "Password", "type": "string"}, "email": { "title": "Email", "type": "string", "format": "email", }, "full_name": { "title": "Full Name", "anyOf": [{"type": "string"}, {"type": "null"}], }, }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": { "$ref": "#/components/schemas/ValidationError" }, } }, }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_param_models/test_tutorial002.py
tests/test_tutorial/test_query_param_models/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=[needs_py310]), pytest.param("tutorial002_an_py39"), pytest.param("tutorial002_an_py310", marks=[needs_py310]), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.query_param_models.{request.param}") client = TestClient(mod.app) return client def test_query_param_model(client: TestClient): response = client.get( "/items/", params={ "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], }, ) assert response.status_code == 200 assert response.json() == { "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], } def test_query_param_model_defaults(client: TestClient): response = client.get("/items/") assert response.status_code == 200 assert response.json() == { "limit": 100, "offset": 0, "order_by": "created_at", "tags": [], } def test_query_param_model_invalid(client: TestClient): response = client.get( "/items/", params={ "limit": 150, "offset": -1, "order_by": "invalid", }, ) assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "less_than_equal", "loc": ["query", "limit"], "msg": "Input should be less than or equal to 100", "input": "150", "ctx": {"le": 100}, }, { "type": "greater_than_equal", "loc": ["query", "offset"], "msg": "Input should be greater than or equal to 0", "input": "-1", "ctx": {"ge": 0}, }, { "type": "literal_error", "loc": ["query", "order_by"], "msg": "Input should be 'created_at' or 'updated_at'", "input": "invalid", "ctx": {"expected": "'created_at' or 'updated_at'"}, }, ] } ) def test_query_param_model_extra(client: TestClient): response = client.get( "/items/", params={ "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], "tool": "plumbus", }, ) assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "extra_forbidden", "loc": ["query", "tool"], "msg": "Extra inputs are not permitted", "input": "plumbus", } ] } ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "limit", "in": "query", "required": False, "schema": { "type": "integer", "maximum": 100, "exclusiveMinimum": 0, "default": 100, "title": "Limit", }, }, { "name": "offset", "in": "query", "required": False, "schema": { "type": "integer", "minimum": 0, "default": 0, "title": "Offset", }, }, { "name": "order_by", "in": "query", "required": False, "schema": { "enum": ["created_at", "updated_at"], "type": "string", "default": "created_at", "title": "Order By", }, }, { "name": "tags", "in": "query", "required": False, "schema": { "type": "array", "items": {"type": "string"}, "default": [], "title": "Tags", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_param_models/test_tutorial001.py
tests/test_tutorial/test_query_param_models/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), pytest.param("tutorial001_an_py39"), pytest.param("tutorial001_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.query_param_models.{request.param}") client = TestClient(mod.app) return client def test_query_param_model(client: TestClient): response = client.get( "/items/", params={ "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], }, ) assert response.status_code == 200 assert response.json() == { "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], } def test_query_param_model_defaults(client: TestClient): response = client.get("/items/") assert response.status_code == 200 assert response.json() == { "limit": 100, "offset": 0, "order_by": "created_at", "tags": [], } def test_query_param_model_invalid(client: TestClient): response = client.get( "/items/", params={ "limit": 150, "offset": -1, "order_by": "invalid", }, ) assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "less_than_equal", "loc": ["query", "limit"], "msg": "Input should be less than or equal to 100", "input": "150", "ctx": {"le": 100}, }, { "type": "greater_than_equal", "loc": ["query", "offset"], "msg": "Input should be greater than or equal to 0", "input": "-1", "ctx": {"ge": 0}, }, { "type": "literal_error", "loc": ["query", "order_by"], "msg": "Input should be 'created_at' or 'updated_at'", "input": "invalid", "ctx": {"expected": "'created_at' or 'updated_at'"}, }, ] } ) def test_query_param_model_extra(client: TestClient): response = client.get( "/items/", params={ "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], "tool": "plumbus", }, ) assert response.status_code == 200 assert response.json() == { "limit": 10, "offset": 5, "order_by": "updated_at", "tags": ["tag1", "tag2"], } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "limit", "in": "query", "required": False, "schema": { "type": "integer", "maximum": 100, "exclusiveMinimum": 0, "default": 100, "title": "Limit", }, }, { "name": "offset", "in": "query", "required": False, "schema": { "type": "integer", "minimum": 0, "default": 0, "title": "Offset", }, }, { "name": "order_by", "in": "query", "required": False, "schema": { "enum": ["created_at", "updated_at"], "type": "string", "default": "created_at", "title": "Order By", }, }, { "name": "tags", "in": "query", "required": False, "schema": { "type": "array", "items": {"type": "string"}, "default": [], "title": "Tags", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_param_models/__init__.py
tests/test_tutorial/test_query_param_models/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_request_form_models/test_tutorial002.py
tests/test_tutorial/test_request_form_models/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ "tutorial002_py39", "tutorial002_an_py39", ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.request_form_models.{request.param}") client = TestClient(mod.app) return client def test_post_body_form(client: TestClient): response = client.post("/login/", data={"username": "Foo", "password": "secret"}) assert response.status_code == 200 assert response.json() == {"username": "Foo", "password": "secret"} def test_post_body_extra_form(client: TestClient): response = client.post( "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"} ) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "extra_forbidden", "loc": ["body", "extra"], "msg": "Extra inputs are not permitted", "input": "extra", } ] } def test_post_body_form_no_password(client: TestClient): response = client.post("/login/", data={"username": "Foo"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "password"], "msg": "Field required", "input": {"username": "Foo"}, } ] } def test_post_body_form_no_username(client: TestClient): response = client.post("/login/", data={"password": "secret"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "username"], "msg": "Field required", "input": {"password": "secret"}, } ] } def test_post_body_form_no_data(client: TestClient): response = client.post("/login/") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "username"], "msg": "Field required", "input": {}, }, { "type": "missing", "loc": ["body", "password"], "msg": "Field required", "input": {}, }, ] } def test_post_body_json(client: TestClient): response = client.post("/login/", json={"username": "Foo", "password": "secret"}) assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "username"], "msg": "Field required", "input": {}, }, { "type": "missing", "loc": ["body", "password"], "msg": "Field required", "input": {}, }, ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/login/": { "post": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Login", "operationId": "login_login__post", "requestBody": { "content": { "application/x-www-form-urlencoded": { "schema": {"$ref": "#/components/schemas/FormData"} } }, "required": True, }, } } }, "components": { "schemas": { "FormData": { "properties": { "username": {"type": "string", "title": "Username"}, "password": {"type": "string", "title": "Password"}, }, "additionalProperties": False, "type": "object", "required": ["username", "password"], "title": "FormData", }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_request_form_models/test_tutorial001.py
tests/test_tutorial/test_request_form_models/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient @pytest.fixture( name="client", params=[ "tutorial001_py39", "tutorial001_an_py39", ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.request_form_models.{request.param}") client = TestClient(mod.app) return client def test_post_body_form(client: TestClient): response = client.post("/login/", data={"username": "Foo", "password": "secret"}) assert response.status_code == 200 assert response.json() == {"username": "Foo", "password": "secret"} def test_post_body_form_no_password(client: TestClient): response = client.post("/login/", data={"username": "Foo"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "password"], "msg": "Field required", "input": {"username": "Foo"}, } ] } def test_post_body_form_no_username(client: TestClient): response = client.post("/login/", data={"password": "secret"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "username"], "msg": "Field required", "input": {"password": "secret"}, } ] } def test_post_body_form_no_data(client: TestClient): response = client.post("/login/") assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "username"], "msg": "Field required", "input": {}, }, { "type": "missing", "loc": ["body", "password"], "msg": "Field required", "input": {}, }, ] } def test_post_body_json(client: TestClient): response = client.post("/login/", json={"username": "Foo", "password": "secret"}) assert response.status_code == 422, response.text assert response.json() == { "detail": [ { "type": "missing", "loc": ["body", "username"], "msg": "Field required", "input": {}, }, { "type": "missing", "loc": ["body", "password"], "msg": "Field required", "input": {}, }, ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/login/": { "post": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Login", "operationId": "login_login__post", "requestBody": { "content": { "application/x-www-form-urlencoded": { "schema": {"$ref": "#/components/schemas/FormData"} } }, "required": True, }, } } }, "components": { "schemas": { "FormData": { "properties": { "username": {"type": "string", "title": "Username"}, "password": {"type": "string", "title": "Password"}, }, "type": "object", "required": ["username", "password"], "title": "FormData", }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_request_form_models/__init__.py
tests/test_tutorial/test_request_form_models/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_param_models/test_tutorial002.py
tests/test_tutorial/test_header_param_models/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=[needs_py310]), pytest.param("tutorial002_an_py39"), pytest.param("tutorial002_an_py310", marks=[needs_py310]), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.header_param_models.{request.param}") client = TestClient(mod.app) client.headers.clear() return client def test_header_param_model(client: TestClient): response = client.get( "/items/", headers=[ ("save-data", "true"), ("if-modified-since", "yesterday"), ("traceparent", "123"), ("x-tag", "one"), ("x-tag", "two"), ], ) assert response.status_code == 200, response.text assert response.json() == { "host": "testserver", "save_data": True, "if_modified_since": "yesterday", "traceparent": "123", "x_tag": ["one", "two"], } def test_header_param_model_defaults(client: TestClient): response = client.get("/items/", headers=[("save-data", "true")]) assert response.status_code == 200 assert response.json() == { "host": "testserver", "save_data": True, "if_modified_since": None, "traceparent": None, "x_tag": [], } def test_header_param_model_invalid(client: TestClient): response = client.get("/items/") assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "missing", "loc": ["header", "save_data"], "msg": "Field required", "input": {"x_tag": [], "host": "testserver"}, } ] } ) def test_header_param_model_extra(client: TestClient): response = client.get( "/items/", headers=[("save-data", "true"), ("tool", "plumbus")] ) assert response.status_code == 422, response.text assert response.json() == snapshot( { "detail": [ { "type": "extra_forbidden", "loc": ["header", "tool"], "msg": "Extra inputs are not permitted", "input": "plumbus", } ] } ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "host", "in": "header", "required": True, "schema": {"type": "string", "title": "Host"}, }, { "name": "save-data", "in": "header", "required": True, "schema": {"type": "boolean", "title": "Save Data"}, }, { "name": "if-modified-since", "in": "header", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "If Modified Since", }, }, { "name": "traceparent", "in": "header", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Traceparent", }, }, { "name": "x-tag", "in": "header", "required": False, "schema": { "type": "array", "items": {"type": "string"}, "default": [], "title": "X Tag", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_param_models/test_tutorial001.py
tests/test_tutorial/test_header_param_models/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), pytest.param("tutorial001_an_py39"), pytest.param("tutorial001_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.header_param_models.{request.param}") client = TestClient(mod.app) return client def test_header_param_model(client: TestClient): response = client.get( "/items/", headers=[ ("save-data", "true"), ("if-modified-since", "yesterday"), ("traceparent", "123"), ("x-tag", "one"), ("x-tag", "two"), ], ) assert response.status_code == 200 assert response.json() == { "host": "testserver", "save_data": True, "if_modified_since": "yesterday", "traceparent": "123", "x_tag": ["one", "two"], } def test_header_param_model_defaults(client: TestClient): response = client.get("/items/", headers=[("save-data", "true")]) assert response.status_code == 200 assert response.json() == { "host": "testserver", "save_data": True, "if_modified_since": None, "traceparent": None, "x_tag": [], } def test_header_param_model_invalid(client: TestClient): response = client.get("/items/") assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "missing", "loc": ["header", "save_data"], "msg": "Field required", "input": { "x_tag": [], "host": "testserver", "accept": "*/*", "accept-encoding": "gzip, deflate", "connection": "keep-alive", "user-agent": "testclient", }, } ] } ) def test_header_param_model_extra(client: TestClient): response = client.get( "/items/", headers=[("save-data", "true"), ("tool", "plumbus")] ) assert response.status_code == 200, response.text assert response.json() == snapshot( { "host": "testserver", "save_data": True, "if_modified_since": None, "traceparent": None, "x_tag": [], } ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "host", "in": "header", "required": True, "schema": {"type": "string", "title": "Host"}, }, { "name": "save-data", "in": "header", "required": True, "schema": {"type": "boolean", "title": "Save Data"}, }, { "name": "if-modified-since", "in": "header", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "If Modified Since", }, }, { "name": "traceparent", "in": "header", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Traceparent", }, }, { "name": "x-tag", "in": "header", "required": False, "schema": { "type": "array", "items": {"type": "string"}, "default": [], "title": "X Tag", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_param_models/__init__.py
tests/test_tutorial/test_header_param_models/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_header_param_models/test_tutorial003.py
tests/test_tutorial/test_header_param_models/test_tutorial003.py
import importlib import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot from tests.utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial003_py39"), pytest.param("tutorial003_py310", marks=needs_py310), pytest.param("tutorial003_an_py39"), pytest.param("tutorial003_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.header_param_models.{request.param}") client = TestClient(mod.app) return client def test_header_param_model(client: TestClient): response = client.get( "/items/", headers=[ ("save_data", "true"), ("if_modified_since", "yesterday"), ("traceparent", "123"), ("x_tag", "one"), ("x_tag", "two"), ], ) assert response.status_code == 200 assert response.json() == { "host": "testserver", "save_data": True, "if_modified_since": "yesterday", "traceparent": "123", "x_tag": ["one", "two"], } def test_header_param_model_no_underscore(client: TestClient): response = client.get( "/items/", headers=[ ("save-data", "true"), ("if-modified-since", "yesterday"), ("traceparent", "123"), ("x-tag", "one"), ("x-tag", "two"), ], ) assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "missing", "loc": ["header", "save_data"], "msg": "Field required", "input": { "host": "testserver", "traceparent": "123", "x_tag": [], "accept": "*/*", "accept-encoding": "gzip, deflate", "connection": "keep-alive", "user-agent": "testclient", "save-data": "true", "if-modified-since": "yesterday", "x-tag": ["one", "two"], }, } ] } ) def test_header_param_model_defaults(client: TestClient): response = client.get("/items/", headers=[("save_data", "true")]) assert response.status_code == 200 assert response.json() == { "host": "testserver", "save_data": True, "if_modified_since": None, "traceparent": None, "x_tag": [], } def test_header_param_model_invalid(client: TestClient): response = client.get("/items/") assert response.status_code == 422 assert response.json() == snapshot( { "detail": [ { "type": "missing", "loc": ["header", "save_data"], "msg": "Field required", "input": { "x_tag": [], "host": "testserver", "accept": "*/*", "accept-encoding": "gzip, deflate", "connection": "keep-alive", "user-agent": "testclient", }, } ] } ) def test_header_param_model_extra(client: TestClient): response = client.get( "/items/", headers=[("save_data", "true"), ("tool", "plumbus")] ) assert response.status_code == 200, response.text assert response.json() == snapshot( { "host": "testserver", "save_data": True, "if_modified_since": None, "traceparent": None, "x_tag": [], } ) def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == snapshot( { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "name": "host", "in": "header", "required": True, "schema": {"type": "string", "title": "Host"}, }, { "name": "save_data", "in": "header", "required": True, "schema": {"type": "boolean", "title": "Save Data"}, }, { "name": "if_modified_since", "in": "header", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "If Modified Since", }, }, { "name": "traceparent", "in": "header", "required": False, "schema": { "anyOf": [{"type": "string"}, {"type": "null"}], "title": "Traceparent", }, }, { "name": "x_tag", "in": "header", "required": False, "schema": { "type": "array", "items": {"type": "string"}, "default": [], "title": "X Tag", }, }, ], "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": { "$ref": "#/components/schemas/ValidationError" }, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, } )
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_middleware/test_tutorial001.py
tests/test_tutorial/test_middleware/test_tutorial001.py
from fastapi.testclient import TestClient from docs_src.middleware.tutorial001_py39 import app client = TestClient(app) def test_response_headers(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert "X-Process-Time" in response.headers def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": { "title": "FastAPI", "version": "0.1.0", }, "paths": {}, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_middleware/__init__.py
tests/test_tutorial/test_middleware/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_openapi_webhooks/test_tutorial001.py
tests/test_tutorial/test_openapi_webhooks/test_tutorial001.py
from fastapi.testclient import TestClient from docs_src.openapi_webhooks.tutorial001_py39 import app client = TestClient(app) def test_get(): response = client.get("/users/") assert response.status_code == 200, response.text assert response.json() == ["Rick", "Morty"] def test_dummy_webhook(): # Just for coverage app.webhooks.routes[0].endpoint({}) def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/users/": { "get": { "summary": "Read Users", "operationId": "read_users_users__get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, } } }, "webhooks": { "new-subscription": { "post": { "summary": "New Subscription", "description": "When a new user subscribes to your service we'll send you a POST request with this\ndata to the URL that you register for the event `new-subscription` in the dashboard.", "operationId": "new_subscriptionnew_subscription_post", "requestBody": { "content": { "application/json": { "schema": {"$ref": "#/components/schemas/Subscription"} } }, "required": True, }, "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "properties": { "detail": { "items": {"$ref": "#/components/schemas/ValidationError"}, "type": "array", "title": "Detail", } }, "type": "object", "title": "HTTPValidationError", }, "Subscription": { "properties": { "username": {"type": "string", "title": "Username"}, "monthly_fee": {"type": "number", "title": "Monthly Fee"}, "start_date": { "type": "string", "format": "date-time", "title": "Start Date", }, }, "type": "object", "required": ["username", "monthly_fee", "start_date"], "title": "Subscription", }, "ValidationError": { "properties": { "loc": { "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, "type": "array", "title": "Location", }, "msg": {"type": "string", "title": "Message"}, "type": {"type": "string", "title": "Error Type"}, }, "type": "object", "required": ["loc", "msg", "type"], "title": "ValidationError", }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_openapi_webhooks/__init__.py
tests/test_tutorial/test_openapi_webhooks/__init__.py
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_params_str_validations/test_tutorial007.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial007.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial007_py39"), pytest.param("tutorial007_py310", marks=needs_py310), pytest.param("tutorial007_an_py39"), pytest.param("tutorial007_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.query_params_str_validations.{request.param}" ) client = TestClient(mod.app) return client def test_query_params_str_validations_no_query(client: TestClient): response = client.get("/items/") assert response.status_code == 200 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} def test_query_params_str_validations_q_fixedquery(client: TestClient): response = client.get("/items/", params={"q": "fixedquery"}) assert response.status_code == 200 assert response.json() == { "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], "q": "fixedquery", } def test_query_params_str_validations_q_fixedquery_too_short(client: TestClient): response = client.get("/items/", params={"q": "fa"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "string_too_short", "loc": ["query", "q"], "msg": "String should have at least 3 characters", "input": "fa", "ctx": {"min_length": 3}, } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": False, "schema": { "anyOf": [ { "type": "string", "minLength": 3, }, {"type": "null"}, ], "title": "Query string", }, "name": "q", "in": "query", } ], } } }, "components": { "schemas": { "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_params_str_validations/test_tutorial002.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial002.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial002_py39"), pytest.param("tutorial002_py310", marks=needs_py310), pytest.param("tutorial002_an_py39"), pytest.param("tutorial002_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.query_params_str_validations.{request.param}" ) client = TestClient(mod.app) return client def test_query_params_str_validations_no_query(client: TestClient): response = client.get("/items/") assert response.status_code == 200 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} def test_query_params_str_validations_q_empty_str(client: TestClient): response = client.get("/items/", params={"q": ""}) assert response.status_code == 200 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} def test_query_params_str_validations_q_query(client: TestClient): response = client.get("/items/", params={"q": "query"}) assert response.status_code == 200 assert response.json() == { "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], "q": "query", } def test_query_params_str_validations_q_too_long(client: TestClient): response = client.get("/items/", params={"q": "q" * 51}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "string_too_long", "loc": ["query", "q"], "msg": "String should have at most 50 characters", "input": "q" * 51, "ctx": {"max_length": 50}, } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": False, "schema": { "anyOf": [ { "type": "string", "maxLength": 50, }, {"type": "null"}, ], "title": "Q", }, "name": "q", "in": "query", } ], } } }, "components": { "schemas": { "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_params_str_validations/test_tutorial014.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial014.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial014_py39"), pytest.param("tutorial014_py310", marks=needs_py310), pytest.param("tutorial014_an_py39"), pytest.param("tutorial014_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.query_params_str_validations.{request.param}" ) client = TestClient(mod.app) return client def test_hidden_query(client: TestClient): response = client.get("/items?hidden_query=somevalue") assert response.status_code == 200, response.text assert response.json() == {"hidden_query": "somevalue"} def test_no_hidden_query(client: TestClient): response = client.get("/items") assert response.status_code == 200, response.text assert response.json() == {"hidden_query": "Not found"} def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "summary": "Read Items", "operationId": "read_items_items__get", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, } } }, "components": { "schemas": { "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_params_str_validations/test_tutorial001.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial001.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial001_py39"), pytest.param("tutorial001_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.query_params_str_validations.{request.param}" ) client = TestClient(mod.app) return client def test_query_params_str_validations_no_query(client: TestClient): response = client.get("/items/") assert response.status_code == 200 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} def test_query_params_str_validations_q_empty_str(client: TestClient): response = client.get("/items/", params={"q": ""}) assert response.status_code == 200 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} def test_query_params_str_validations_q_query(client: TestClient): response = client.get("/items/", params={"q": "query"}) assert response.status_code == 200 assert response.json() == { "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], "q": "query", } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": False, "schema": { "anyOf": [ {"type": "string"}, {"type": "null"}, ], "title": "Q", }, "name": "q", "in": "query", } ], } } }, "components": { "schemas": { "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false
fastapi/fastapi
https://github.com/fastapi/fastapi/blob/f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a/tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py
import importlib import pytest from fastapi.testclient import TestClient from ...utils import needs_py310 @pytest.fixture( name="client", params=[ pytest.param("tutorial006c_py39"), pytest.param("tutorial006c_py310", marks=needs_py310), pytest.param("tutorial006c_an_py39"), pytest.param("tutorial006c_an_py310", marks=needs_py310), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module( f"docs_src.query_params_str_validations.{request.param}" ) client = TestClient(mod.app) return client @pytest.mark.xfail( reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419" ) def test_query_params_str_validations_no_query(client: TestClient): response = client.get("/items/") assert response.status_code == 200 assert response.json() == { # pragma: no cover "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], } @pytest.mark.xfail( reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419" ) def test_query_params_str_validations_empty_str(client: TestClient): response = client.get("/items/?q=") assert response.status_code == 200 assert response.json() == { # pragma: no cover "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], } def test_query_params_str_validations_q_query(client: TestClient): response = client.get("/items/", params={"q": "query"}) assert response.status_code == 200 assert response.json() == { "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], "q": "query", } def test_query_params_str_validations_q_short(client: TestClient): response = client.get("/items/", params={"q": "fa"}) assert response.status_code == 422 assert response.json() == { "detail": [ { "type": "string_too_short", "loc": ["query", "q"], "msg": "String should have at least 3 characters", "input": "fa", "ctx": {"min_length": 3}, } ] } def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/items/": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, "422": { "description": "Validation Error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, }, }, "summary": "Read Items", "operationId": "read_items_items__get", "parameters": [ { "required": True, "schema": { "anyOf": [ {"type": "string", "minLength": 3}, {"type": "null"}, ], "title": "Q", }, "name": "q", "in": "query", } ], } } }, "components": { "schemas": { "ValidationError": { "title": "ValidationError", "required": ["loc", "msg", "type"], "type": "object", "properties": { "loc": { "title": "Location", "type": "array", "items": { "anyOf": [{"type": "string"}, {"type": "integer"}] }, }, "msg": {"title": "Message", "type": "string"}, "type": {"title": "Error Type", "type": "string"}, }, }, "HTTPValidationError": { "title": "HTTPValidationError", "type": "object", "properties": { "detail": { "title": "Detail", "type": "array", "items": {"$ref": "#/components/schemas/ValidationError"}, } }, }, } }, }
python
MIT
f2687dc1bb01f4cb62eee90e656b61c38c2aaf6a
2026-01-04T14:38:15.465144Z
false