Ticket #9659: 9659-fixed-filewrapper.patch

File 9659-fixed-filewrapper.patch, 2.6 KB (added by Armin Ronacher, 15 years ago)
  • django/core/servers/basehttp.py

     
    313313        in the event loop to iterate over the data, and to call
    314314        'self.close()' once the response is finished.
    315315        """
    316         if not self.result_is_file() and not self.sendfile():
     316        if not self.result_is_file() or not self.sendfile():
    317317            for data in self.result:
    318318                self.write(data)
    319319            self.finish_content()
  • tests/regressiontests/bug9659/tests.py

    Property changes on: tests/regressiontests/bug9659
    ___________________________________________________________________
    Name: svn:ignore
       + *.pyc
    *.pyo
    
    
     
     1from unittest import TestCase
     2from StringIO import StringIO
     3
     4from django.core.servers.basehttp import ServerHandler
     5
     6
     7class DummyHandler(object):
     8
     9    def log_request(*args, **kwargs):
     10        pass
     11
     12
     13class 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
     25def hello_file(environ, start_response):
     26    start_response('200 OK', [('Content-Type', 'text/plain')])
     27    return environ['wsgi.file_wrapper'](StringIO('foo'))
     28
     29
     30def hello_world(environ, start_response):
     31    start_response('200 OK', [('Content-Type', 'text/plain')])
     32    return ['Hello World!']
     33
     34
     35class 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!')
Back to Top