]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Fix Form and File params must always be embeded
authorSebastián Ramírez <tiangolo@gmail.com>
Sat, 22 Dec 2018 04:23:35 +0000 (08:23 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Sat, 22 Dec 2018 04:24:48 +0000 (08:24 +0400)
and add tests for forms and files

fastapi/params.py
tests/test_tutorial/test_request_files/__init__.py [new file with mode: 0644]
tests/test_tutorial/test_request_files/test_tutorial001.py [new file with mode: 0644]
tests/test_tutorial/test_request_forms/test_tutorial001.py

index 499d17184dabe6e47f300dc151f679ab88a10be2..3d9afec786c57b92990f11fffd890e0374565c0d 100644 (file)
@@ -241,7 +241,6 @@ class Form(Body):
         self,
         default: Any,
         *,
-        sub_key: bool = False,
         media_type: str = "application/x-www-form-urlencoded",
         alias: str = None,
         title: str = None,
@@ -257,7 +256,7 @@ class Form(Body):
     ):
         super().__init__(
             default,
-            embed=sub_key,
+            embed=True,
             media_type=media_type,
             alias=alias,
             title=title,
@@ -278,7 +277,6 @@ class File(Form):
         self,
         default: Any,
         *,
-        sub_key: bool = False,
         media_type: str = "multipart/form-data",
         alias: str = None,
         title: str = None,
@@ -294,7 +292,6 @@ class File(Form):
     ):
         super().__init__(
             default,
-            embed=sub_key,
             media_type=media_type,
             alias=alias,
             title=title,
diff --git a/tests/test_tutorial/test_request_files/__init__.py b/tests/test_tutorial/test_request_files/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_tutorial/test_request_files/test_tutorial001.py b/tests/test_tutorial/test_request_files/test_tutorial001.py
new file mode 100644 (file)
index 0000000..1a59876
--- /dev/null
@@ -0,0 +1,120 @@
+import os
+
+from starlette.testclient import TestClient
+
+from request_files.tutorial001 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "Fast API", "version": "0.1.0"},
+    "paths": {
+        "/files/": {
+            "post": {
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+                "summary": "Create File Post",
+                "operationId": "create_file_files__post",
+                "requestBody": {
+                    "content": {
+                        "multipart/form-data": {
+                            "schema": {"$ref": "#/components/schemas/Body_create_file"}
+                        }
+                    },
+                    "required": True,
+                },
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "Body_create_file": {
+                "title": "Body_create_file",
+                "required": ["file"],
+                "type": "object",
+                "properties": {
+                    "file": {"title": "File", "type": "string", "format": "binary"}
+                },
+            },
+            "ValidationError": {
+                "title": "ValidationError",
+                "required": ["loc", "msg", "type"],
+                "type": "object",
+                "properties": {
+                    "loc": {
+                        "title": "Location",
+                        "type": "array",
+                        "items": {"type": "string"},
+                    },
+                    "msg": {"title": "Message", "type": "string"},
+                    "type": {"title": "Error Type", "type": "string"},
+                },
+            },
+            "HTTPValidationError": {
+                "title": "HTTPValidationError",
+                "type": "object",
+                "properties": {
+                    "detail": {
+                        "title": "Detail",
+                        "type": "array",
+                        "items": {"$ref": "#/components/schemas/ValidationError"},
+                    }
+                },
+            },
+        }
+    },
+}
+
+
+def test_openapi_scheme():
+    response = client.get("/openapi.json")
+    assert response.status_code == 200
+    assert response.json() == openapi_schema
+
+
+file_required = {
+    "detail": [
+        {
+            "loc": ["body", "file"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+
+
+def test_post_form_no_body():
+    response = client.post("/files/")
+    assert response.status_code == 422
+    assert response.json() == file_required
+
+
+def test_post_body_json():
+    response = client.post("/files/", json={"file": "Foo"})
+    assert response.status_code == 422
+    assert response.json() == file_required
+
+
+def test_multipart_request_files(tmpdir):
+    path = os.path.join(tmpdir, "test.txt")
+    with open(path, "wb") as file:
+        file.write(b"<file content>")
+
+    client = TestClient(app)
+    response = client.post("/files/", files={"file": open(path, "rb")})
+    assert response.json() == {"file_size": 14}
index 610523e92c188bea80e4232077367b6d0f172f2c..4e6ae457bf6d07459d7ab72f8362cf1bc44006cd 100644 (file)
@@ -147,13 +147,11 @@ username_and_password_required = {
 )
 def test_post_body_form(path, body, expected_status, expected_response):
     response = client.post(path, data=body)
-    print(response.text)
     assert response.status_code == expected_status
     assert response.json() == expected_response
 
 
 def test_post_body_json():
     response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    print(response.text)
     assert response.status_code == 422
     assert response.json() == username_and_password_required