mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-21 19:18:41 +00:00
media type deserialize form urlencoded
This commit is contained in:
parent
6a222b93df
commit
74920a77de
5 changed files with 113 additions and 89 deletions
|
@ -1,4 +1,4 @@
|
|||
from openapi_core.deserializing.media_types.util import json_loads
|
||||
from openapi_core.deserializing.media_types.util import json_loads, form_loads
|
||||
|
||||
from openapi_core.deserializing.media_types.deserializers import (
|
||||
PrimitiveDeserializer,
|
||||
|
@ -9,6 +9,7 @@ class MediaTypeDeserializersFactory(object):
|
|||
|
||||
MEDIA_TYPE_DESERIALIZERS = {
|
||||
'application/json': json_loads,
|
||||
'application/x-www-form-urlencoded': form_loads,
|
||||
}
|
||||
|
||||
def __init__(self, custom_deserializers=None):
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from json import loads
|
||||
|
||||
from six import binary_type
|
||||
from six.moves.urllib.parse import parse_qsl
|
||||
|
||||
|
||||
def json_loads(value):
|
||||
|
@ -8,3 +9,7 @@ def json_loads(value):
|
|||
if isinstance(value, (binary_type, )):
|
||||
value = value.decode()
|
||||
return loads(value)
|
||||
|
||||
|
||||
def form_loads(value):
|
||||
return dict(parse_qsl(value))
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
import pytest
|
||||
|
||||
from openapi_core.deserializing.exceptions import DeserializeError
|
||||
from openapi_core.deserializing.media_types.factories import (
|
||||
MediaTypeDeserializersFactory,
|
||||
)
|
||||
from openapi_core.deserializing.parameters.factories import (
|
||||
ParameterDeserializersFactory,
|
||||
)
|
||||
from openapi_core.deserializing.parameters.exceptions import (
|
||||
EmptyParameterValue,
|
||||
)
|
||||
from openapi_core.schema.media_types.models import MediaType
|
||||
from openapi_core.schema.parameters.models import Parameter
|
||||
|
||||
|
||||
class TestParameterDeserialise(object):
|
||||
|
||||
@pytest.fixture
|
||||
def deserializer_factory(self):
|
||||
def create_deserializer(param):
|
||||
return ParameterDeserializersFactory().create(param)
|
||||
return create_deserializer
|
||||
|
||||
def test_deprecated(self, deserializer_factory):
|
||||
param = Parameter('param', 'query', deprecated=True)
|
||||
value = 'test'
|
||||
|
||||
with pytest.warns(DeprecationWarning):
|
||||
result = deserializer_factory(param)(value)
|
||||
|
||||
assert result == value
|
||||
|
||||
def test_query_empty(self, deserializer_factory):
|
||||
param = Parameter('param', 'query')
|
||||
value = ''
|
||||
|
||||
with pytest.raises(EmptyParameterValue):
|
||||
deserializer_factory(param)(value)
|
||||
|
||||
def test_query_valid(self, deserializer_factory):
|
||||
param = Parameter('param', 'query')
|
||||
value = 'test'
|
||||
|
||||
result = deserializer_factory(param)(value)
|
||||
|
||||
assert result == value
|
||||
|
||||
|
||||
class TestMediaTypeDeserialise(object):
|
||||
|
||||
@pytest.fixture
|
||||
def deserializer_factory(self):
|
||||
def create_deserializer(media_type, custom_deserializers=None):
|
||||
return MediaTypeDeserializersFactory(
|
||||
custom_deserializers=custom_deserializers).create(media_type)
|
||||
return create_deserializer
|
||||
|
||||
def test_empty(self, deserializer_factory):
|
||||
media_type = MediaType('application/json')
|
||||
value = ''
|
||||
|
||||
with pytest.raises(DeserializeError):
|
||||
deserializer_factory(media_type)(value)
|
||||
|
||||
def test_no_schema_deserialised(self, deserializer_factory):
|
||||
media_type = MediaType('application/json')
|
||||
value = "{}"
|
||||
|
||||
result = deserializer_factory(media_type)(value)
|
||||
|
||||
assert result == {}
|
||||
|
||||
def test_no_schema_custom_deserialiser(self, deserializer_factory):
|
||||
custom_mimetype = 'application/custom'
|
||||
media_type = MediaType(custom_mimetype)
|
||||
value = "{}"
|
||||
|
||||
def custom_deserializer(value):
|
||||
return 'custom'
|
||||
custom_deserializers = {
|
||||
custom_mimetype: custom_deserializer,
|
||||
}
|
||||
|
||||
result = deserializer_factory(
|
||||
media_type, custom_deserializers=custom_deserializers)(value)
|
||||
|
||||
assert result == 'custom'
|
64
tests/unit/deserializing/test_media_types_deserializers.py
Normal file
64
tests/unit/deserializing/test_media_types_deserializers.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
import pytest
|
||||
|
||||
from openapi_core.deserializing.exceptions import DeserializeError
|
||||
from openapi_core.deserializing.media_types.factories import (
|
||||
MediaTypeDeserializersFactory,
|
||||
)
|
||||
from openapi_core.schema.media_types.models import MediaType
|
||||
|
||||
|
||||
class TestMediaTypeDeserializer(object):
|
||||
|
||||
@pytest.fixture
|
||||
def deserializer_factory(self):
|
||||
def create_deserializer(media_type, custom_deserializers=None):
|
||||
return MediaTypeDeserializersFactory(
|
||||
custom_deserializers=custom_deserializers).create(media_type)
|
||||
return create_deserializer
|
||||
|
||||
def test_json_empty(self, deserializer_factory):
|
||||
media_type = MediaType('application/json')
|
||||
value = ''
|
||||
|
||||
with pytest.raises(DeserializeError):
|
||||
deserializer_factory(media_type)(value)
|
||||
|
||||
def test_json_empty_object(self, deserializer_factory):
|
||||
media_type = MediaType('application/json')
|
||||
value = "{}"
|
||||
|
||||
result = deserializer_factory(media_type)(value)
|
||||
|
||||
assert result == {}
|
||||
|
||||
def test_form_urlencoded_empty(self, deserializer_factory):
|
||||
media_type = MediaType('application/x-www-form-urlencoded')
|
||||
value = ''
|
||||
|
||||
result = deserializer_factory(media_type)(value)
|
||||
|
||||
assert result == {}
|
||||
|
||||
def test_form_urlencoded_simple(self, deserializer_factory):
|
||||
media_type = MediaType('application/x-www-form-urlencoded')
|
||||
value = 'param1=test'
|
||||
|
||||
result = deserializer_factory(media_type)(value)
|
||||
|
||||
assert result == {'param1': 'test'}
|
||||
|
||||
def test_custom_simple(self, deserializer_factory):
|
||||
custom_mimetype = 'application/custom'
|
||||
media_type = MediaType(custom_mimetype)
|
||||
value = "{}"
|
||||
|
||||
def custom_deserializer(value):
|
||||
return 'custom'
|
||||
custom_deserializers = {
|
||||
custom_mimetype: custom_deserializer,
|
||||
}
|
||||
|
||||
result = deserializer_factory(
|
||||
media_type, custom_deserializers=custom_deserializers)(value)
|
||||
|
||||
assert result == 'custom'
|
42
tests/unit/deserializing/test_parameters_deserializers.py
Normal file
42
tests/unit/deserializing/test_parameters_deserializers.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
import pytest
|
||||
|
||||
from openapi_core.deserializing.parameters.factories import (
|
||||
ParameterDeserializersFactory,
|
||||
)
|
||||
from openapi_core.deserializing.parameters.exceptions import (
|
||||
EmptyParameterValue,
|
||||
)
|
||||
from openapi_core.schema.parameters.models import Parameter
|
||||
|
||||
|
||||
class TestParameterDeserializer(object):
|
||||
|
||||
@pytest.fixture
|
||||
def deserializer_factory(self):
|
||||
def create_deserializer(param):
|
||||
return ParameterDeserializersFactory().create(param)
|
||||
return create_deserializer
|
||||
|
||||
def test_deprecated(self, deserializer_factory):
|
||||
param = Parameter('param', 'query', deprecated=True)
|
||||
value = 'test'
|
||||
|
||||
with pytest.warns(DeprecationWarning):
|
||||
result = deserializer_factory(param)(value)
|
||||
|
||||
assert result == value
|
||||
|
||||
def test_query_empty(self, deserializer_factory):
|
||||
param = Parameter('param', 'query')
|
||||
value = ''
|
||||
|
||||
with pytest.raises(EmptyParameterValue):
|
||||
deserializer_factory(param)(value)
|
||||
|
||||
def test_query_valid(self, deserializer_factory):
|
||||
param = Parameter('param', 'query')
|
||||
value = 'test'
|
||||
|
||||
result = deserializer_factory(param)(value)
|
||||
|
||||
assert result == value
|
Loading…
Reference in a new issue