-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from python-openapi/feature/request-response-…
…binary-format-integration-tests request response binary format integration tests
- Loading branch information
Showing
32 changed files
with
751 additions
and
10 deletions.
There are no files selected for viewing
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
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
Empty file.
15 changes: 15 additions & 0 deletions
15
tests/integration/contrib/aiohttp/data/v3.0/aiohttpproject/__main__.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,15 @@ | ||
from aiohttp import web | ||
from aiohttpproject.pets.views import PetPhotoView | ||
|
||
routes = [ | ||
web.view("/v1/pets/{petId}/photo", PetPhotoView), | ||
] | ||
|
||
|
||
def get_app(loop=None): | ||
app = web.Application(loop=loop) | ||
app.add_routes(routes) | ||
return app | ||
|
||
|
||
app = get_app() |
9 changes: 9 additions & 0 deletions
9
tests/integration/contrib/aiohttp/data/v3.0/aiohttpproject/openapi.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,9 @@ | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
from openapi_core import Spec | ||
|
||
openapi_spec_path = Path("tests/integration/data/v3.0/petstore.yaml") | ||
spec_dict = yaml.load(openapi_spec_path.read_text(), yaml.Loader) | ||
spec = Spec.from_dict(spec_dict) |
Empty file.
52 changes: 52 additions & 0 deletions
52
tests/integration/contrib/aiohttp/data/v3.0/aiohttpproject/pets/views.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,52 @@ | ||
from base64 import b64decode | ||
from io import BytesIO | ||
|
||
from aiohttp import web | ||
from aiohttpproject.openapi import spec | ||
from multidict import MultiDict | ||
|
||
from openapi_core import unmarshal_request | ||
from openapi_core import unmarshal_response | ||
from openapi_core.contrib.aiohttp import AIOHTTPOpenAPIWebRequest | ||
from openapi_core.contrib.aiohttp import AIOHTTPOpenAPIWebResponse | ||
|
||
|
||
class PetPhotoView(web.View): | ||
OPENID_LOGO = b64decode( | ||
""" | ||
R0lGODlhEAAQAMQAAO3t7eHh4srKyvz8/P5pDP9rENLS0v/28P/17tXV1dHEvPDw8M3Nzfn5+d3d | ||
3f5jA97Syvnv6MfLzcfHx/1mCPx4Kc/S1Pf189C+tP+xgv/k1N3OxfHy9NLV1/39/f///yH5BAAA | ||
AAAALAAAAAAQABAAAAVq4CeOZGme6KhlSDoexdO6H0IUR+otwUYRkMDCUwIYJhLFTyGZJACAwQcg | ||
EAQ4kVuEE2AIGAOPQQAQwXCfS8KQGAwMjIYIUSi03B7iJ+AcnmclHg4TAh0QDzIpCw4WGBUZeikD | ||
Fzk0lpcjIQA7 | ||
""" | ||
) | ||
|
||
async def get(self): | ||
request_body = await self.request.text() | ||
openapi_request = AIOHTTPOpenAPIWebRequest( | ||
self.request, body=request_body | ||
) | ||
request_unmarshalled = unmarshal_request(openapi_request, spec=spec) | ||
response = web.Response( | ||
body=self.OPENID_LOGO, | ||
content_type="image/gif", | ||
) | ||
openapi_response = AIOHTTPOpenAPIWebResponse(response) | ||
response_unmarshalled = unmarshal_response( | ||
openapi_request, openapi_response, spec=spec | ||
) | ||
return response | ||
|
||
async def post(self): | ||
request_body = await self.request.read() | ||
openapi_request = AIOHTTPOpenAPIWebRequest( | ||
self.request, body=request_body | ||
) | ||
request_unmarshalled = unmarshal_request(openapi_request, spec=spec) | ||
response = web.Response(status=201) | ||
openapi_response = AIOHTTPOpenAPIWebResponse(response) | ||
response_unmarshalled = unmarshal_response( | ||
openapi_request, openapi_response, spec=spec | ||
) | ||
return response |
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,83 @@ | ||
import os | ||
import sys | ||
from base64 import b64encode | ||
|
||
import pytest | ||
from starlette.testclient import TestClient | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="session") | ||
def project_setup(): | ||
directory = os.path.abspath(os.path.dirname(__file__)) | ||
project_dir = os.path.join(directory, "data/v3.0") | ||
sys.path.insert(0, project_dir) | ||
yield | ||
sys.path.remove(project_dir) | ||
|
||
|
||
@pytest.fixture | ||
def app(project_setup, loop): | ||
from aiohttpproject.__main__ import get_app | ||
|
||
return get_app(loop=loop) | ||
|
||
|
||
@pytest.fixture | ||
async def client(app, aiohttp_client): | ||
return await aiohttp_client(app) | ||
|
||
|
||
class BaseTestPetstore: | ||
api_key = "12345" | ||
|
||
@property | ||
def api_key_encoded(self): | ||
api_key_bytes = self.api_key.encode("utf8") | ||
api_key_bytes_enc = b64encode(api_key_bytes) | ||
return str(api_key_bytes_enc, "utf8") | ||
|
||
|
||
class TestPetPhotoView(BaseTestPetstore): | ||
@pytest.mark.xfail( | ||
reason="response binary format not supported", | ||
strict=True, | ||
) | ||
async def test_get_valid(self, client, data_gif): | ||
headers = { | ||
"Authorization": "Basic testuser", | ||
"Api-Key": self.api_key_encoded, | ||
"Host": "petstore.swagger.io", | ||
} | ||
|
||
cookies = {"user": "1"} | ||
response = await client.get( | ||
"/v1/pets/1/photo", | ||
headers=headers, | ||
cookies=cookies, | ||
) | ||
|
||
assert await response.content.read() == data_gif | ||
assert response.status == 200 | ||
|
||
async def test_post_valid(self, client, data_gif): | ||
content_type = "image/gif" | ||
headers = { | ||
"Authorization": "Basic testuser", | ||
"Api-Key": self.api_key_encoded, | ||
"Content-Type": content_type, | ||
"Host": "petstore.swagger.io", | ||
} | ||
data = { | ||
"file": data_gif, | ||
} | ||
|
||
cookies = {"user": "1"} | ||
response = await client.post( | ||
"/v1/pets/1/photo", | ||
headers=headers, | ||
data=data, | ||
cookies=cookies, | ||
) | ||
|
||
assert not await response.text() | ||
assert response.status == 201 |
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
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
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
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
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
Oops, something went wrong.