More tests passing

This commit is contained in:
Tom Christie 2011-04-11 11:54:26 +01:00
parent 338b5213fa
commit d4ed178456
2 changed files with 46 additions and 64 deletions

View File

@ -27,38 +27,7 @@ except ImportError:
class ParserMixin(object): class ParserMixin(object):
parsers = () 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): class BaseParser(object):
@ -122,6 +91,18 @@ class DataFlatener(object):
return False 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): class FormParser(BaseParser, DataFlatener):
"""The default parser for form data. """The default parser for form data.
Return a dict containing a single value for each non-reserved parameter. Return a dict containing a single value for each non-reserved parameter.

View File

@ -37,50 +37,51 @@ class TestContentMixins(TestCase):
self.assertEqual(view.RAW_CONTENT, None) self.assertEqual(view.RAW_CONTENT, None)
def ensure_determines_form_content_POST(self, view): 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'} form_data = {'qwerty': 'uiop'}
view.parsers = (FormParser, MultipartParser) view.parsers = (FormParser, MultipartParser)
view.request = self.req.post('/', data=form_data) view.request = self.req.post('/', data=form_data)
self.assertEqual(view.RAW_CONTENT, form_data) self.assertEqual(view.RAW_CONTENT, form_data)
# def ensure_determines_non_form_content_POST(self, mixin): def ensure_determines_non_form_content_POST(self, mixin):
# """Ensure determine_content(request) returns (content type, content) for POST request with content.""" """Ensure view.RAW_CONTENT returns content for POST request with non-form content."""
# content = 'qwerty' content = 'qwerty'
# content_type = 'text/plain' content_type = 'text/plain'
# request = self.req.post('/', content, content_type=content_type) view.parsers = (PlainTextParser,)
# self.assertEqual(mixin.determine_content(request), (content_type, content)) 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 determine_content(request) returns content for PUT request with content.""" def ensure_determines_form_content_PUT(self, mixin):
# form_data = {'qwerty': 'uiop'} """Ensure view.RAW_CONTENT returns content for PUT request with form content."""
# request = self.req.put('/', data=form_data) form_data = {'qwerty': 'uiop'}
# self.assertEqual(mixin.determine_content(request), (request.META['CONTENT_TYPE'], request.raw_post_data)) view.parsers = (FormParser, MultipartParser)
# view.request = self.req.put('/', data=form_data)
# def ensure_determines_non_form_content_PUT(self, mixin): self.assertEqual(view.RAW_CONTENT, form_data)
# """Ensure determine_content(request) returns (content type, content) for PUT request with content."""
# content = 'qwerty' def ensure_determines_non_form_content_PUT(self, mixin):
# content_type = 'text/plain' """Ensure view.RAW_CONTENT returns content for PUT request with non-form content."""
# request = self.req.put('/', content, content_type=content_type) content = 'qwerty'
# self.assertEqual(mixin.determine_content(request), (content_type, content)) content_type = 'text/plain'
# view.parsers = (PlainTextParser,)
# # StandardContentMixin behavioural tests 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): def test_standard_behaviour_determines_no_content_GET(self):
"""Ensure request.RAW_CONTENT returns None for GET request with no content.""" """Ensure request.RAW_CONTENT returns None for GET request with no content."""
self.ensure_determines_no_content_GET(RequestMixin()) self.ensure_determines_no_content_GET(RequestMixin())
def test_standard_behaviour_determines_form_content_POST(self): 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()) self.ensure_determines_form_content_POST(RequestMixin())
#
# def test_standard_behaviour_determines_non_form_content_POST(self): def test_standard_behaviour_determines_non_form_content_POST(self):
# """Ensure StandardContentMixin.determine_content(request) returns (content type, content) for POST request with content.""" """Ensure StandardContentMixin.determine_content(request) returns (content type, content) for POST request with content."""
# self.ensure_determines_non_form_content_POST(StandardContentMixin()) self.ensure_determines_non_form_content_POST(RequestMixin())
#
# def test_standard_behaviour_determines_form_content_PUT(self): def test_standard_behaviour_determines_form_content_PUT(self):
# """Ensure StandardContentMixin.determine_content(request) returns content for PUT request with content.""" """Ensure StandardContentMixin.determine_content(request) returns content for PUT request with content."""
# self.ensure_determines_form_content_PUT(StandardContentMixin()) self.ensure_determines_form_content_PUT(RequestMixin())
#
# def test_standard_behaviour_determines_non_form_content_PUT(self): # def test_standard_behaviour_determines_non_form_content_PUT(self):
# """Ensure StandardContentMixin.determine_content(request) returns (content type, content) for PUT request with content.""" # """Ensure StandardContentMixin.determine_content(request) returns (content type, content) for PUT request with content."""
# self.ensure_determines_non_form_content_PUT(StandardContentMixin()) # self.ensure_determines_non_form_content_PUT(StandardContentMixin())