From 01a33294c383cea24dd7ec2ec16cf4beed2656fb Mon Sep 17 00:00:00 2001 From: Volodymyr Kochetkov Date: Sun, 14 Jan 2024 10:20:53 +0200 Subject: [PATCH] feat: issue-2008 add tests --- tests/test_auto_options_route.py | 284 +++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 tests/test_auto_options_route.py diff --git a/tests/test_auto_options_route.py b/tests/test_auto_options_route.py new file mode 100644 index 000000000..08595a89f --- /dev/null +++ b/tests/test_auto_options_route.py @@ -0,0 +1,284 @@ +from fastapi import FastAPI +from fastapi.responses import JSONResponse +from fastapi.testclient import TestClient +from pydantic import BaseModel + +app = FastAPI() + + +class Item(BaseModel): + name: str + + +@app.get("/home", add_auto_options_route=True) +def get_home(): + return {"hello": "world"} + + +@app.post("/items/", add_auto_options_route=True) +def create_item(item: Item): + return item + + +@app.head("/items/{item_id}", add_auto_options_route=True) +def head_item(item_id: str): + return JSONResponse(None, headers={"x-fastapi-item-id": item_id}) + + +@app.patch("/items/{item_id}", add_auto_options_route=True) +def patch_item(item_id: str, item: Item): + return {"item_id": item_id, "item": item} + + +@app.trace("/items/{item_id}", add_auto_options_route=True) +def trace_item(item_id: str): + return JSONResponse(None, media_type="message/http") + + +client = TestClient(app) + + +def test_get_api_route(): + response = client.get("/home") + assert response.status_code == 200, response.text + assert response.json() == {"hello": "world"} + + +def test_get_auto_options(): + response = client.options("/home") + assert response.status_code == 200, response.text + assert response.headers.raw[0][0].decode("utf-8") == "allow" + assert response.headers.raw[0][1].decode("utf-8") == "GET" + + +def test_post_auto_options(): + response = client.options("/items/") + assert response.status_code == 200, response.text + assert response.headers.raw[0][0].decode("utf-8") == "allow" + assert response.headers.raw[0][1].decode("utf-8") == "POST" + + +def test_other_auto_options(): + response = client.options("/items/foo") + assert response.status_code == 200, response.text + assert response.headers.raw[0][0].decode("utf-8") == "allow" + assert set(response.headers.raw[0][1].decode("utf-8").split(", ")) == { + "HEAD", + "PATCH", + "TRACE", + } + + +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": { + "/home": { + "get": { + "summary": "Get Home", + "operationId": "get_home_home_get", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + }, + "options": { + "summary": "Options Route", + "operationId": "options_route_home_options", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + }, + }, + "/items/": { + "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" + } + } + }, + }, + }, + }, + "options": { + "summary": "Options Route", + "operationId": "options_route_items__options", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + }, + }, + "/items/{item_id}": { + "head": { + "summary": "Head Item", + "operationId": "head_item_items__item_id__head", + "parameters": [ + { + "name": "item_id", + "in": "path", + "required": True, + "schema": {"type": "string", "title": "Item Id"}, + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + }, + }, + }, + }, + "patch": { + "summary": "Patch Item", + "operationId": "patch_item_items__item_id__patch", + "parameters": [ + { + "name": "item_id", + "in": "path", + "required": True, + "schema": {"type": "string", "title": "Item Id"}, + } + ], + "requestBody": { + "required": True, + "content": { + "application/json": { + "schema": {"$ref": "#/components/schemas/Item"} + } + }, + }, + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + }, + }, + }, + }, + "trace": { + "summary": "Trace Item", + "operationId": "trace_item_items__item_id__trace", + "parameters": [ + { + "name": "item_id", + "in": "path", + "required": True, + "schema": {"type": "string", "title": "Item Id"}, + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + }, + }, + }, + }, + "options": { + "summary": "Options Route", + "operationId": "options_route_items__item_id__options", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + }, + }, + }, + "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"}}, + "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", + }, + } + }, + }