Browse Source
* Check if Form exists and multipart is in virtual environment * Remove unused import * Move BodyFieldInfo check to separate helper function * Fix type UploadFile to File for BodyFieldInfo check * Working solution. Kind of nasty though. * Use better method of determing if correct package imported * Use better method of determing if correct package imported * Add raising exceptions, update error messages * Check if Form exists and multipart is in virtual environment * Move BodyFieldInfo check to separate helper function * Fix type UploadFile to File for BodyFieldInfo check * Use better method of determing if correct package imported * Add raising exceptions, update error messages * Removed unused import, added comments Co-authored-by: Christopher Nguyen <[email protected]> * Updated what kind of exception will be thrown * Add type annotations Adds annotations to is_form_data * Fix import order * Add basic tests * Fixed Travis tests * Replace logging with fastapi logger * Change AttributeError to ImportError to fix exception handling * Fixing tests * Catch ModuleNotFoundError first Fix code coverage * Update fastapi/dependencies/utils.py Remove error spaces when printing Co-authored-by: Marcelo Trylesinski <[email protected]> * Update fastapi/dependencies/utils.py Co-authored-by: Marcelo Trylesinski <[email protected]> * Removed spaces in error printing * ♻️ Refactor form data detection * ✅ Update/increase tests for incorrect multipart install * 🔥 Remove deprecated Travis (moved to GitHub Actions) Co-authored-by: yk396 <[email protected]> Co-authored-by: Christopher Nguyen <[email protected]> Co-authored-by: Kai Chen <[email protected]> Co-authored-by: Chris N <[email protected]> Co-authored-by: Marcelo Trylesinski <[email protected]>pull/1853/head
committed by
GitHub
2 changed files with 150 additions and 4 deletions
@ -0,0 +1,106 @@ |
|||
import pytest |
|||
from fastapi import FastAPI, File, Form, UploadFile |
|||
from fastapi.dependencies.utils import ( |
|||
multipart_incorrect_install_error, |
|||
multipart_not_installed_error, |
|||
) |
|||
|
|||
|
|||
def test_incorrect_multipart_installed_form(monkeypatch): |
|||
monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(username: str = Form(...)): |
|||
return username # pragma: nocover |
|||
|
|||
|
|||
def test_incorrect_multipart_installed_file_upload(monkeypatch): |
|||
monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(f: UploadFile = File(...)): |
|||
return f # pragma: nocover |
|||
|
|||
|
|||
def test_incorrect_multipart_installed_file_bytes(monkeypatch): |
|||
monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(f: bytes = File(...)): |
|||
return f # pragma: nocover |
|||
|
|||
|
|||
def test_incorrect_multipart_installed_multi_form(monkeypatch): |
|||
monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(username: str = Form(...), pasword: str = Form(...)): |
|||
return username # pragma: nocover |
|||
|
|||
|
|||
def test_incorrect_multipart_installed_form_file(monkeypatch): |
|||
monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(username: str = Form(...), f: UploadFile = File(...)): |
|||
return username # pragma: nocover |
|||
|
|||
|
|||
def test_no_multipart_installed(monkeypatch): |
|||
monkeypatch.delattr("multipart.__version__", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_not_installed_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(username: str = Form(...)): |
|||
return username # pragma: nocover |
|||
|
|||
|
|||
def test_no_multipart_installed_file(monkeypatch): |
|||
monkeypatch.delattr("multipart.__version__", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_not_installed_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(f: UploadFile = File(...)): |
|||
return f # pragma: nocover |
|||
|
|||
|
|||
def test_no_multipart_installed_file_bytes(monkeypatch): |
|||
monkeypatch.delattr("multipart.__version__", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_not_installed_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(f: bytes = File(...)): |
|||
return f # pragma: nocover |
|||
|
|||
|
|||
def test_no_multipart_installed_multi_form(monkeypatch): |
|||
monkeypatch.delattr("multipart.__version__", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_not_installed_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(username: str = Form(...), password: str = Form(...)): |
|||
return username # pragma: nocover |
|||
|
|||
|
|||
def test_no_multipart_installed_form_file(monkeypatch): |
|||
monkeypatch.delattr("multipart.__version__", raising=False) |
|||
with pytest.raises(RuntimeError, match=multipart_not_installed_error): |
|||
app = FastAPI() |
|||
|
|||
@app.post("/") |
|||
async def root(username: str = Form(...), f: UploadFile = File(...)): |
|||
return username # pragma: nocover |
Loading…
Reference in new issue