1
1
"""Tests in this file have been adapted from Starlette."""
2
2
3
3
import os
4
+ from os .path import abspath , dirname , join
4
5
from typing import TYPE_CHECKING , Any , Callable , Dict , List , Mapping , Tuple , Union
5
6
6
7
import pytest
@@ -55,7 +56,7 @@ def test_client_factory() -> Callable[[Any], TestClient]:
55
56
FORCE_MULTIPART = ForceMultipartDict ()
56
57
57
58
58
- async def app (scope : "Scope" , receive : "Receive" , send : "Send" ) -> None :
59
+ async def standard_app (scope : "Scope" , receive : "Receive" , send : "Send" ) -> None :
59
60
request = Request (scope , receive )
60
61
data = await request .form ()
61
62
output = {}
@@ -130,7 +131,7 @@ async def app_read_body(scope: "Scope", receive: "Receive", send: "Send") -> Non
130
131
131
132
132
133
def test_multipart_request_data (test_client_factory : Callable [[Any ], TestClient ]) -> None :
133
- client = test_client_factory (app )
134
+ client = test_client_factory (standard_app )
134
135
response = client .post ("/" , data = {"some" : "data" }, files = FORCE_MULTIPART )
135
136
assert response .json () == {"some" : "data" }
136
137
@@ -140,7 +141,7 @@ def test_multipart_request_files(tmpdir: Any, test_client_factory: Callable[[Any
140
141
with open (path , "wb" ) as file :
141
142
file .write (b"<file content>" )
142
143
143
- client = test_client_factory (app )
144
+ client = test_client_factory (standard_app )
144
145
with open (path , "rb" ) as f :
145
146
response = client .post ("/" , files = {"test" : f })
146
147
assert response .json () == {
@@ -159,7 +160,7 @@ def test_multipart_request_files_with_content_type(
159
160
with open (path , "wb" ) as file :
160
161
file .write (b"<file content>" )
161
162
162
- client = test_client_factory (app )
163
+ client = test_client_factory (standard_app )
163
164
with open (path , "rb" ) as f :
164
165
response = client .post ("/" , files = {"test" : ("test.txt" , f , "text/plain" )})
165
166
assert response .json () == {
@@ -180,7 +181,7 @@ def test_multipart_request_multiple_files(tmpdir: Any, test_client_factory: Call
180
181
with open (path2 , "wb" ) as file :
181
182
file .write (b"<file2 content>" )
182
183
183
- client = test_client_factory (app )
184
+ client = test_client_factory (standard_app )
184
185
with open (path1 , "rb" ) as f1 , open (path2 , "rb" ) as f2 :
185
186
response = client .post ("/" , files = {"test1" : f1 , "test2" : ("test2.txt" , f2 , "text/plain" )})
186
187
assert response .json () == {
@@ -266,7 +267,7 @@ def test_multi_items(tmpdir: Any, test_client_factory: Callable[[Any], TestClien
266
267
267
268
268
269
def test_multipart_request_mixed_files_and_data (test_client_factory : Callable [[Any ], TestClient ]) -> None :
269
- client = test_client_factory (app )
270
+ client = test_client_factory (standard_app )
270
271
response = client .post (
271
272
"/" ,
272
273
data = (
@@ -299,7 +300,7 @@ def test_multipart_request_mixed_files_and_data(test_client_factory: Callable[[A
299
300
300
301
301
302
def test_multipart_request_with_charset_for_filename (test_client_factory : Callable [[Any ], TestClient ]) -> None :
302
- client = test_client_factory (app )
303
+ client = test_client_factory (standard_app )
303
304
response = client .post (
304
305
"/" ,
305
306
data = (
@@ -322,7 +323,7 @@ def test_multipart_request_with_charset_for_filename(test_client_factory: Callab
322
323
323
324
324
325
def test_multipart_request_without_charset_for_filename (test_client_factory : Callable [[Any ], TestClient ]) -> None :
325
- client = test_client_factory (app )
326
+ client = test_client_factory (standard_app )
326
327
response = client .post (
327
328
"/" ,
328
329
data = (
@@ -345,7 +346,7 @@ def test_multipart_request_without_charset_for_filename(test_client_factory: Cal
345
346
346
347
347
348
def test_multipart_request_with_encoded_value (test_client_factory : Callable [[Any ], TestClient ]) -> None :
348
- client = test_client_factory (app )
349
+ client = test_client_factory (standard_app )
349
350
response = client .post (
350
351
"/" ,
351
352
data = (
@@ -361,7 +362,7 @@ def test_multipart_request_with_encoded_value(test_client_factory: Callable[[Any
361
362
362
363
363
364
def test_no_request_data (test_client_factory : Callable [[Any ], TestClient ]) -> None :
364
- client = test_client_factory (app )
365
+ client = test_client_factory (standard_app )
365
366
response = client .post ("/" )
366
367
assert response .json () == {}
367
368
@@ -385,7 +386,7 @@ def test_postman_multipart_form_data(test_client_factory: Callable[[Any], TestCl
385
386
"content-length" : "2455" ,
386
387
}
387
388
388
- client = test_client_factory (app )
389
+ client = test_client_factory (standard_app )
389
390
response = client .post ("/" , data = postman_body , headers = postman_headers )
390
391
assert response .json () == {
391
392
"attributes" : {
@@ -399,3 +400,33 @@ def test_postman_multipart_form_data(test_client_factory: Callable[[Any], TestCl
399
400
"content_type" : "application/octet-stream" ,
400
401
},
401
402
}
403
+
404
+
405
+ def test_image_upload (test_client_factory : Callable [[Any ], TestClient ]) -> None :
406
+ async def test_app (scope : "Scope" , receive : "Receive" , send : "Send" ) -> None :
407
+ request = Request (scope , receive )
408
+ data = await request .form ()
409
+ output : Dict [str , list ] = {} # type: ignore
410
+ for key , value in data .multi_items ():
411
+ if key not in output :
412
+ output [key ] = []
413
+ if isinstance (value , UploadFile ):
414
+ content = await value .read ()
415
+ output [key ].append (
416
+ {
417
+ "filename" : value .filename ,
418
+ "content" : content .decode ("latin-1" ),
419
+ "content_type" : value .content_type ,
420
+ }
421
+ )
422
+ else :
423
+ output [key ].append (value )
424
+ await request .close ()
425
+ response = JSONResponse (output )
426
+ await response (scope , receive , send )
427
+
428
+ client = test_client_factory (test_app )
429
+
430
+ with open (join (dirname (abspath (__file__ )), "flower.jpeg" ), "rb" ) as f :
431
+ data = f .read ()
432
+ client .post ("http://localhost:8000/" , files = {"flower" : data })
0 commit comments