| 1 | from unittest import TestCase |
| 2 | from StringIO import StringIO |
| 3 | |
| 4 | from django.core.servers.basehttp import ServerHandler |
| 5 | |
| 6 | |
| 7 | class DummyHandler(object): |
| 8 | |
| 9 | def log_request(*args, **kwargs): |
| 10 | pass |
| 11 | |
| 12 | |
| 13 | class FileWrapperHandler(ServerHandler): |
| 14 | |
| 15 | def __init__(self, *args, **kwargs): |
| 16 | ServerHandler.__init__(self, *args, **kwargs) |
| 17 | self.request_handler = DummyHandler() |
| 18 | self._used_sendfile = False |
| 19 | |
| 20 | def sendfile(self): |
| 21 | self._used_sendfile = True |
| 22 | return True |
| 23 | |
| 24 | |
| 25 | def hello_file(environ, start_response): |
| 26 | start_response('200 OK', [('Content-Type', 'text/plain')]) |
| 27 | return environ['wsgi.file_wrapper'](StringIO('foo')) |
| 28 | |
| 29 | |
| 30 | def hello_world(environ, start_response): |
| 31 | start_response('200 OK', [('Content-Type', 'text/plain')]) |
| 32 | return ['Hello World!'] |
| 33 | |
| 34 | |
| 35 | class Bug9659Test(TestCase): |
| 36 | """ |
| 37 | Test that the file wrapper works. |
| 38 | """ |
| 39 | |
| 40 | def test_bug_9659(self): |
| 41 | env = {'SERVER_PROTOCOL': 'HTTP/1.0'} |
| 42 | err = StringIO() |
| 43 | handler = FileWrapperHandler(None, StringIO(), err, env) |
| 44 | handler.run(hello_file) |
| 45 | self.assert_(handler._used_sendfile) |
| 46 | |
| 47 | handler = FileWrapperHandler(None, StringIO(), err, env) |
| 48 | handler.run(hello_world) |
| 49 | self.assert_(not handler._used_sendfile) |
| 50 | self.assertEqual(handler.stdout.getvalue().splitlines()[-1], |
| 51 | 'Hello World!') |