mirror of
https://github.com/correl/tornado-openapi3.git
synced 2024-11-23 19:19:55 +00:00
45 lines
936 B
ReStructuredText
45 lines
936 B
ReStructuredText
|
Testing API Responses
|
||
|
=====================
|
||
|
|
||
|
Adding custom deserializers
|
||
|
---------------------------
|
||
|
|
||
|
.. code-block:: python
|
||
|
|
||
|
import json
|
||
|
|
||
|
from tornado_openapi3.testing import AsyncOpenAPITestCase
|
||
|
|
||
|
|
||
|
class TestCase(AsyncOpenAPITestCase):
|
||
|
custom_media_type_deserializers = {
|
||
|
"application/vnd.example.resource+json": json.loads,
|
||
|
}
|
||
|
|
||
|
...
|
||
|
|
||
|
Adding custom formatters
|
||
|
------------------------
|
||
|
|
||
|
.. code-block:: python
|
||
|
|
||
|
import datetime
|
||
|
|
||
|
from tornado_openapi3.testing import AsyncOpenAPITestCase
|
||
|
|
||
|
|
||
|
class USDateFormatter:
|
||
|
def validate(self, value: str) -> bool:
|
||
|
return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))
|
||
|
|
||
|
def unmarshal(self, value: str) -> datetime.date:
|
||
|
return datetime.datetime.strptime(value, "%m/%d/%Y").date()
|
||
|
|
||
|
|
||
|
class TestCase(AsyncOpenAPITestCase):
|
||
|
custom_formatters = {
|
||
|
"usdate": USDateFormatter(),
|
||
|
}
|
||
|
|
||
|
...
|