Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9659: fixed `wsgi.file_wrapper` in the builtin server. Thanks,…

… mitsuhiko.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10690 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bfdb7d26aa534578dbac3c43e76d8ac3a5ad6a06 1 parent 523da38
Jacob Kaplan-Moss authored May 07, 2009
13  django/core/servers/basehttp.py
@@ -306,14 +306,15 @@ def setup_environ(self):
306 306
             env.setdefault('SERVER_SOFTWARE',self.server_software)
307 307
 
308 308
     def finish_response(self):
309  
-        """Send any iterable data, then close self and the iterable
  309
+        """
  310
+        Send any iterable data, then close self and the iterable
310 311
 
311  
-        Subclasses intended for use in asynchronous servers will
312  
-        want to redefine this method, such that it sets up callbacks
313  
-        in the event loop to iterate over the data, and to call
314  
-        'self.close()' once the response is finished.
  312
+        Subclasses intended for use in asynchronous servers will want to
  313
+        redefine this method, such that it sets up callbacks in the event loop
  314
+        to iterate over the data, and to call 'self.close()' once the response
  315
+        is finished.
315 316
         """
316  
-        if not self.result_is_file() and not self.sendfile():
  317
+        if not self.result_is_file() or not self.sendfile():
317 318
             for data in self.result:
318 319
                 self.write(data)
319 320
             self.finish_content()
0  tests/regressiontests/builtin_server/__init__.py
No changes.
0  tests/regressiontests/builtin_server/models.py
No changes.
51  tests/regressiontests/builtin_server/tests.py
... ...
@@ -0,0 +1,51 @@
  1
+from unittest import TestCase
  2
+from StringIO import StringIO
  3
+from django.core.servers.basehttp import ServerHandler
  4
+
  5
+#
  6
+# Tests for #9659: wsgi.file_wrapper in the builtin server.
  7
+# We need to mock a couple of of handlers and keep track of what
  8
+# gets called when using a couple kinds of WSGI apps.
  9
+#
  10
+
  11
+class DummyHandler(object):
  12
+    def log_request(*args, **kwargs):
  13
+        pass
  14
+
  15
+class FileWrapperHandler(ServerHandler):
  16
+    def __init__(self, *args, **kwargs):
  17
+        ServerHandler.__init__(self, *args, **kwargs)
  18
+        self.request_handler = DummyHandler()
  19
+        self._used_sendfile = False
  20
+
  21
+    def sendfile(self):
  22
+        self._used_sendfile = True
  23
+        return True
  24
+
  25
+def wsgi_app(environ, start_response):
  26
+    start_response('200 OK', [('Content-Type', 'text/plain')])
  27
+    return ['Hello World!']
  28
+
  29
+def wsgi_app_file_wrapper(environ, start_response):
  30
+    start_response('200 OK', [('Content-Type', 'text/plain')])
  31
+    return environ['wsgi.file_wrapper'](StringIO('foo'))
  32
+
  33
+class WSGIFileWrapperTests(TestCase):
  34
+    """
  35
+    Test that the wsgi.file_wrapper works for the builting server.
  36
+    """
  37
+
  38
+    def test_file_wrapper_uses_sendfile(self):
  39
+        env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
  40
+        err = StringIO()
  41
+        handler = FileWrapperHandler(None, StringIO(), err, env)
  42
+        handler.run(wsgi_app_file_wrapper)
  43
+        self.assert_(handler._used_sendfile)
  44
+
  45
+    def test_file_wrapper_no_sendfile(self):
  46
+        env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
  47
+        err = StringIO()
  48
+        handler = FileWrapperHandler(None, StringIO(), err, env)
  49
+        handler.run(wsgi_app)
  50
+        self.failIf(handler._used_sendfile)
  51
+        self.assertEqual(handler.stdout.getvalue().splitlines()[-1],'Hello World!')

0 notes on commit bfdb7d2

Please sign in to comment.
Something went wrong with that request. Please try again.