fastapi

Форк
0
/
test_response_code_no_body.py 
114 строк · 3.2 Кб
1
import typing
2

3
from fastapi import FastAPI
4
from fastapi.responses import JSONResponse
5
from fastapi.testclient import TestClient
6
from pydantic import BaseModel
7

8
app = FastAPI()
9

10

11
class JsonApiResponse(JSONResponse):
12
    media_type = "application/vnd.api+json"
13

14

15
class Error(BaseModel):
16
    status: str
17
    title: str
18

19

20
class JsonApiError(BaseModel):
21
    errors: typing.List[Error]
22

23

24
@app.get(
25
    "/a",
26
    status_code=204,
27
    response_class=JsonApiResponse,
28
    responses={500: {"description": "Error", "model": JsonApiError}},
29
)
30
async def a():
31
    pass
32

33

34
@app.get("/b", responses={204: {"description": "No Content"}})
35
async def b():
36
    pass  # pragma: no cover
37

38

39
client = TestClient(app)
40

41

42
def test_get_response():
43
    response = client.get("/a")
44
    assert response.status_code == 204, response.text
45
    assert "content-length" not in response.headers
46
    assert response.content == b""
47

48

49
def test_openapi_schema():
50
    response = client.get("/openapi.json")
51
    assert response.status_code == 200, response.text
52
    assert response.json() == {
53
        "openapi": "3.1.0",
54
        "info": {"title": "FastAPI", "version": "0.1.0"},
55
        "paths": {
56
            "/a": {
57
                "get": {
58
                    "responses": {
59
                        "500": {
60
                            "description": "Error",
61
                            "content": {
62
                                "application/vnd.api+json": {
63
                                    "schema": {
64
                                        "$ref": "#/components/schemas/JsonApiError"
65
                                    }
66
                                }
67
                            },
68
                        },
69
                        "204": {"description": "Successful Response"},
70
                    },
71
                    "summary": "A",
72
                    "operationId": "a_a_get",
73
                }
74
            },
75
            "/b": {
76
                "get": {
77
                    "responses": {
78
                        "204": {"description": "No Content"},
79
                        "200": {
80
                            "description": "Successful Response",
81
                            "content": {"application/json": {"schema": {}}},
82
                        },
83
                    },
84
                    "summary": "B",
85
                    "operationId": "b_b_get",
86
                }
87
            },
88
        },
89
        "components": {
90
            "schemas": {
91
                "Error": {
92
                    "title": "Error",
93
                    "required": ["status", "title"],
94
                    "type": "object",
95
                    "properties": {
96
                        "status": {"title": "Status", "type": "string"},
97
                        "title": {"title": "Title", "type": "string"},
98
                    },
99
                },
100
                "JsonApiError": {
101
                    "title": "JsonApiError",
102
                    "required": ["errors"],
103
                    "type": "object",
104
                    "properties": {
105
                        "errors": {
106
                            "title": "Errors",
107
                            "type": "array",
108
                            "items": {"$ref": "#/components/schemas/Error"},
109
                        }
110
                    },
111
                },
112
            }
113
        },
114
    }
115

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

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

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

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