mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Tests for FileUploadParser
This commit is contained in:
parent
0c85768435
commit
318fdaabe5
|
@ -1,10 +1,11 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from rest_framework.compat import StringIO
|
from rest_framework.compat import StringIO
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.core.files.uploadhandler import MemoryFileUploadHandler
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils import unittest
|
from django.utils import unittest
|
||||||
from rest_framework.compat import etree
|
from rest_framework.compat import etree
|
||||||
from rest_framework.parsers import FormParser
|
from rest_framework.parsers import FormParser, FileUploadParser
|
||||||
from rest_framework.parsers import XMLParser
|
from rest_framework.parsers import XMLParser
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
@ -82,3 +83,27 @@ class TestXMLParser(TestCase):
|
||||||
parser = XMLParser()
|
parser = XMLParser()
|
||||||
data = parser.parse(self._complex_data_input)
|
data = parser.parse(self._complex_data_input)
|
||||||
self.assertEqual(data, self._complex_data)
|
self.assertEqual(data, self._complex_data)
|
||||||
|
|
||||||
|
|
||||||
|
class TestFileUploadParser(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
class MockRequest(object):
|
||||||
|
pass
|
||||||
|
from io import BytesIO
|
||||||
|
self.stream = BytesIO(
|
||||||
|
"Test text file".encode('utf-8')
|
||||||
|
)
|
||||||
|
request = MockRequest()
|
||||||
|
request.upload_handlers = (MemoryFileUploadHandler(),)
|
||||||
|
request.META = {
|
||||||
|
'HTTP_CONTENT_DISPOSITION': 'Content-Disposition: inline; filename=file.txt'.encode('utf-8'),
|
||||||
|
'HTTP_CONTENT_LENGTH': 14,
|
||||||
|
}
|
||||||
|
self.parser_context = {'request': request}
|
||||||
|
|
||||||
|
def test_parse(self):
|
||||||
|
""" Make sure the `QueryDict` works OK """
|
||||||
|
parser = FileUploadParser()
|
||||||
|
data_and_files = parser.parse(self.stream, parser_context=self.parser_context)
|
||||||
|
file_obj = data_and_files.files['file']
|
||||||
|
self.assertEqual(file_obj._size, 14)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user