mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
More tests passing
This commit is contained in:
parent
338b5213fa
commit
d4ed178456
|
@ -27,38 +27,7 @@ except ImportError:
|
|||
class ParserMixin(object):
|
||||
parsers = ()
|
||||
|
||||
def parse(self, stream, content_type):
|
||||
"""
|
||||
Parse the request content.
|
||||
|
||||
May raise a 415 ResponseException (Unsupported Media Type),
|
||||
or a 400 ResponseException (Bad Request).
|
||||
"""
|
||||
parsers = as_tuple(self.parsers)
|
||||
|
||||
parser = None
|
||||
for parser_cls in parsers:
|
||||
if parser_cls.handles(content_type):
|
||||
parser = parser_cls(self)
|
||||
break
|
||||
|
||||
if parser is None:
|
||||
raise ResponseException(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||
{'error': 'Unsupported media type in request \'%s\'.' %
|
||||
content_type.media_type})
|
||||
|
||||
return parser.parse(stream)
|
||||
|
||||
@property
|
||||
def parsed_media_types(self):
|
||||
"""Return an list of all the media types that this ParserMixin can parse."""
|
||||
return [parser.media_type for parser in self.parsers]
|
||||
|
||||
@property
|
||||
def default_parser(self):
|
||||
"""Return the ParerMixin's most prefered emitter.
|
||||
(This has no behavioural effect, but is may be used by documenting emitters)"""
|
||||
return self.parsers[0]
|
||||
|
||||
|
||||
class BaseParser(object):
|
||||
|
@ -122,6 +91,18 @@ class DataFlatener(object):
|
|||
return False
|
||||
|
||||
|
||||
class PlainTextParser(BaseParser):
|
||||
"""
|
||||
Plain text parser.
|
||||
|
||||
Simply returns the content of the stream
|
||||
"""
|
||||
media_type = MediaType('text/plain')
|
||||
|
||||
def parse(self, stream):
|
||||
return stream.read()
|
||||
|
||||
|
||||
class FormParser(BaseParser, DataFlatener):
|
||||
"""The default parser for form data.
|
||||
Return a dict containing a single value for each non-reserved parameter.
|
||||
|
|
|
@ -37,50 +37,51 @@ class TestContentMixins(TestCase):
|
|||
self.assertEqual(view.RAW_CONTENT, None)
|
||||
|
||||
def ensure_determines_form_content_POST(self, view):
|
||||
"""Ensure determine_content(request) returns content for POST request with content."""
|
||||
"""Ensure view.RAW_CONTENT returns content for POST request with form content."""
|
||||
form_data = {'qwerty': 'uiop'}
|
||||
view.parsers = (FormParser, MultipartParser)
|
||||
view.request = self.req.post('/', data=form_data)
|
||||
self.assertEqual(view.RAW_CONTENT, form_data)
|
||||
|
||||
# def ensure_determines_non_form_content_POST(self, mixin):
|
||||
# """Ensure determine_content(request) returns (content type, content) for POST request with content."""
|
||||
# content = 'qwerty'
|
||||
# content_type = 'text/plain'
|
||||
# request = self.req.post('/', content, content_type=content_type)
|
||||
# self.assertEqual(mixin.determine_content(request), (content_type, content))
|
||||
#
|
||||
# def ensure_determines_form_content_PUT(self, mixin):
|
||||
# """Ensure determine_content(request) returns content for PUT request with content."""
|
||||
# form_data = {'qwerty': 'uiop'}
|
||||
# request = self.req.put('/', data=form_data)
|
||||
# self.assertEqual(mixin.determine_content(request), (request.META['CONTENT_TYPE'], request.raw_post_data))
|
||||
#
|
||||
# def ensure_determines_non_form_content_PUT(self, mixin):
|
||||
# """Ensure determine_content(request) returns (content type, content) for PUT request with content."""
|
||||
# content = 'qwerty'
|
||||
# content_type = 'text/plain'
|
||||
# request = self.req.put('/', content, content_type=content_type)
|
||||
# self.assertEqual(mixin.determine_content(request), (content_type, content))
|
||||
#
|
||||
# # StandardContentMixin behavioural tests
|
||||
#
|
||||
def ensure_determines_non_form_content_POST(self, mixin):
|
||||
"""Ensure view.RAW_CONTENT returns content for POST request with non-form content."""
|
||||
content = 'qwerty'
|
||||
content_type = 'text/plain'
|
||||
view.parsers = (PlainTextParser,)
|
||||
view.request = self.req.post('/', content, content_type=content_type)
|
||||
self.assertEqual(view.RAW_CONTENT, form_data)
|
||||
|
||||
def ensure_determines_form_content_PUT(self, mixin):
|
||||
"""Ensure view.RAW_CONTENT returns content for PUT request with form content."""
|
||||
form_data = {'qwerty': 'uiop'}
|
||||
view.parsers = (FormParser, MultipartParser)
|
||||
view.request = self.req.put('/', data=form_data)
|
||||
self.assertEqual(view.RAW_CONTENT, form_data)
|
||||
|
||||
def ensure_determines_non_form_content_PUT(self, mixin):
|
||||
"""Ensure view.RAW_CONTENT returns content for PUT request with non-form content."""
|
||||
content = 'qwerty'
|
||||
content_type = 'text/plain'
|
||||
view.parsers = (PlainTextParser,)
|
||||
view.request = self.req.post('/', content, content_type=content_type)
|
||||
self.assertEqual(view.RAW_CONTENT, form_data)#
|
||||
|
||||
def test_standard_behaviour_determines_no_content_GET(self):
|
||||
"""Ensure request.RAW_CONTENT returns None for GET request with no content."""
|
||||
self.ensure_determines_no_content_GET(RequestMixin())
|
||||
|
||||
def test_standard_behaviour_determines_form_content_POST(self):
|
||||
"""Ensure request.RAW_CONTENT returns content for POST request with content."""
|
||||
"""Ensure request.RAW_CONTENT returns content for POST request with form content."""
|
||||
self.ensure_determines_form_content_POST(RequestMixin())
|
||||
#
|
||||
# def test_standard_behaviour_determines_non_form_content_POST(self):
|
||||
# """Ensure StandardContentMixin.determine_content(request) returns (content type, content) for POST request with content."""
|
||||
# self.ensure_determines_non_form_content_POST(StandardContentMixin())
|
||||
#
|
||||
# def test_standard_behaviour_determines_form_content_PUT(self):
|
||||
# """Ensure StandardContentMixin.determine_content(request) returns content for PUT request with content."""
|
||||
# self.ensure_determines_form_content_PUT(StandardContentMixin())
|
||||
#
|
||||
|
||||
def test_standard_behaviour_determines_non_form_content_POST(self):
|
||||
"""Ensure StandardContentMixin.determine_content(request) returns (content type, content) for POST request with content."""
|
||||
self.ensure_determines_non_form_content_POST(RequestMixin())
|
||||
|
||||
def test_standard_behaviour_determines_form_content_PUT(self):
|
||||
"""Ensure StandardContentMixin.determine_content(request) returns content for PUT request with content."""
|
||||
self.ensure_determines_form_content_PUT(RequestMixin())
|
||||
|
||||
# def test_standard_behaviour_determines_non_form_content_PUT(self):
|
||||
# """Ensure StandardContentMixin.determine_content(request) returns (content type, content) for PUT request with content."""
|
||||
# self.ensure_determines_non_form_content_PUT(StandardContentMixin())
|
||||
|
|
Loading…
Reference in New Issue
Block a user