Ticket #9659: 9659-fixed-filewrapper.2.patch

File 9659-fixed-filewrapper.2.patch, 1.9 KB (added by Armin Ronacher, 15 years ago)
  • regressiontests/basehttp_server/tests.py

     
     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