PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/regressiontests/builtin_server/tests.py

https://code.google.com/p/mango-py/
Python | 52 lines | 34 code | 10 blank | 8 comment | 0 complexity | 8d12e9e52754710648ad1d5dcde8d209 MD5 | raw file
 1from StringIO import StringIO
 2
 3from django.core.servers.basehttp import ServerHandler
 4from django.utils.unittest import TestCase
 5
 6#
 7# Tests for #9659: wsgi.file_wrapper in the builtin server.
 8# We need to mock a couple of of handlers and keep track of what
 9# gets called when using a couple kinds of WSGI apps.
10#
11
12class DummyHandler(object):
13    def log_request(*args, **kwargs):
14        pass
15
16class FileWrapperHandler(ServerHandler):
17    def __init__(self, *args, **kwargs):
18        ServerHandler.__init__(self, *args, **kwargs)
19        self.request_handler = DummyHandler()
20        self._used_sendfile = False
21
22    def sendfile(self):
23        self._used_sendfile = True
24        return True
25
26def wsgi_app(environ, start_response):
27    start_response('200 OK', [('Content-Type', 'text/plain')])
28    return ['Hello World!']
29
30def wsgi_app_file_wrapper(environ, start_response):
31    start_response('200 OK', [('Content-Type', 'text/plain')])
32    return environ['wsgi.file_wrapper'](StringIO('foo'))
33
34class WSGIFileWrapperTests(TestCase):
35    """
36    Test that the wsgi.file_wrapper works for the builting server.
37    """
38
39    def test_file_wrapper_uses_sendfile(self):
40        env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
41        err = StringIO()
42        handler = FileWrapperHandler(None, StringIO(), err, env)
43        handler.run(wsgi_app_file_wrapper)
44        self.assertTrue(handler._used_sendfile)
45
46    def test_file_wrapper_no_sendfile(self):
47        env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
48        err = StringIO()
49        handler = FileWrapperHandler(None, StringIO(), err, env)
50        handler.run(wsgi_app)
51        self.assertFalse(handler._used_sendfile)
52        self.assertEqual(handler.stdout.getvalue().splitlines()[-1],'Hello World!')