Coverage for tests / test_multipart_installation.py: 100%

101 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import warnings 1abcd

2 

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) 

9 

10 

11def test_incorrect_multipart_installed_form(monkeypatch): 1abcd

12 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1efgh

13 with warnings.catch_warnings(record=True): 1efgh

14 warnings.simplefilter("always") 1efgh

15 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1efgh

16 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1efgh

17 app = FastAPI() 1efgh

18 

19 @app.post("/") 1efgh

20 async def root(username: str = Form()): 1efgh

21 return username # pragma: nocover 

22 

23 

24def test_incorrect_multipart_installed_file_upload(monkeypatch): 1abcd

25 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1ijkl

26 with warnings.catch_warnings(record=True): 1ijkl

27 warnings.simplefilter("always") 1ijkl

28 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1ijkl

29 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1ijkl

30 app = FastAPI() 1ijkl

31 

32 @app.post("/") 1ijkl

33 async def root(f: UploadFile = File()): 1ijkl

34 return f # pragma: nocover 

35 

36 

37def test_incorrect_multipart_installed_file_bytes(monkeypatch): 1abcd

38 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1mnop

39 with warnings.catch_warnings(record=True): 1mnop

40 warnings.simplefilter("always") 1mnop

41 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1mnop

42 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1mnop

43 app = FastAPI() 1mnop

44 

45 @app.post("/") 1mnop

46 async def root(f: bytes = File()): 1mnop

47 return f # pragma: nocover 

48 

49 

50def test_incorrect_multipart_installed_multi_form(monkeypatch): 1abcd

51 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1qrst

52 with warnings.catch_warnings(record=True): 1qrst

53 warnings.simplefilter("always") 1qrst

54 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1qrst

55 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1qrst

56 app = FastAPI() 1qrst

57 

58 @app.post("/") 1qrst

59 async def root(username: str = Form(), password: str = Form()): 1qrst

60 return username # pragma: nocover 

61 

62 

63def test_incorrect_multipart_installed_form_file(monkeypatch): 1abcd

64 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1uvwx

65 with warnings.catch_warnings(record=True): 1uvwx

66 warnings.simplefilter("always") 1uvwx

67 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1uvwx

68 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1uvwx

69 app = FastAPI() 1uvwx

70 

71 @app.post("/") 1uvwx

72 async def root(username: str = Form(), f: UploadFile = File()): 1uvwx

73 return username # pragma: nocover 

74 

75 

76def test_no_multipart_installed(monkeypatch): 1abcd

77 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1yzAB

78 with warnings.catch_warnings(record=True): 1yzAB

79 warnings.simplefilter("always") 1yzAB

80 monkeypatch.delattr("multipart.__version__", raising=False) 1yzAB

81 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1yzAB

82 app = FastAPI() 1yzAB

83 

84 @app.post("/") 1yzAB

85 async def root(username: str = Form()): 1yzAB

86 return username # pragma: nocover 

87 

88 

89def test_no_multipart_installed_file(monkeypatch): 1abcd

90 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1CDEF

91 with warnings.catch_warnings(record=True): 1CDEF

92 warnings.simplefilter("always") 1CDEF

93 monkeypatch.delattr("multipart.__version__", raising=False) 1CDEF

94 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1CDEF

95 app = FastAPI() 1CDEF

96 

97 @app.post("/") 1CDEF

98 async def root(f: UploadFile = File()): 1CDEF

99 return f # pragma: nocover 

100 

101 

102def test_no_multipart_installed_file_bytes(monkeypatch): 1abcd

103 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1GHIJ

104 with warnings.catch_warnings(record=True): 1GHIJ

105 warnings.simplefilter("always") 1GHIJ

106 monkeypatch.delattr("multipart.__version__", raising=False) 1GHIJ

107 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1GHIJ

108 app = FastAPI() 1GHIJ

109 

110 @app.post("/") 1GHIJ

111 async def root(f: bytes = File()): 1GHIJ

112 return f # pragma: nocover 

113 

114 

115def test_no_multipart_installed_multi_form(monkeypatch): 1abcd

116 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1KLMN

117 with warnings.catch_warnings(record=True): 1KLMN

118 warnings.simplefilter("always") 1KLMN

119 monkeypatch.delattr("multipart.__version__", raising=False) 1KLMN

120 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1KLMN

121 app = FastAPI() 1KLMN

122 

123 @app.post("/") 1KLMN

124 async def root(username: str = Form(), password: str = Form()): 1KLMN

125 return username # pragma: nocover 

126 

127 

128def test_no_multipart_installed_form_file(monkeypatch): 1abcd

129 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1OPQR

130 with warnings.catch_warnings(record=True): 1OPQR

131 warnings.simplefilter("always") 1OPQR

132 monkeypatch.delattr("multipart.__version__", raising=False) 1OPQR

133 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1OPQR

134 app = FastAPI() 1OPQR

135 

136 @app.post("/") 1OPQR

137 async def root(username: str = Form(), f: UploadFile = File()): 1OPQR

138 return username # pragma: nocover 

139 

140 

141def test_old_multipart_installed(monkeypatch): 1abcd

142 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1STUV

143 with warnings.catch_warnings(record=True): 1STUV

144 warnings.simplefilter("always") 1STUV

145 app = FastAPI() 1STUV

146 

147 @app.post("/") 1STUV

148 async def root(username: str = Form()): 1STUV

149 return username # pragma: nocover