forked from apiflask/apiflask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fields.py
81 lines (70 loc) · 2.26 KB
/
test_fields.py
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
import io
from werkzeug.datastructures import FileStorage
from .schemas import Files
from .schemas import FilesList
def test_file_field(app, client):
@app.post('/')
@app.input(Files, location='files')
def index(files_data):
data = {}
if 'image' in files_data and isinstance(files_data['image'], FileStorage):
data['image'] = True
return data
rv = client.get('/openapi.json')
assert rv.status_code == 200
assert 'image' in rv.json['components']['schemas']['Files']['properties']
assert rv.json['components']['schemas']['Files']['properties']['image']['type'] == 'string'
assert rv.json['components']['schemas']['Files']['properties']['image']['format'] == 'binary'
rv = client.post(
'/',
data={
'image': (io.BytesIO(b'test'), 'test.jpg'),
},
content_type='multipart/form-data'
)
assert rv.status_code == 200
assert rv.json == {'image': True}
rv = client.post(
'/',
data={
'image': 'test',
},
content_type='multipart/form-data'
)
assert rv.status_code == 422
assert rv.json['detail']['files']['image'] == ['Not a valid file.']
def test_multiple_file_field(app, client):
@app.post('/')
@app.input(FilesList, location='files')
def index(files_list_data):
data = {'images': True}
for f in files_list_data['images']:
if not isinstance(f, FileStorage):
data['images'] = False
return data
rv = client.post(
'/',
data={
'images': [
(io.BytesIO(b'test0'), 'test0.jpg'),
(io.BytesIO(b'test1'), 'test1.jpg'),
(io.BytesIO(b'test2'), 'test2.jpg'),
]
},
content_type='multipart/form-data'
)
assert rv.status_code == 200
assert rv.json == {'images': True}
rv = client.post(
'/',
data={
'images': [
(io.BytesIO(b'test0'), 'test0.jpg'),
(io.BytesIO(b'test1'), 'test1.jpg'),
'test2',
]
},
content_type='multipart/form-data'
)
assert rv.status_code == 422
assert rv.json['detail']['files']['images']['2'] == ['Not a valid file.']