form overloading tests passing

This commit is contained in:
Tom Christie 2011-04-11 12:19:28 +01:00
parent e29a3f4cf1
commit 0fe8d1a15d
3 changed files with 29 additions and 19 deletions

View File

@ -1,6 +1,7 @@
from djangorestframework.mediatypes import MediaType from djangorestframework.mediatypes import MediaType
from djangorestframework.utils import as_tuple from djangorestframework.utils import as_tuple
from djangorestframework.response import ResponseException from djangorestframework.response import ResponseException
from djangorestframework.parsers import FormParser, MultipartParser
from djangorestframework import status from djangorestframework import status
#from djangorestframework.requestparsing import parse, load_parser #from djangorestframework.requestparsing import parse, load_parser
@ -151,11 +152,18 @@ class RequestMixin(object):
if not self.USE_FORM_OVERLOADING or self.method != 'POST' or not self.content_type.is_form(): if not self.USE_FORM_OVERLOADING or self.method != 'POST' or not self.content_type.is_form():
return return
# Temporarily switch to using the form parsers, then parse the content
parsers = self.parsers
self.parsers = (FormParser, MultipartParser)
content = self.RAW_CONTENT content = self.RAW_CONTENT
self.parsers = parsers
# Method overloading - change the method and remove the param from the content
if self.METHOD_PARAM in content: if self.METHOD_PARAM in content:
self.method = content[self.METHOD_PARAM].upper() self.method = content[self.METHOD_PARAM].upper()
del self._raw_content[self.METHOD_PARAM] del self._raw_content[self.METHOD_PARAM]
# Content overloading - rewind the stream and modify the content type
if self.CONTENT_PARAM in content and self.CONTENTTYPE_PARAM in content: if self.CONTENT_PARAM in content and self.CONTENTTYPE_PARAM in content:
self._content_type = MediaType(content[self.CONTENTTYPE_PARAM]) self._content_type = MediaType(content[self.CONTENTTYPE_PARAM])
self._stream = StringIO(content[self.CONTENT_PARAM]) self._stream = StringIO(content[self.CONTENT_PARAM])

View File

@ -67,25 +67,25 @@ class TestContentMixins(TestCase):
self.assertEqual(view.RAW_CONTENT, content) self.assertEqual(view.RAW_CONTENT, content)
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 view.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 form content.""" """Ensure view.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 view.RAW_CONTENT returns content for POST request with non-form content."""
self.ensure_determines_non_form_content_POST(RequestMixin()) 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 view.RAW_CONTENT returns content for PUT request with form content."""
self.ensure_determines_form_content_PUT(RequestMixin()) 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 view.RAW_CONTENT returns content for PUT request with non-form content."""
# self.ensure_determines_non_form_content_PUT(StandardContentMixin()) self.ensure_determines_non_form_content_PUT(RequestMixin())
#
# # OverloadedContentMixin behavioural tests # # OverloadedContentMixin behavioural tests
# #
# def test_overloaded_behaviour_determines_no_content_GET(self): # def test_overloaded_behaviour_determines_no_content_GET(self):
@ -108,16 +108,18 @@ class TestContentMixins(TestCase):
# """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(OverloadedContentMixin()) # self.ensure_determines_non_form_content_PUT(OverloadedContentMixin())
# #
# def test_overloaded_behaviour_allows_content_tunnelling(self): def test_overloaded_behaviour_allows_content_tunnelling(self):
# """Ensure determine_content(request) returns (content type, content) for overloaded POST request""" """Ensure request.RAW_CONTENT returns content for overloaded POST request"""
# content = 'qwerty' content = 'qwerty'
# content_type = 'text/plain' content_type = 'text/plain'
# form_data = {OverloadedContentMixin.CONTENT_PARAM: content, view = RequestMixin()
# OverloadedContentMixin.CONTENTTYPE_PARAM: content_type} form_data = {view.CONTENT_PARAM: content,
# request = self.req.post('/', form_data) view.CONTENTTYPE_PARAM: content_type}
# self.assertEqual(OverloadedContentMixin().determine_content(request), (content_type, content)) view.request = self.req.post('/', form_data)
# self.assertEqual(request.META['CONTENT_TYPE'], content_type) view.parsers = (PlainTextParser,)
# view.perform_form_overloading()
self.assertEqual(view.RAW_CONTENT, content)
# def test_overloaded_behaviour_allows_content_tunnelling_content_type_not_set(self): # def test_overloaded_behaviour_allows_content_tunnelling_content_type_not_set(self):
# """Ensure determine_content(request) returns (None, content) for overloaded POST request with content type not set""" # """Ensure determine_content(request) returns (None, content) for overloaded POST request with content type not set"""
# content = 'qwerty' # content = 'qwerty'