2011-04-11 16:13:25 +04:00
|
|
|
# TODO: finish off the refactoring
|
2011-04-11 14:21:41 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from djangorestframework.compat import RequestFactory
|
|
|
|
from djangorestframework.request import RequestMixin
|
2011-04-11 14:56:07 +04:00
|
|
|
from djangorestframework.parsers import FormParser, MultipartParser, PlainTextParser
|
2011-04-11 16:13:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestContentParsing(TestCase):
|
2011-04-11 14:21:41 +04:00
|
|
|
def setUp(self):
|
|
|
|
self.req = RequestFactory()
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# # Interface tests
|
|
|
|
#
|
|
|
|
# def test_content_mixin_interface(self):
|
|
|
|
# """Ensure the ContentMixin interface is as expected."""
|
|
|
|
# self.assertRaises(NotImplementedError, ContentMixin().determine_content, None)
|
|
|
|
#
|
|
|
|
# def test_standard_content_mixin_interface(self):
|
|
|
|
# """Ensure the OverloadedContentMixin interface is as expected."""
|
|
|
|
# self.assertTrue(issubclass(StandardContentMixin, ContentMixin))
|
|
|
|
# getattr(StandardContentMixin, 'determine_content')
|
|
|
|
#
|
|
|
|
# def test_overloaded_content_mixin_interface(self):
|
|
|
|
# """Ensure the OverloadedContentMixin interface is as expected."""
|
|
|
|
# self.assertTrue(issubclass(OverloadedContentMixin, ContentMixin))
|
|
|
|
# getattr(OverloadedContentMixin, 'CONTENT_PARAM')
|
|
|
|
# getattr(OverloadedContentMixin, 'CONTENTTYPE_PARAM')
|
|
|
|
# getattr(OverloadedContentMixin, 'determine_content')
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# # Common functionality to test with both StandardContentMixin and OverloadedContentMixin
|
|
|
|
#
|
2011-04-11 14:21:41 +04:00
|
|
|
def ensure_determines_no_content_GET(self, view):
|
2011-04-11 14:47:22 +04:00
|
|
|
"""Ensure view.RAW_CONTENT returns None for GET request with no content."""
|
2011-04-11 14:21:41 +04:00
|
|
|
view.request = self.req.get('/')
|
|
|
|
self.assertEqual(view.RAW_CONTENT, None)
|
2011-04-11 14:47:22 +04:00
|
|
|
|
|
|
|
def ensure_determines_form_content_POST(self, view):
|
2011-04-11 14:54:26 +04:00
|
|
|
"""Ensure view.RAW_CONTENT returns content for POST request with form content."""
|
2011-04-11 14:47:22 +04:00
|
|
|
form_data = {'qwerty': 'uiop'}
|
|
|
|
view.parsers = (FormParser, MultipartParser)
|
|
|
|
view.request = self.req.post('/', data=form_data)
|
|
|
|
self.assertEqual(view.RAW_CONTENT, form_data)
|
|
|
|
|
2011-04-11 14:56:07 +04:00
|
|
|
def ensure_determines_non_form_content_POST(self, view):
|
2011-04-11 14:54:26 +04:00
|
|
|
"""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)
|
2011-04-11 14:56:07 +04:00
|
|
|
self.assertEqual(view.RAW_CONTENT, content)
|
2011-04-11 14:54:26 +04:00
|
|
|
|
2011-04-11 14:56:07 +04:00
|
|
|
def ensure_determines_form_content_PUT(self, view):
|
2011-04-11 14:54:26 +04:00
|
|
|
"""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)
|
|
|
|
|
2011-04-11 14:56:07 +04:00
|
|
|
def ensure_determines_non_form_content_PUT(self, view):
|
2011-04-11 14:54:26 +04:00
|
|
|
"""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)
|
2011-04-11 14:56:07 +04:00
|
|
|
self.assertEqual(view.RAW_CONTENT, content)
|
2011-04-11 14:54:26 +04:00
|
|
|
|
2011-04-11 14:21:41 +04:00
|
|
|
def test_standard_behaviour_determines_no_content_GET(self):
|
2011-04-11 15:19:28 +04:00
|
|
|
"""Ensure view.RAW_CONTENT returns None for GET request with no content."""
|
2011-04-11 14:21:41 +04:00
|
|
|
self.ensure_determines_no_content_GET(RequestMixin())
|
2011-04-11 14:47:22 +04:00
|
|
|
|
|
|
|
def test_standard_behaviour_determines_form_content_POST(self):
|
2011-04-11 15:19:28 +04:00
|
|
|
"""Ensure view.RAW_CONTENT returns content for POST request with form content."""
|
2011-04-11 14:47:22 +04:00
|
|
|
self.ensure_determines_form_content_POST(RequestMixin())
|
2011-04-11 14:54:26 +04:00
|
|
|
|
|
|
|
def test_standard_behaviour_determines_non_form_content_POST(self):
|
2011-04-11 15:19:28 +04:00
|
|
|
"""Ensure view.RAW_CONTENT returns content for POST request with non-form content."""
|
2011-04-11 14:54:26 +04:00
|
|
|
self.ensure_determines_non_form_content_POST(RequestMixin())
|
|
|
|
|
|
|
|
def test_standard_behaviour_determines_form_content_PUT(self):
|
2011-04-11 15:19:28 +04:00
|
|
|
"""Ensure view.RAW_CONTENT returns content for PUT request with form content."""
|
2011-04-11 14:54:26 +04:00
|
|
|
self.ensure_determines_form_content_PUT(RequestMixin())
|
|
|
|
|
2011-04-11 15:19:28 +04:00
|
|
|
def test_standard_behaviour_determines_non_form_content_PUT(self):
|
|
|
|
"""Ensure view.RAW_CONTENT returns content for PUT request with non-form content."""
|
|
|
|
self.ensure_determines_non_form_content_PUT(RequestMixin())
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# # OverloadedContentMixin behavioural tests
|
|
|
|
#
|
|
|
|
# def test_overloaded_behaviour_determines_no_content_GET(self):
|
|
|
|
# """Ensure StandardContentMixin.determine_content(request) returns None for GET request with no content."""
|
|
|
|
# self.ensure_determines_no_content_GET(OverloadedContentMixin())
|
|
|
|
#
|
|
|
|
# def test_overloaded_behaviour_determines_form_content_POST(self):
|
|
|
|
# """Ensure StandardContentMixin.determine_content(request) returns content for POST request with content."""
|
|
|
|
# self.ensure_determines_form_content_POST(OverloadedContentMixin())
|
|
|
|
#
|
|
|
|
# def test_overloaded_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(OverloadedContentMixin())
|
|
|
|
#
|
|
|
|
# def test_overloaded_behaviour_determines_form_content_PUT(self):
|
|
|
|
# """Ensure StandardContentMixin.determine_content(request) returns content for PUT request with content."""
|
|
|
|
# self.ensure_determines_form_content_PUT(OverloadedContentMixin())
|
|
|
|
#
|
|
|
|
# def test_overloaded_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(OverloadedContentMixin())
|
|
|
|
#
|
2011-04-11 15:19:28 +04:00
|
|
|
def test_overloaded_behaviour_allows_content_tunnelling(self):
|
|
|
|
"""Ensure request.RAW_CONTENT returns content for overloaded POST request"""
|
|
|
|
content = 'qwerty'
|
|
|
|
content_type = 'text/plain'
|
|
|
|
view = RequestMixin()
|
|
|
|
form_data = {view.CONTENT_PARAM: content,
|
|
|
|
view.CONTENTTYPE_PARAM: content_type}
|
|
|
|
view.request = self.req.post('/', form_data)
|
|
|
|
view.parsers = (PlainTextParser,)
|
|
|
|
view.perform_form_overloading()
|
|
|
|
self.assertEqual(view.RAW_CONTENT, content)
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
# 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"""
|
|
|
|
# content = 'qwerty'
|
|
|
|
# request = self.req.post('/', {OverloadedContentMixin.CONTENT_PARAM: content})
|
|
|
|
# self.assertEqual(OverloadedContentMixin().determine_content(request), (None, content))
|