]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Re-implement check for body as a workaround
authorSebastián Ramírez <tiangolo@gmail.com>
Sat, 22 Dec 2018 03:40:56 +0000 (07:40 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Sat, 22 Dec 2018 03:40:56 +0000 (07:40 +0400)
while encode/starlette#287 is merged

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

index fbd6a8d9cab2c157c91f008f0954830d041abd41..964d61ee704869ebfa13a4a2fe1599f522d64461 100644 (file)
@@ -51,8 +51,7 @@ def get_app(
         try:
             body = None
             if body_field:
-                body_bytes = await request.body()
-                if body_bytes and is_body_form:
+                if is_body_form:
                     raw_body = await request.form()
                     body = {}
                     for field, value in raw_body.items():
@@ -60,8 +59,12 @@ def get_app(
                             body[field] = await value.read()
                         else:
                             body[field] = value
-                elif body_bytes:
-                    body = await request.json()
+                    if not body:
+                        body = None
+                else:
+                    body_bytes = await request.body()
+                    if body_bytes:
+                        body = await request.json()
         except Exception as e:
             logging.error("Error getting request body", e)
             raise HTTPException(
diff --git a/tests/test_tutorial/test_request_forms/__init__.py b/tests/test_tutorial/test_request_forms/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_tutorial/test_request_forms/test_tutorial001.py b/tests/test_tutorial/test_request_forms/test_tutorial001.py
new file mode 100644 (file)
index 0000000..610523e
--- /dev/null
@@ -0,0 +1,159 @@
+import pytest
+from starlette.testclient import TestClient
+
+from request_forms.tutorial001 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "Fast API", "version": "0.1.0"},
+    "paths": {
+        "/login/": {
+            "post": {
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+                "summary": "Login Post",
+                "operationId": "login_login__post",
+                "requestBody": {
+                    "content": {
+                        "application/x-www-form-urlencoded": {
+                            "schema": {"$ref": "#/components/schemas/Body_login"}
+                        }
+                    },
+                    "required": True,
+                },
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "Body_login": {
+                "title": "Body_login",
+                "required": ["username", "password"],
+                "type": "object",
+                "properties": {
+                    "username": {"title": "Username", "type": "string"},
+                    "password": {"title": "Password", "type": "string"},
+                },
+            },
+            "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
+
+
+item_id_not_int = {
+    "detail": [
+        {
+            "loc": ["path", "item_id"],
+            "msg": "value is not a valid integer",
+            "type": "type_error.integer",
+        }
+    ]
+}
+
+password_required = {
+    "detail": [
+        {
+            "loc": ["body", "password"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+username_required = {
+    "detail": [
+        {
+            "loc": ["body", "username"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+username_and_password_required = {
+    "detail": [
+        {
+            "loc": ["body", "username"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        },
+        {
+            "loc": ["body", "password"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        },
+    ]
+}
+
+
+@pytest.mark.parametrize(
+    "path,body,expected_status,expected_response",
+    [
+        (
+            "/login/",
+            {"username": "Foo", "password": "secret"},
+            200,
+            {"username": "Foo"},
+        ),
+        ("/login/", {"username": "Foo"}, 422, password_required),
+        ("/login/", {"password": "secret"}, 422, username_required),
+        ("/login/", None, 422, 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