2024-10-18 21:04:45 +00:00
|
|
|
import typing
|
2021-02-26 03:55:07 +00:00
|
|
|
|
2024-10-15 21:31:10 +00:00
|
|
|
import tornado.httpclient
|
|
|
|
import tornado.testing
|
2024-10-18 21:04:45 +00:00
|
|
|
import openapi_core
|
2021-02-26 03:55:07 +00:00
|
|
|
|
2024-10-18 21:04:45 +00:00
|
|
|
from tornado_openapi3.requests import TornadoOpenAPIRequest
|
|
|
|
from tornado_openapi3.responses import TornadoOpenAPIResponse
|
|
|
|
from tornado_openapi3.types import Deserializer, Formatter
|
2021-02-26 03:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncOpenAPITestCase(tornado.testing.AsyncHTTPTestCase):
|
2021-03-05 20:23:23 +00:00
|
|
|
"""A test case that starts up an HTTP server.
|
|
|
|
|
2021-03-19 15:17:18 +00:00
|
|
|
An async test case extending :class:`tornado.testing.AsyncHTTPTestCase`,
|
2021-03-05 20:23:23 +00:00
|
|
|
providing OpenAPI spec validation on the responses from your application and
|
|
|
|
raising errors in tests.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def spec_dict(self) -> dict:
|
|
|
|
"""The OpenAPI 3 specification
|
|
|
|
|
|
|
|
Override this in your test cases to load or define your OpenAPI 3 spec.
|
|
|
|
|
|
|
|
:rtype: dict
|
|
|
|
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-02-26 17:03:04 +00:00
|
|
|
@property
|
2024-10-18 21:04:45 +00:00
|
|
|
def spec(self) -> openapi_core.OpenAPI:
|
2021-02-27 04:03:33 +00:00
|
|
|
"""The OpenAPI 3 specification.
|
2021-02-26 17:03:04 +00:00
|
|
|
|
2021-03-05 20:23:23 +00:00
|
|
|
Override this in your test cases to customize how your OpenAPI 3 spec is
|
|
|
|
loaded and validated.
|
|
|
|
|
2021-03-19 15:17:18 +00:00
|
|
|
:rtype: :class:`openapi_core.schema.specs.model.Spec`
|
2021-02-26 17:03:04 +00:00
|
|
|
|
|
|
|
"""
|
2024-10-18 21:04:45 +00:00
|
|
|
config = openapi_core.Config(
|
|
|
|
extra_format_unmarshallers={
|
|
|
|
format: formatter.unmarshal
|
|
|
|
for format, formatter in self.custom_formatters.items()
|
|
|
|
},
|
|
|
|
extra_format_validators={
|
|
|
|
format: formatter.validate
|
|
|
|
for format, formatter in self.custom_formatters.items()
|
|
|
|
},
|
|
|
|
extra_media_type_deserializers=self.custom_media_type_deserializers,
|
|
|
|
)
|
|
|
|
return openapi_core.OpenAPI.from_dict(self.spec_dict, config=config)
|
2021-02-26 17:03:04 +00:00
|
|
|
|
2021-03-18 15:03:36 +00:00
|
|
|
@property
|
2024-10-18 21:04:45 +00:00
|
|
|
def custom_formatters(self) -> typing.Dict[str, Formatter]:
|
2021-03-18 15:03:36 +00:00
|
|
|
"""A dictionary mapping value formats to formatter objects.
|
|
|
|
|
|
|
|
A formatter object must provide:
|
|
|
|
- validate(self, value) -> bool
|
|
|
|
- unmarshal(self, value) -> Any
|
|
|
|
"""
|
|
|
|
|
|
|
|
return dict()
|
|
|
|
|
2021-02-26 17:03:04 +00:00
|
|
|
@property
|
2024-10-18 21:04:45 +00:00
|
|
|
def custom_media_type_deserializers(self) -> typing.Dict[str, Deserializer]:
|
2021-02-26 17:03:04 +00:00
|
|
|
"""A dictionary mapping media types to deserializing functions.
|
|
|
|
|
|
|
|
If your endpoints make use of content types beyond ``application/json``,
|
|
|
|
you must add them to this dictionary with a deserializing method that
|
|
|
|
converts the raw body (as ``bytes`` or ``str``) to Python objects.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return dict()
|
2021-02-26 03:55:07 +00:00
|
|
|
|
|
|
|
def fetch(
|
2024-10-18 21:04:45 +00:00
|
|
|
self, path: str, raise_error: bool = False, **kwargs: typing.Any
|
2021-02-26 03:55:07 +00:00
|
|
|
) -> tornado.httpclient.HTTPResponse:
|
2021-02-26 17:03:04 +00:00
|
|
|
"""Convenience methiod to synchronously fetch a URL.
|
|
|
|
|
|
|
|
Extends the fetch method in Tornado's
|
|
|
|
:class:``tornado.testing.AsyncHTTPTestCase`` to perform OpenAPI 3
|
|
|
|
validation on the response received before returning it. If validation
|
|
|
|
fails, an :class:`openapi_core.exceptions.OpenAPIError` will be raised
|
|
|
|
describing the failure.
|
|
|
|
|
2021-04-03 05:06:02 +00:00
|
|
|
If the path begins with http:// or https://, it will be treated as a
|
|
|
|
full URL and will be fetched as-is, and no validation will occur.
|
|
|
|
|
2021-02-26 17:03:04 +00:00
|
|
|
"""
|
2021-04-03 05:06:02 +00:00
|
|
|
if path.lower().startswith(("http://", "https://")):
|
|
|
|
return super().fetch(path, raise_error=raise_error, **kwargs)
|
|
|
|
|
2021-02-26 03:55:07 +00:00
|
|
|
response = super().fetch(path, raise_error=False, **kwargs)
|
2024-10-18 21:04:45 +00:00
|
|
|
result = self.spec.unmarshal_response(
|
|
|
|
request=TornadoOpenAPIRequest(response.request),
|
|
|
|
response=TornadoOpenAPIResponse(response),
|
|
|
|
)
|
2021-02-26 03:55:07 +00:00
|
|
|
result.raise_for_errors()
|
|
|
|
if raise_error:
|
|
|
|
response.rethrow()
|
|
|
|
return response
|