-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: regression test for stringified body params
- Loading branch information
1 parent
c1a58f4
commit edd933a
Showing
2 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
samples/openapi3/client/petstore/python-aiohttp/tests/test_rest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import AsyncMock, Mock, patch | ||
|
||
|
||
import petstore_api | ||
|
||
|
||
def get_field_from_formdata(formdata, name): | ||
return next(filter(lambda x: x[0]["name"] == name, formdata._fields))[-1] | ||
|
||
|
||
class TestMultipleResponseTypes(unittest.IsolatedAsyncioTestCase): | ||
def setUpFiles(self): | ||
self.test_file_dir = os.path.join(os.path.dirname(__file__), "..", "testfiles") | ||
self.test_file_dir = os.path.realpath(self.test_file_dir) | ||
self.test_file_path = os.path.join(self.test_file_dir, "foo.png") | ||
|
||
def setUp(self): | ||
self.api_client = petstore_api.ApiClient() | ||
self.fake_api = petstore_api.FakeApi(self.api_client) | ||
self.setUpFiles() | ||
|
||
async def test_multipart_requests(self): | ||
mock_resp = AsyncMock() | ||
mock_resp.return_value.read.return_value = b"some text" | ||
mock_resp.return_value.status = 200 | ||
mock_resp.return_value.headers = {} | ||
|
||
marker = petstore_api.TestObjectForMultipartRequestsRequestMarker( | ||
name="name", | ||
) | ||
|
||
with patch("aiohttp.ClientSession.request", mock_resp): | ||
returned = await self.fake_api.test_object_for_multipart_requests( | ||
marker=marker | ||
) | ||
assert returned is None | ||
|
||
async def test_multipart_requests_with_file_and_additional_properties(self): | ||
mock_resp = Mock() | ||
mock_resp.status = 200 | ||
mock_resp.read = AsyncMock( | ||
return_value=b'{"code": 200, "type": "success", "message": "OK"}' | ||
) | ||
mock_resp.headers = {"Content-Type": "application/json"} | ||
|
||
mock_request = AsyncMock(return_value=mock_resp) | ||
with open(self.test_file_path, "rb") as f, patch( | ||
"aiohttp.ClientSession.request", mock_request | ||
): | ||
returned = await self.fake_api.upload_file_with_additional_properties( | ||
file=(self.test_file_path, f.read()), | ||
count=100, | ||
object=petstore_api.UploadFileWithAdditionalPropertiesRequestObject( | ||
name="foo" | ||
), | ||
) | ||
|
||
assert ( | ||
returned.code == 200 | ||
and returned.type == "success" | ||
and returned.message == "OK" | ||
) | ||
|
||
mock_request.assert_called_once() | ||
|
||
formdata = mock_request.call_args_list[0].kwargs["data"] | ||
|
||
data_object = get_field_from_formdata(formdata, "object") | ||
data_count = get_field_from_formdata(formdata, "count") | ||
|
||
assert type(data_count) is str | ||
assert type(data_object) is str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters