2
0

tests.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from __future__ import unicode_literals
  2. from io import BytesIO
  3. from unittest import TestCase
  4. from django.core.servers.basehttp import ServerHandler, MAX_SOCKET_CHUNK_SIZE
  5. class DummyHandler(object):
  6. def log_request(self, *args, **kwargs):
  7. pass
  8. class FileWrapperHandler(ServerHandler):
  9. def __init__(self, *args, **kwargs):
  10. super(FileWrapperHandler, self).__init__(*args, **kwargs)
  11. self.request_handler = DummyHandler()
  12. self._used_sendfile = False
  13. def sendfile(self):
  14. self._used_sendfile = True
  15. return True
  16. def wsgi_app(environ, start_response):
  17. start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
  18. return [b'Hello World!']
  19. def wsgi_app_file_wrapper(environ, start_response):
  20. start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
  21. return environ['wsgi.file_wrapper'](BytesIO(b'foo'))
  22. class WSGIFileWrapperTests(TestCase):
  23. """
  24. Test that the wsgi.file_wrapper works for the builting server.
  25. Tests for #9659: wsgi.file_wrapper in the builtin server.
  26. We need to mock a couple of handlers and keep track of what
  27. gets called when using a couple kinds of WSGI apps.
  28. """
  29. def test_file_wrapper_uses_sendfile(self):
  30. env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
  31. handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
  32. handler.run(wsgi_app_file_wrapper)
  33. self.assertTrue(handler._used_sendfile)
  34. self.assertEqual(handler.stdout.getvalue(), b'')
  35. self.assertEqual(handler.stderr.getvalue(), b'')
  36. def test_file_wrapper_no_sendfile(self):
  37. env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
  38. handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
  39. handler.run(wsgi_app)
  40. self.assertFalse(handler._used_sendfile)
  41. self.assertEqual(handler.stdout.getvalue().splitlines()[-1], b'Hello World!')
  42. self.assertEqual(handler.stderr.getvalue(), b'')
  43. class WriteChunkCounterHandler(ServerHandler):
  44. """
  45. Server handler that counts the number of chunks written after headers were
  46. sent. Used to make sure large response body chunking works properly.
  47. """
  48. def __init__(self, *args, **kwargs):
  49. super(WriteChunkCounterHandler, self).__init__(*args, **kwargs)
  50. self.request_handler = DummyHandler()
  51. self.headers_written = False
  52. self.write_chunk_counter = 0
  53. def send_headers(self):
  54. super(WriteChunkCounterHandler, self).send_headers()
  55. self.headers_written = True
  56. def _write(self, data):
  57. if self.headers_written:
  58. self.write_chunk_counter += 1
  59. self.stdout.write(data)
  60. def send_big_data_app(environ, start_response):
  61. start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
  62. # Return a blob of data that is 1.5 times the maximum chunk size.
  63. return [b'x' * (MAX_SOCKET_CHUNK_SIZE + MAX_SOCKET_CHUNK_SIZE // 2)]
  64. class ServerHandlerChunksProperly(TestCase):
  65. """
  66. Test that the ServerHandler chunks data properly.
  67. Tests for #18972: The logic that performs the math to break data into
  68. 32MB (MAX_SOCKET_CHUNK_SIZE) chunks was flawed, BUT it didn't actually
  69. cause any problems.
  70. """
  71. def test_chunked_data(self):
  72. env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
  73. handler = WriteChunkCounterHandler(None, BytesIO(), BytesIO(), env)
  74. handler.run(send_big_data_app)
  75. self.assertEqual(handler.write_chunk_counter, 2)