2012-08-16 19:01:16 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
2009-05-07 23:39:06 +08:00
|
|
|
from django.core.servers.basehttp import ServerHandler
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils.unittest import TestCase
|
2009-05-07 23:39:06 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Tests for #9659: wsgi.file_wrapper in the builtin server.
|
2013-01-29 23:45:40 +08:00
|
|
|
# We need to mock a couple of handlers and keep track of what
|
2009-05-07 23:39:06 +08:00
|
|
|
# gets called when using a couple kinds of WSGI apps.
|
|
|
|
#
|
|
|
|
|
|
|
|
class DummyHandler(object):
|
|
|
|
def log_request(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FileWrapperHandler(ServerHandler):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
ServerHandler.__init__(self, *args, **kwargs)
|
|
|
|
self.request_handler = DummyHandler()
|
|
|
|
self._used_sendfile = False
|
|
|
|
|
|
|
|
def sendfile(self):
|
|
|
|
self._used_sendfile = True
|
|
|
|
return True
|
|
|
|
|
|
|
|
def wsgi_app(environ, start_response):
|
2012-08-16 19:01:16 +08:00
|
|
|
start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
|
|
|
|
return [b'Hello World!']
|
2009-05-07 23:39:06 +08:00
|
|
|
|
|
|
|
def wsgi_app_file_wrapper(environ, start_response):
|
2012-08-16 19:01:16 +08:00
|
|
|
start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
|
|
|
|
return environ['wsgi.file_wrapper'](BytesIO(b'foo'))
|
2009-05-07 23:39:06 +08:00
|
|
|
|
|
|
|
class WSGIFileWrapperTests(TestCase):
|
|
|
|
"""
|
|
|
|
Test that the wsgi.file_wrapper works for the builting server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_file_wrapper_uses_sendfile(self):
|
|
|
|
env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
|
2012-08-16 19:01:16 +08:00
|
|
|
handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
|
2009-05-07 23:39:06 +08:00
|
|
|
handler.run(wsgi_app_file_wrapper)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(handler._used_sendfile)
|
2012-08-16 19:01:16 +08:00
|
|
|
self.assertEqual(handler.stdout.getvalue(), b'')
|
|
|
|
self.assertEqual(handler.stderr.getvalue(), b'')
|
2009-05-07 23:39:06 +08:00
|
|
|
|
|
|
|
def test_file_wrapper_no_sendfile(self):
|
|
|
|
env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
|
2012-08-16 19:01:16 +08:00
|
|
|
handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
|
2009-05-07 23:39:06 +08:00
|
|
|
handler.run(wsgi_app)
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(handler._used_sendfile)
|
2012-08-16 19:01:16 +08:00
|
|
|
self.assertEqual(handler.stdout.getvalue().splitlines()[-1], b'Hello World!')
|
|
|
|
self.assertEqual(handler.stderr.getvalue(), b'')
|