1
- import io
2
- from tempfile import SpooledTemporaryFile
3
- from typing import BinaryIO
4
-
1
+ import anyio
5
2
import pytest
6
3
7
4
from starlette .datastructures import (
@@ -308,29 +305,41 @@ def test_queryparams() -> None:
308
305
@pytest .mark .anyio
309
306
async def test_upload_file_file_input () -> None :
310
307
"""Test passing file/stream into the UploadFile constructor"""
311
- stream = io .BytesIO (b"data" )
312
- file = UploadFile (filename = "file" , file = stream , size = 4 )
313
- assert await file .read () == b"data"
314
- assert file .size == 4
315
- await file .write (b" and more data!" )
316
- assert await file .read () == b""
317
- assert file .size == 19
318
- await file .seek (0 )
319
- assert await file .read () == b"data and more data!"
308
+ async with anyio .SpooledTemporaryFile (max_size = 1024 * 1024 ) as stream :
309
+ await stream .write (b"data" )
310
+ await stream .seek (0 )
311
+
312
+ file = UploadFile (filename = "file" , file = stream , size = 4 )
313
+ try :
314
+ assert await file .read () == b"data"
315
+ assert file .size == 4
316
+ await file .write (b" and more data!" )
317
+ assert await file .read () == b""
318
+ assert file .size == 19
319
+ await file .seek (0 )
320
+ assert await file .read () == b"data and more data!"
321
+ finally :
322
+ await file .close ()
320
323
321
324
322
325
@pytest .mark .anyio
323
326
async def test_upload_file_without_size () -> None :
324
327
"""Test passing file/stream into the UploadFile constructor without size"""
325
- stream = io .BytesIO (b"data" )
326
- file = UploadFile (filename = "file" , file = stream )
327
- assert await file .read () == b"data"
328
- assert file .size is None
329
- await file .write (b" and more data!" )
330
- assert await file .read () == b""
331
- assert file .size is None
332
- await file .seek (0 )
333
- assert await file .read () == b"data and more data!"
328
+ async with anyio .SpooledTemporaryFile (max_size = 1024 * 1024 ) as stream :
329
+ await stream .write (b"data" )
330
+ await stream .seek (0 )
331
+
332
+ file = UploadFile (filename = "file" , file = stream )
333
+ try :
334
+ assert await file .read () == b"data"
335
+ assert file .size is None
336
+ await file .write (b" and more data!" )
337
+ assert await file .read () == b""
338
+ assert file .size is None
339
+ await file .seek (0 )
340
+ assert await file .read () == b"data and more data!"
341
+ finally :
342
+ await file .close ()
334
343
335
344
336
345
@pytest .mark .anyio
@@ -339,61 +348,81 @@ async def test_uploadfile_rolling(max_size: int) -> None:
339
348
"""Test that we can r/w to a SpooledTemporaryFile
340
349
managed by UploadFile before and after it rolls to disk
341
350
"""
342
- stream : BinaryIO = SpooledTemporaryFile ( # type: ignore[assignment]
343
- max_size = max_size
344
- )
345
- file = UploadFile (filename = "file" , file = stream , size = 0 )
346
- assert await file .read () == b""
347
- assert file .size == 0
348
- await file .write (b"data" )
349
- assert await file .read () == b""
350
- assert file .size == 4
351
- await file .seek (0 )
352
- assert await file .read () == b"data"
353
- await file .write (b" more" )
354
- assert await file .read () == b""
355
- assert file .size == 9
356
- await file .seek (0 )
357
- assert await file .read () == b"data more"
358
- assert file .size == 9
359
- await file .close ()
360
-
361
-
362
- def test_formdata () -> None :
363
- stream = io .BytesIO (b"data" )
364
- upload = UploadFile (filename = "file" , file = stream , size = 4 )
365
- form = FormData ([("a" , "123" ), ("a" , "456" ), ("b" , upload )])
366
- assert "a" in form
367
- assert "A" not in form
368
- assert "c" not in form
369
- assert form ["a" ] == "456"
370
- assert form .get ("a" ) == "456"
371
- assert form .get ("nope" , default = None ) is None
372
- assert form .getlist ("a" ) == ["123" , "456" ]
373
- assert list (form .keys ()) == ["a" , "b" ]
374
- assert list (form .values ()) == ["456" , upload ]
375
- assert list (form .items ()) == [("a" , "456" ), ("b" , upload )]
376
- assert len (form ) == 2
377
- assert list (form ) == ["a" , "b" ]
378
- assert dict (form ) == {"a" : "456" , "b" : upload }
379
- assert repr (form ) == "FormData([('a', '123'), ('a', '456'), ('b', " + repr (upload ) + ")])"
380
- assert FormData (form ) == form
381
- assert FormData ({"a" : "123" , "b" : "789" }) == FormData ([("a" , "123" ), ("b" , "789" )])
382
- assert FormData ({"a" : "123" , "b" : "789" }) != {"a" : "123" , "b" : "789" }
351
+ async with anyio .SpooledTemporaryFile (max_size = max_size ) as stream :
352
+ file = UploadFile (filename = "file" , file = stream , size = 0 )
353
+ try :
354
+ assert await file .read () == b""
355
+ assert file .size == 0
356
+ await file .write (b"data" )
357
+ assert await file .read () == b""
358
+ assert file .size == 4
359
+ await file .seek (0 )
360
+ assert await file .read () == b"data"
361
+ await file .write (b" more" )
362
+ assert await file .read () == b""
363
+ assert file .size == 9
364
+ await file .seek (0 )
365
+ assert await file .read () == b"data more"
366
+ assert file .size == 9
367
+ finally :
368
+ await file .close ()
369
+
370
+
371
+ @pytest .mark .anyio
372
+ async def test_formdata () -> None :
373
+ async with anyio .SpooledTemporaryFile (max_size = 1024 ) as stream :
374
+ await stream .write (b"data" )
375
+ await stream .seek (0 )
376
+
377
+ upload = UploadFile (filename = "file" , file = stream , size = 4 )
378
+
379
+ form = FormData ([("a" , "123" ), ("a" , "456" ), ("b" , upload )])
380
+
381
+ assert "a" in form
382
+ assert "A" not in form
383
+ assert "c" not in form
384
+ assert form ["a" ] == "456"
385
+ assert form .get ("a" ) == "456"
386
+ assert form .get ("nope" , default = None ) is None
387
+ assert form .getlist ("a" ) == ["123" , "456" ]
388
+ assert list (form .keys ()) == ["a" , "b" ]
389
+ assert list (form .values ()) == ["456" , upload ]
390
+ assert list (form .items ()) == [("a" , "456" ), ("b" , upload )]
391
+ assert len (form ) == 2
392
+ assert list (form ) == ["a" , "b" ]
393
+ assert dict (form ) == {"a" : "456" , "b" : upload }
394
+ assert repr (form ) == "FormData([('a', '123'), ('a', '456'), ('b', " + repr (upload ) + ")])"
395
+ assert FormData (form ) == form
396
+ assert FormData ({"a" : "123" , "b" : "789" }) == FormData ([("a" , "123" ), ("b" , "789" )])
397
+ assert FormData ({"a" : "123" , "b" : "789" }) != {"a" : "123" , "b" : "789" }
383
398
384
399
385
400
@pytest .mark .anyio
386
401
async def test_upload_file_repr () -> None :
387
- stream = io .BytesIO (b"data" )
388
- file = UploadFile (filename = "file" , file = stream , size = 4 )
389
- assert repr (file ) == "UploadFile(filename='file', size=4, headers=Headers({}))"
402
+ """Test the string representation of UploadFile"""
403
+ async with anyio .SpooledTemporaryFile (max_size = 1024 * 1024 ) as stream :
404
+ await stream .write (b"data" )
405
+ await stream .seek (0 )
406
+
407
+ file = UploadFile (filename = "file" , file = stream , size = 4 )
408
+ try :
409
+ assert repr (file ) == "UploadFile(filename='file', size=4, headers=Headers({}))"
410
+ finally :
411
+ await file .close ()
390
412
391
413
392
414
@pytest .mark .anyio
393
415
async def test_upload_file_repr_headers () -> None :
394
- stream = io .BytesIO (b"data" )
395
- file = UploadFile (filename = "file" , file = stream , headers = Headers ({"foo" : "bar" }))
396
- assert repr (file ) == "UploadFile(filename='file', size=None, headers=Headers({'foo': 'bar'}))"
416
+ """Test the string representation of UploadFile with custom headers"""
417
+ async with anyio .SpooledTemporaryFile (max_size = 1024 * 1024 ) as stream :
418
+ await stream .write (b"data" )
419
+ await stream .seek (0 )
420
+
421
+ file = UploadFile (filename = "file" , file = stream , headers = Headers ({"foo" : "bar" }))
422
+ try :
423
+ assert repr (file ) == "UploadFile(filename='file', size=None, headers=Headers({'foo': 'bar'}))"
424
+ finally :
425
+ await file .close ()
397
426
398
427
399
428
def test_multidict () -> None :
0 commit comments