-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathtests.py
157 lines (121 loc) · 5.92 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# coding=utf-8
from django.conf import settings
from django.test import TestCase
from django.http import HttpResponse, Http404, HttpRequest
from django.utils.encoding import smart_str
import os.path
from tempfile import mkdtemp
import shutil
from sendfile import sendfile as real_sendfile, _get_sendfile
try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote
def sendfile(request, filename, **kwargs):
# just a simple response with the filename
# as content - so we can test without a backend active
return HttpResponse(filename)
class TempFileTestCase(TestCase):
def setUp(self):
super(TempFileTestCase, self).setUp()
self.TEMP_FILE_ROOT = mkdtemp()
def tearDown(self):
super(TempFileTestCase, self).tearDown()
if os.path.exists(self.TEMP_FILE_ROOT):
shutil.rmtree(self.TEMP_FILE_ROOT)
def ensure_file(self, filename):
path = os.path.join(self.TEMP_FILE_ROOT, filename)
if not os.path.exists(path):
open(path, 'w').close()
return path
class TestSendfile(TempFileTestCase):
def setUp(self):
super(TestSendfile, self).setUp()
# set ourselves to be the sendfile backend
settings.SENDFILE_BACKEND = 'sendfile.tests'
_get_sendfile.clear()
def _get_readme(self):
return self.ensure_file('testfile.txt')
def test_404(self):
try:
real_sendfile(HttpRequest(), 'fhdsjfhjk.txt')
except Http404:
pass
def test_sendfile(self):
response = real_sendfile(HttpRequest(), self._get_readme())
self.assertTrue(response is not None)
self.assertEqual('text/plain', response['Content-Type'])
self.assertEqual(self._get_readme(), smart_str(response.content))
def test_set_mimetype(self):
response = real_sendfile(HttpRequest(), self._get_readme(), mimetype='text/plain')
self.assertTrue(response is not None)
self.assertEqual('text/plain', response['Content-Type'])
def test_set_encoding(self):
response = real_sendfile(HttpRequest(), self._get_readme(), encoding='utf8')
self.assertTrue(response is not None)
self.assertEqual('utf8', response['Content-Encoding'])
def test_attachment(self):
response = real_sendfile(HttpRequest(), self._get_readme(), attachment=True)
self.assertTrue(response is not None)
self.assertEqual('attachment; filename="testfile.txt"', response['Content-Disposition'])
def test_attachment_filename_false(self):
response = real_sendfile(HttpRequest(), self._get_readme(), attachment=True, attachment_filename=False)
self.assertTrue(response is not None)
self.assertEqual('attachment', response['Content-Disposition'])
def test_attachment_filename(self):
response = real_sendfile(HttpRequest(), self._get_readme(), attachment=True, attachment_filename='tests.txt')
self.assertTrue(response is not None)
self.assertEqual('attachment; filename="tests.txt"', response['Content-Disposition'])
def test_attachment_filename_unicode(self):
response = real_sendfile(HttpRequest(), self._get_readme(), attachment=True, attachment_filename='test’s.txt')
self.assertTrue(response is not None)
self.assertEqual('attachment; filename="tests.txt"; filename*=UTF-8\'\'test%E2%80%99s.txt', response['Content-Disposition'])
class TestXSendfileBackend(TempFileTestCase):
def setUp(self):
super(TestXSendfileBackend, self).setUp()
settings.SENDFILE_BACKEND = 'sendfile.backends.xsendfile'
_get_sendfile.clear()
def test_correct_file_in_xsendfile_header(self):
filepath = self.ensure_file('readme.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(filepath, response['X-Sendfile'])
def test_xsendfile_header_containing_unicode(self):
filepath = self.ensure_file(u'péter_là_gueule.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(smart_str(filepath), response['X-Sendfile'])
class TestNginxBackend(TempFileTestCase):
def setUp(self):
super(TestNginxBackend, self).setUp()
settings.SENDFILE_BACKEND = 'sendfile.backends.nginx'
settings.SENDFILE_ROOT = self.TEMP_FILE_ROOT
settings.SENDFILE_URL = '/private'
_get_sendfile.clear()
def test_correct_url_in_xaccelredirect_header(self):
filepath = self.ensure_file('readme.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual('/private/readme.txt', response['X-Accel-Redirect'])
def test_xaccelredirect_header_containing_unicode(self):
filepath = self.ensure_file(u'péter_là_gueule.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(u'/private/péter_là_gueule.txt', unquote(response['X-Accel-Redirect']))
class TestModWsgiBackend(TempFileTestCase):
def setUp(self):
super(TestModWsgiBackend, self).setUp()
settings.SENDFILE_BACKEND = 'sendfile.backends.mod_wsgi'
settings.SENDFILE_ROOT = self.TEMP_FILE_ROOT
settings.SENDFILE_URL = '/private'
_get_sendfile.clear()
def test_correct_url_in_location_header(self):
filepath = self.ensure_file('readme.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual('/private/readme.txt', response['Location'])
def test_location_header_containing_unicode(self):
filepath = self.ensure_file(u'péter_là_gueule.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(u'/private/péter_là_gueule.txt', unquote(response['Location']))