mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-21 19:18:41 +00:00
deserialize data form media type py27
This commit is contained in:
parent
852c081068
commit
5f62c62937
2 changed files with 18 additions and 30 deletions
|
@ -1,4 +1,4 @@
|
|||
from email.parser import BytesParser
|
||||
from email.parser import Parser
|
||||
from json import loads
|
||||
|
||||
from six import binary_type
|
||||
|
@ -17,10 +17,10 @@ def urlencoded_form_loads(value):
|
|||
|
||||
|
||||
def data_form_loads(value):
|
||||
if issubclass(type(value), str):
|
||||
value = value.encode()
|
||||
parser = BytesParser()
|
||||
parts = parser.parsebytes(value)
|
||||
if issubclass(type(value), binary_type):
|
||||
value = value.decode('ASCII', errors='surrogateescape')
|
||||
parser = Parser()
|
||||
parts = parser.parsestr(value, headersonly=False)
|
||||
return dict(
|
||||
(
|
||||
part.get_param('name', header='content-disposition'),
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.nonmultipart import MIMENonMultipart
|
||||
|
||||
import pytest
|
||||
|
||||
from six import b, u
|
||||
|
||||
from openapi_core.deserializing.exceptions import DeserializeError
|
||||
from openapi_core.deserializing.media_types.factories import (
|
||||
MediaTypeDeserializersFactory,
|
||||
|
@ -10,24 +9,6 @@ from openapi_core.deserializing.media_types.factories import (
|
|||
from openapi_core.schema.media_types.models import MediaType
|
||||
|
||||
|
||||
class MIMEFormdata(MIMENonMultipart):
|
||||
def __init__(self, keyname, *args, **kwargs):
|
||||
super(MIMEFormdata, self).__init__(*args, **kwargs)
|
||||
self.add_header(
|
||||
"Content-Disposition", "form-data; name=\"%s\"" % keyname)
|
||||
|
||||
|
||||
def encode_multipart_formdata(fields):
|
||||
m = MIMEMultipart("form-data")
|
||||
|
||||
for field, value in fields.items():
|
||||
data = MIMEFormdata(field, "text", "plain")
|
||||
data.set_payload(value)
|
||||
m.attach(data)
|
||||
|
||||
return m
|
||||
|
||||
|
||||
class TestMediaTypeDeserializer(object):
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -68,7 +49,7 @@ class TestMediaTypeDeserializer(object):
|
|||
|
||||
assert result == {'param1': 'test'}
|
||||
|
||||
@pytest.mark.parametrize('value', [b'', ''])
|
||||
@pytest.mark.parametrize('value', [b(''), u('')])
|
||||
def test_data_form_empty(self, deserializer_factory, value):
|
||||
media_type = MediaType('multipart/form-data')
|
||||
|
||||
|
@ -78,12 +59,19 @@ class TestMediaTypeDeserializer(object):
|
|||
|
||||
def test_data_form_simple(self, deserializer_factory):
|
||||
media_type = MediaType('multipart/form-data')
|
||||
formdata = encode_multipart_formdata({'param1': 'test'})
|
||||
value = str(formdata)
|
||||
value = b(
|
||||
'Content-Type: multipart/form-data; boundary="'
|
||||
'===============2872712225071193122=="\n'
|
||||
'MIME-Version: 1.0\n\n'
|
||||
'--===============2872712225071193122==\n'
|
||||
'Content-Type: text/plain\nMIME-Version: 1.0\n'
|
||||
'Content-Disposition: form-data; name="param1"\n\ntest\n'
|
||||
'--===============2872712225071193122==--\n'
|
||||
)
|
||||
|
||||
result = deserializer_factory(media_type)(value)
|
||||
|
||||
assert result == {'param1': b'test'}
|
||||
assert result == {'param1': b('test')}
|
||||
|
||||
def test_custom_simple(self, deserializer_factory):
|
||||
custom_mimetype = 'application/custom'
|
||||
|
|
Loading…
Reference in a new issue