2014-09-06 02:33:47 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-04-14 15:02:52 +04:00
|
|
|
|
2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-09-20 16:06:27 +04:00
|
|
|
from django import forms
|
2013-05-02 23:39:08 +04:00
|
|
|
from django.core.files.uploadhandler import MemoryFileUploadHandler
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.test import TestCase
|
2014-11-29 21:50:51 +03:00
|
|
|
from rest_framework.compat import StringIO
|
2013-05-02 23:39:08 +04:00
|
|
|
from rest_framework.parsers import FormParser, FileUploadParser
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Form(forms.Form):
|
|
|
|
field1 = forms.CharField(max_length=3)
|
|
|
|
field2 = forms.CharField()
|
|
|
|
|
|
|
|
|
|
|
|
class TestFormParser(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.string = "field1=abc&field2=defghijk"
|
|
|
|
|
|
|
|
def test_parse(self):
|
|
|
|
""" Make sure the `QueryDict` works OK """
|
|
|
|
parser = FormParser()
|
|
|
|
|
|
|
|
stream = StringIO(self.string)
|
|
|
|
data = parser.parse(stream)
|
|
|
|
|
|
|
|
self.assertEqual(Form(data).is_valid(), True)
|
|
|
|
|
|
|
|
|
2013-05-02 23:39:08 +04:00
|
|
|
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 = {
|
2014-04-14 12:28:41 +04:00
|
|
|
'HTTP_CONTENT_DISPOSITION': 'Content-Disposition: inline; filename=file.txt',
|
2013-05-02 23:39:08 +04:00
|
|
|
'HTTP_CONTENT_LENGTH': 14,
|
|
|
|
}
|
2013-05-04 12:58:21 +04:00
|
|
|
self.parser_context = {'request': request, 'kwargs': {}}
|
2013-05-02 23:39:08 +04:00
|
|
|
|
|
|
|
def test_parse(self):
|
|
|
|
""" Make sure the `QueryDict` works OK """
|
|
|
|
parser = FileUploadParser()
|
2013-05-04 12:58:21 +04:00
|
|
|
self.stream.seek(0)
|
|
|
|
data_and_files = parser.parse(self.stream, None, self.parser_context)
|
2013-05-02 23:39:08 +04:00
|
|
|
file_obj = data_and_files.files['file']
|
|
|
|
self.assertEqual(file_obj._size, 14)
|
2013-05-04 12:58:21 +04:00
|
|
|
|
|
|
|
def test_get_filename(self):
|
|
|
|
parser = FileUploadParser()
|
|
|
|
filename = parser.get_filename(self.stream, None, self.parser_context)
|
2014-04-14 13:04:18 +04:00
|
|
|
self.assertEqual(filename, 'file.txt')
|
2014-04-14 14:25:30 +04:00
|
|
|
|
|
|
|
def test_get_encoded_filename(self):
|
|
|
|
parser = FileUploadParser()
|
|
|
|
|
|
|
|
self.__replace_content_disposition('inline; filename*=utf-8\'\'ÀĥƦ.txt')
|
|
|
|
filename = parser.get_filename(self.stream, None, self.parser_context)
|
|
|
|
self.assertEqual(filename, 'ÀĥƦ.txt')
|
|
|
|
|
|
|
|
self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8\'\'ÀĥƦ.txt')
|
|
|
|
filename = parser.get_filename(self.stream, None, self.parser_context)
|
|
|
|
self.assertEqual(filename, 'ÀĥƦ.txt')
|
|
|
|
|
2014-09-06 02:22:43 +04:00
|
|
|
self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8\'en-us\'ÀĥƦ.txt')
|
|
|
|
filename = parser.get_filename(self.stream, None, self.parser_context)
|
|
|
|
self.assertEqual(filename, 'ÀĥƦ.txt')
|
|
|
|
|
2014-04-14 14:25:30 +04:00
|
|
|
self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8--ÀĥƦ.txt')
|
|
|
|
filename = parser.get_filename(self.stream, None, self.parser_context)
|
|
|
|
self.assertEqual(filename, 'fallback.txt')
|
|
|
|
|
|
|
|
def __replace_content_disposition(self, disposition):
|
|
|
|
self.parser_context['request'].META['HTTP_CONTENT_DISPOSITION'] = disposition
|