tests.py 1.7 KB

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