From 37e4121874c6f669f212c6bb2445f12a128248c3 Mon Sep 17 00:00:00 2001 From: Casper van der Wel Date: Tue, 10 Oct 2023 16:28:30 +0200 Subject: [PATCH] Disable multipart encoding --- CHANGES.md | 4 ++-- clean_python/api_client/sync_api_provider.py | 4 +++- .../fastapi_example/presentation.py | 20 +++++++++++++++---- tests/api_client/test_sync_api_provider.py | 2 ++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1671191..34c346c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,7 @@ 0.6.9 (unreleased) ------------------ -- Nothing changed yet. +- Disable multpart encoding in `SyncApiProvider`. 0.6.8 (2023-10-10) @@ -16,7 +16,7 @@ 0.6.7 (2023-10-09) ------------------ -- Adapt call signature of the `fetch_token` callable in `ApiProvicer`. +- Adapt call signature of the `fetch_token` callable in `ApiProvider`. - Add `clean_python.oauth.client_credentials`. diff --git a/clean_python/api_client/sync_api_provider.py b/clean_python/api_client/sync_api_provider.py index 88db4f9..7c754e4 100644 --- a/clean_python/api_client/sync_api_provider.py +++ b/clean_python/api_client/sync_api_provider.py @@ -74,7 +74,9 @@ def _request( elif fields is not None: request_kwargs["fields"] = fields headers.update(self._fetch_token()) - return self._pool.request(headers=headers, **request_kwargs) + return self._pool.request( + headers=headers, encode_multipart=False, **request_kwargs + ) def request( self, diff --git a/integration_tests/fastapi_example/presentation.py b/integration_tests/fastapi_example/presentation.py index ad466a1..7e96034 100644 --- a/integration_tests/fastapi_example/presentation.py +++ b/integration_tests/fastapi_example/presentation.py @@ -6,6 +6,7 @@ from fastapi import Depends from fastapi import Form +from fastapi import Request from fastapi import Response from fastapi import UploadFile from fastapi.responses import JSONResponse @@ -86,19 +87,30 @@ async def urlencode(self, name: str): @post("/token") def token( self, + request: Request, grant_type: str = Form(), scope: str = Form(), credentials: HTTPBasicCredentials = Depends(basic), ): """For testing client credentials grant""" + if request.headers["Content-Type"] != "application/x-www-form-urlencoded": + return Response(status_code=HTTPStatus.METHOD_NOT_ALLOWED) if grant_type != "client_credentials": - return JSONResponse({"error": "invalid_grant"}) + return JSONResponse( + {"error": "invalid_grant"}, status_code=HTTPStatus.BAD_REQUEST + ) if credentials.username != "testclient": - return JSONResponse({"error": "invalid_client"}) + return JSONResponse( + {"error": "invalid_client"}, status_code=HTTPStatus.BAD_REQUEST + ) if credentials.password != "supersecret": - return JSONResponse({"error": "invalid_client"}) + return JSONResponse( + {"error": "invalid_client"}, status_code=HTTPStatus.BAD_REQUEST + ) if scope != "all": - return JSONResponse({"error": "invalid_grant"}) + return JSONResponse( + {"error": "invalid_grant"}, status_code=HTTPStatus.BAD_REQUEST + ) claims = {"user": "foo", "exp": int(time.time()) + 3600} payload = base64.b64encode(json.dumps(claims).encode()).decode() return { diff --git a/tests/api_client/test_sync_api_provider.py b/tests/api_client/test_sync_api_provider.py index d3669f3..a19125e 100644 --- a/tests/api_client/test_sync_api_provider.py +++ b/tests/api_client/test_sync_api_provider.py @@ -49,6 +49,7 @@ def test_get(api_provider: SyncApiProvider, response): url="http://testserver/foo", headers={"Authorization": "Bearer tenant-2"}, timeout=5.0, + encode_multipart=False, ) assert actual == {"foo": 2} @@ -69,6 +70,7 @@ def test_post_json(api_provider: SyncApiProvider, response): "Authorization": "Bearer tenant-2", }, timeout=5.0, + encode_multipart=False, ) assert actual == {"foo": 2}