File: test_request.py

package info (click to toggle)
flask-openapi3 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,224 kB
  • sloc: python: 4,802; makefile: 14; javascript: 5
file content (180 lines) | stat: -rw-r--r-- 3,985 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2022/9/2 15:35
from enum import Enum
from functools import wraps

import pytest
from pydantic import BaseModel, Field

from flask_openapi3 import FileStorage, OpenAPI, RawModel

app = OpenAPI(__name__)
app.config["TESTING"] = True


@pytest.fixture
def client():
    client = app.test_client()

    return client


class TypeEnum(str, Enum):
    A = "A"
    B = "B"


class BookForm(BaseModel):
    file: FileStorage
    files: list[FileStorage]
    string: str
    string_list: list[str]


class BookQuery(BaseModel):
    age: list[int]
    book_type: TypeEnum | None = None


class BookQueryFilter(BaseModel):
    age: list[int]
    fields: list[str] | None = None


class BookBody(BaseModel):
    age: int


class BookCookie(BaseModel):
    token: str | None = None
    token_type: TypeEnum | None = None


class BookHeader(BaseModel):
    Hello1: str = Field("what's up", max_length=12, description="sds")
    # required
    hello2: str = Field(..., max_length=12, description="sds")
    api_key: str = Field(..., description="API Key")
    api_type: TypeEnum | None = None
    x_hello: str = Field(..., max_length=12, description="Header with alias to support dash", alias="x-hello")


def decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    return wrapper


@app.get("/query")
@decorator
def api_query(query: BookQuery):
    print(query)
    return {"code": 0, "message": "ok"}


@app.get("/filter-query")
@decorator
def api_filter_query(query: BookQueryFilter):
    print(query)
    return {"fields": query.fields, "message": "ok"}


@app.post("/form")
def api_form(form: BookForm):
    print(form)
    return {"code": 0, "message": "ok"}


@app.post("/body")
def api_error_json(body: BookBody):
    print(body)  # pragma: no cover


@app.get("/header")
def get_book(header: BookHeader):
    return header.model_dump(by_alias=True)


@app.post("/cookie")
def api_cookie(cookie: BookCookie):
    print(cookie)
    return {"code": 0, "message": "ok"}


class BookRaw(RawModel):
    mimetypes = ["text/csv", "application/json"]


@app.post("/raw")
def api_raw(raw: BookRaw):
    # raw equals to flask.request
    assert raw.data == b"raw"
    assert raw.mimetype == "text/plain"
    return "ok"


def test_query(client):
    r = client.get("/query?age=1")
    print(r.json)
    assert r.status_code == 200


def test_query_list(client):
    r = client.get("/filter-query?age=1&fields=name&fields=age")
    print(r.json)
    assert r.status_code == 200
    assert r.json["fields"] == ["name", "age"]


def test_query_list_no_fields(client):
    r = client.get("/filter-query?age=1")
    print(r.json)
    assert r.status_code == 200
    assert r.json["fields"] is None


def test_query_list_single_field(client):
    r = client.get("/filter-query?age=1&fields=age")
    print(r.json)
    assert r.status_code == 200
    assert r.json["fields"] == ["age"]


def test_form(client):
    from io import BytesIO

    data = {
        "file": (BytesIO(b"post-data"), "filename"),
        "files": [(BytesIO(b"post-data"), "filename"), (BytesIO(b"post-data"), "filename")],
        "string": "a",
        "string_list": ["a", "b", "c"],
    }
    r = client.post("/form", data=data, content_type="multipart/form-data")
    assert r.status_code == 200


def test_error_json(client):
    r = client.post("/body", json="{age: 1}")
    assert r.status_code == 422


def test_cookie(client):
    r = client.post("/cookie")
    print(r.json)
    assert r.status_code == 200


def test_header(client):
    headers = {"Hello1": "111", "hello2": "222", "api_key": "333", "api_type": "A", "x-hello": "444"}
    resp = client.get("/header", headers=headers)
    print(resp.json)
    assert resp.status_code == 200
    assert resp.json == headers


def test_raw(client):
    resp = client.post("/raw", data="raw", headers={"Content-Type": "text/plain"})
    assert resp.status_code == 200