diff options
| author | tom christie tom@tomchristie.com | 2011-04-02 16:32:37 +0100 | 
|---|---|---|
| committer | tom christie tom@tomchristie.com | 2011-04-02 16:32:37 +0100 | 
| commit | 4687db680cda52e9836743940e4cf7279b307294 (patch) | |
| tree | 23b9b22eee3c08f6de09295b3c6630f5fb0730fa /djangorestframework/tests/parsers.py | |
| parent | 8845b281fe9aafbc9f9b2a283fafbde9787f4734 (diff) | |
| download | django-rest-framework-4687db680cda52e9836743940e4cf7279b307294.tar.bz2 | |
Refactor to use self.CONTENT to access request body.  Get file upload working
Diffstat (limited to 'djangorestframework/tests/parsers.py')
| -rw-r--r-- | djangorestframework/tests/parsers.py | 23 | 
1 files changed, 13 insertions, 10 deletions
| diff --git a/djangorestframework/tests/parsers.py b/djangorestframework/tests/parsers.py index d4cd1e87..4753f6f3 100644 --- a/djangorestframework/tests/parsers.py +++ b/djangorestframework/tests/parsers.py @@ -1,12 +1,13 @@  """  ..      >>> from djangorestframework.parsers import FormParser -    >>> from djangorestframework.resource import Resource      >>> from djangorestframework.compat import RequestFactory +    >>> from djangorestframework.resource import Resource +    >>> from StringIO import StringIO      >>> from urllib import urlencode      >>> req = RequestFactory().get('/')      >>> some_resource = Resource() -    >>> trash = some_resource.dispatch(req)# Some variables are set only when calling dispatch +    >>> some_resource.request = req  # Make as if this request had been dispatched  FormParser  ============ @@ -23,7 +24,7 @@ Here is some example data, which would eventually be sent along with a post requ  Default behaviour for :class:`parsers.FormParser`, is to return a single value for each parameter : -    >>> FormParser(some_resource).parse(inpt) == {'key1': 'bla1', 'key2': 'blo1'} +    >>> FormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'bla1', 'key2': 'blo1'}      True  However, you can customize this behaviour by subclassing :class:`parsers.FormParser`, and overriding :meth:`parsers.FormParser.is_a_list` : @@ -35,7 +36,7 @@ However, you can customize this behaviour by subclassing :class:`parsers.FormPar  This new parser only flattens the lists of parameters that contain a single value. -    >>> MyFormParser(some_resource).parse(inpt) == {'key1': 'bla1', 'key2': ['blo1', 'blo2']} +    >>> MyFormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'bla1', 'key2': ['blo1', 'blo2']}      True  .. note:: The same functionality is available for :class:`parsers.MultipartParser`. @@ -60,7 +61,7 @@ The browsers usually strip the parameter completely. A hack to avoid this, and t  :class:`parsers.FormParser` strips the values ``_empty`` from all the lists. -    >>> MyFormParser(some_resource).parse(inpt) == {'key1': 'blo1'} +    >>> MyFormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'blo1'}      True  Oh ... but wait a second, the parameter ``key2`` isn't even supposed to be a list, so the parser just stripped it. @@ -70,7 +71,7 @@ Oh ... but wait a second, the parameter ``key2`` isn't even supposed to be a lis      ...     def is_a_list(self, key, val_list):      ...         return key == 'key2'      ...  -    >>> MyFormParser(some_resource).parse(inpt) == {'key1': 'blo1', 'key2': []} +    >>> MyFormParser(some_resource).parse(StringIO(inpt)) == {'key1': 'blo1', 'key2': []}      True  Better like that. Note that you can configure something else than ``_empty`` for the empty value by setting :attr:`parsers.FormParser.EMPTY_VALUE`. @@ -81,6 +82,8 @@ from django.test import TestCase  from djangorestframework.compat import RequestFactory  from djangorestframework.parsers import MultipartParser  from djangorestframework.resource import Resource +from djangorestframework.mediatypes import MediaType +from StringIO import StringIO  def encode_multipart_formdata(fields, files):      """For testing multipart parser. @@ -119,9 +122,9 @@ class TestMultipartParser(TestCase):      def test_multipartparser(self):          """Ensure that MultipartParser can parse multipart/form-data that contains a mix of several files and parameters."""          post_req = RequestFactory().post('/', self.body, content_type=self.content_type) -        some_resource = Resource() -        some_resource.dispatch(post_req) -        parsed = MultipartParser(some_resource).parse(self.body) +        resource = Resource() +        resource.request = post_req +        parsed = MultipartParser(resource).parse(StringIO(self.body))          self.assertEqual(parsed['key1'], 'val1') -        self.assertEqual(parsed['file1'].read(), 'blablabla') +        self.assertEqual(parsed.FILES['file1'].read(), 'blablabla') | 
