Coverage for tests / test_multipart_installation.py: 100%
101 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import warnings 1abcd
3import pytest 1abcd
4from fastapi import FastAPI, File, Form, UploadFile 1abcd
5from fastapi.dependencies.utils import ( 1abcd
6 multipart_incorrect_install_error,
7 multipart_not_installed_error,
8)
11def test_incorrect_multipart_installed_form(monkeypatch): 1abcd
12 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1efg
13 with warnings.catch_warnings(record=True): 1efg
14 warnings.simplefilter("always") 1efg
15 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1efg
16 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1efg
17 app = FastAPI() 1efg
19 @app.post("/") 1efg
20 async def root(username: str = Form()): 1efg
21 return username # pragma: nocover
24def test_incorrect_multipart_installed_file_upload(monkeypatch): 1abcd
25 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1hij
26 with warnings.catch_warnings(record=True): 1hij
27 warnings.simplefilter("always") 1hij
28 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1hij
29 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1hij
30 app = FastAPI() 1hij
32 @app.post("/") 1hij
33 async def root(f: UploadFile = File()): 1hij
34 return f # pragma: nocover
37def test_incorrect_multipart_installed_file_bytes(monkeypatch): 1abcd
38 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1klm
39 with warnings.catch_warnings(record=True): 1klm
40 warnings.simplefilter("always") 1klm
41 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1klm
42 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1klm
43 app = FastAPI() 1klm
45 @app.post("/") 1klm
46 async def root(f: bytes = File()): 1klm
47 return f # pragma: nocover
50def test_incorrect_multipart_installed_multi_form(monkeypatch): 1abcd
51 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1nop
52 with warnings.catch_warnings(record=True): 1nop
53 warnings.simplefilter("always") 1nop
54 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1nop
55 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1nop
56 app = FastAPI() 1nop
58 @app.post("/") 1nop
59 async def root(username: str = Form(), password: str = Form()): 1nop
60 return username # pragma: nocover
63def test_incorrect_multipart_installed_form_file(monkeypatch): 1abcd
64 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1qrs
65 with warnings.catch_warnings(record=True): 1qrs
66 warnings.simplefilter("always") 1qrs
67 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1qrs
68 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1qrs
69 app = FastAPI() 1qrs
71 @app.post("/") 1qrs
72 async def root(username: str = Form(), f: UploadFile = File()): 1qrs
73 return username # pragma: nocover
76def test_no_multipart_installed(monkeypatch): 1abcd
77 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1tuv
78 with warnings.catch_warnings(record=True): 1tuv
79 warnings.simplefilter("always") 1tuv
80 monkeypatch.delattr("multipart.__version__", raising=False) 1tuv
81 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1tuv
82 app = FastAPI() 1tuv
84 @app.post("/") 1tuv
85 async def root(username: str = Form()): 1tuv
86 return username # pragma: nocover
89def test_no_multipart_installed_file(monkeypatch): 1abcd
90 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1wxy
91 with warnings.catch_warnings(record=True): 1wxy
92 warnings.simplefilter("always") 1wxy
93 monkeypatch.delattr("multipart.__version__", raising=False) 1wxy
94 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1wxy
95 app = FastAPI() 1wxy
97 @app.post("/") 1wxy
98 async def root(f: UploadFile = File()): 1wxy
99 return f # pragma: nocover
102def test_no_multipart_installed_file_bytes(monkeypatch): 1abcd
103 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1zAB
104 with warnings.catch_warnings(record=True): 1zAB
105 warnings.simplefilter("always") 1zAB
106 monkeypatch.delattr("multipart.__version__", raising=False) 1zAB
107 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1zAB
108 app = FastAPI() 1zAB
110 @app.post("/") 1zAB
111 async def root(f: bytes = File()): 1zAB
112 return f # pragma: nocover
115def test_no_multipart_installed_multi_form(monkeypatch): 1abcd
116 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1CDE
117 with warnings.catch_warnings(record=True): 1CDE
118 warnings.simplefilter("always") 1CDE
119 monkeypatch.delattr("multipart.__version__", raising=False) 1CDE
120 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1CDE
121 app = FastAPI() 1CDE
123 @app.post("/") 1CDE
124 async def root(username: str = Form(), password: str = Form()): 1CDE
125 return username # pragma: nocover
128def test_no_multipart_installed_form_file(monkeypatch): 1abcd
129 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1FGH
130 with warnings.catch_warnings(record=True): 1FGH
131 warnings.simplefilter("always") 1FGH
132 monkeypatch.delattr("multipart.__version__", raising=False) 1FGH
133 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1FGH
134 app = FastAPI() 1FGH
136 @app.post("/") 1FGH
137 async def root(username: str = Form(), f: UploadFile = File()): 1FGH
138 return username # pragma: nocover
141def test_old_multipart_installed(monkeypatch): 1abcd
142 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1IJK
143 with warnings.catch_warnings(record=True): 1IJK
144 warnings.simplefilter("always") 1IJK
145 app = FastAPI() 1IJK
147 @app.post("/") 1IJK
148 async def root(username: str = Form()): 1IJK
149 return username # pragma: nocover