fastapi

Форк
0
/
test_reponse_set_reponse_code_empty.py 
97 строк · 3.1 Кб
1
from typing import Any
2

3
from fastapi import FastAPI, Response
4
from fastapi.testclient import TestClient
5

6
app = FastAPI()
7

8

9
@app.delete(
10
    "/{id}",
11
    status_code=204,
12
    response_model=None,
13
)
14
async def delete_deployment(
15
    id: int,
16
    response: Response,
17
) -> Any:
18
    response.status_code = 400
19
    return {"msg": "Status overwritten", "id": id}
20

21

22
client = TestClient(app)
23

24

25
def test_dependency_set_status_code():
26
    response = client.delete("/1")
27
    assert response.status_code == 400 and response.content
28
    assert response.json() == {"msg": "Status overwritten", "id": 1}
29

30

31
def test_openapi_schema():
32
    response = client.get("/openapi.json")
33
    assert response.status_code == 200, response.text
34
    assert response.json() == {
35
        "openapi": "3.1.0",
36
        "info": {"title": "FastAPI", "version": "0.1.0"},
37
        "paths": {
38
            "/{id}": {
39
                "delete": {
40
                    "summary": "Delete Deployment",
41
                    "operationId": "delete_deployment__id__delete",
42
                    "parameters": [
43
                        {
44
                            "required": True,
45
                            "schema": {"title": "Id", "type": "integer"},
46
                            "name": "id",
47
                            "in": "path",
48
                        }
49
                    ],
50
                    "responses": {
51
                        "204": {"description": "Successful Response"},
52
                        "422": {
53
                            "description": "Validation Error",
54
                            "content": {
55
                                "application/json": {
56
                                    "schema": {
57
                                        "$ref": "#/components/schemas/HTTPValidationError"
58
                                    }
59
                                }
60
                            },
61
                        },
62
                    },
63
                }
64
            }
65
        },
66
        "components": {
67
            "schemas": {
68
                "HTTPValidationError": {
69
                    "title": "HTTPValidationError",
70
                    "type": "object",
71
                    "properties": {
72
                        "detail": {
73
                            "title": "Detail",
74
                            "type": "array",
75
                            "items": {"$ref": "#/components/schemas/ValidationError"},
76
                        }
77
                    },
78
                },
79
                "ValidationError": {
80
                    "title": "ValidationError",
81
                    "required": ["loc", "msg", "type"],
82
                    "type": "object",
83
                    "properties": {
84
                        "loc": {
85
                            "title": "Location",
86
                            "type": "array",
87
                            "items": {
88
                                "anyOf": [{"type": "string"}, {"type": "integer"}]
89
                            },
90
                        },
91
                        "msg": {"title": "Message", "type": "string"},
92
                        "type": {"title": "Error Type", "type": "string"},
93
                    },
94
                },
95
            }
96
        },
97
    }
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.