Skip to content

Commit

Permalink
Version 0.3.2
Browse files Browse the repository at this point in the history
Added fix for unicode files name for nginx and xsendfile backends.

Fixes #11
  • Loading branch information
johnsensible committed Mar 25, 2013
1 parent e5d3032 commit 1b2eca1
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion sendfile/__init__.py
@@ -1,4 +1,4 @@
VERSION = (0, 3, 1)
VERSION = (0, 3, 2)
__version__ = '.'.join(map(str, VERSION))

import os.path
Expand Down
3 changes: 2 additions & 1 deletion sendfile/backends/nginx.py
Expand Up @@ -4,6 +4,7 @@

def sendfile(request, filename, **kwargs):
response = HttpResponse()
response['X-Accel-Redirect'] = _convert_file_to_url(filename)
url = _convert_file_to_url(filename)
response['X-Accel-Redirect'] = url.encode('utf-8')

return response
2 changes: 1 addition & 1 deletion sendfile/backends/xsendfile.py
Expand Up @@ -2,7 +2,7 @@

def sendfile(request, filename, **kwargs):
response = HttpResponse()
response['X-Sendfile'] = filename
response['X-Sendfile'] = unicode(filename).encode('utf-8')

return response

Empty file removed sendfile/testfile.txt
Empty file.
71 changes: 55 additions & 16 deletions sendfile/tests.py
@@ -1,80 +1,119 @@
# coding=utf-8

from django.conf import settings
from django.test import TestCase
from django.http import HttpResponse, Http404, HttpRequest
import os.path
from tempfile import mkdtemp
import shutil
from sendfile import sendfile as real_sendfile, _get_sendfile


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)


def _get_readme():
return os.path.join(os.path.dirname(__file__), 'testfile.txt')
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(TestCase):

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(), _get_readme())
response = real_sendfile(HttpRequest(), self._get_readme())
self.assertTrue(response is not None)
self.assertEqual('text/plain', response['Content-Type'])
self.assertEqual(_get_readme(), response.content)
self.assertEqual(self._get_readme(), response.content)

def test_set_mimetype(self):
response = real_sendfile(HttpRequest(), _get_readme(), mimetype='text/plain')
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(), _get_readme(), encoding='utf8')
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(), _get_readme(), attachment=True)
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(self):
response = real_sendfile(HttpRequest(), _get_readme(), attachment=True, attachment_filename='tests.txt')
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'])


class TestXSendfileBackend(TestCase):
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):
response = real_sendfile(HttpRequest(), _get_readme())
filepath = self.ensure_file('readme.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual(_get_readme(), response['X-Sendfile'])
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(filepath, response['X-Sendfile'].decode('utf-8'))

class TestNginxBackend(TestCase):

class TestNginxBackend(TempFileTestCase):

def setUp(self):
super(TestNginxBackend, self).setUp()
settings.SENDFILE_BACKEND = 'sendfile.backends.nginx'
settings.SENDFILE_ROOT = os.path.dirname(os.path.dirname(__file__))
settings.SENDFILE_ROOT = self.TEMP_FILE_ROOT
settings.SENDFILE_URL = '/private'
_get_sendfile.clear()

def test_correct_url_in_xaccelredirect_header(self):
response = real_sendfile(HttpRequest(), _get_readme())
filepath = self.ensure_file('readme.txt')
response = real_sendfile(HttpRequest(), filepath)
self.assertTrue(response is not None)
self.assertEqual('/private/sendfile/testfile.txt', response['X-Accel-Redirect'])
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', response['X-Accel-Redirect'].decode('utf-8'))
11 changes: 7 additions & 4 deletions setup.py
@@ -1,14 +1,17 @@
from distutils.core import setup

version=__import__('sendfile').__version__


setup(
name='django-sendfile',
version=__import__('sendfile').__version__,
version=version,
description='Abstraction to offload file uploads to web-server (e.g. Apache with mod_xsendfile) once Django has checked permissions etc.',
long_description=open('README.rst').read(),
author='John Montgomery',
author_email='john@sensibledevelopment.com',
url='http://github.com/johnsensible/django-sendfile',
download_url='http://github.com/johnsensible/django-sendfile/downloads',
url='https://github.com/johnsensible/django-sendfile',
download_url='https://github.com/johnsensible/django-sendfile/archive/v%s.zip#egg=django-sendfile-%s' % (version, version),
license='BSD',

requires=['Django (>=1.4.2)'],
Expand All @@ -33,4 +36,4 @@
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)
)

0 comments on commit 1b2eca1

Please sign in to comment.