Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

163 строки
6.5KB

  1. import os
  2. import unittest
  3. import six
  4. if six.PY3:
  5. from unittest import mock
  6. else:
  7. import mock
  8. import engineio
  9. class TestWSGIApp(unittest.TestCase):
  10. def test_wsgi_routing(self):
  11. mock_wsgi_app = mock.MagicMock()
  12. mock_eio_app = 'foo'
  13. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
  14. environ = {'PATH_INFO': '/foo'}
  15. start_response = "foo"
  16. m(environ, start_response)
  17. mock_wsgi_app.assert_called_once_with(environ, start_response)
  18. def test_eio_routing(self):
  19. mock_wsgi_app = 'foo'
  20. mock_eio_app = mock.Mock()
  21. mock_eio_app.handle_request = mock.MagicMock()
  22. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
  23. environ = {'PATH_INFO': '/engine.io/'}
  24. start_response = "foo"
  25. m(environ, start_response)
  26. mock_eio_app.handle_request.assert_called_once_with(environ,
  27. start_response)
  28. def test_static_files(self):
  29. root_dir = os.path.dirname(__file__)
  30. m = engineio.WSGIApp('foo', None, static_files={
  31. '/': root_dir + '/index.html',
  32. '/foo': {'content_type': 'text/plain',
  33. 'filename': root_dir + '/index.html'},
  34. '/static': root_dir,
  35. '/static/test/': root_dir + '/',
  36. })
  37. def check_path(path, status_code, content_type, body):
  38. environ = {'PATH_INFO': path}
  39. start_response = mock.MagicMock()
  40. r = m(environ, start_response)
  41. self.assertEqual(r, [body.encode('utf-8')])
  42. start_response.assert_called_once_with(
  43. status_code, [('Content-Type', content_type)])
  44. check_path('/', '200 OK', 'text/html', '<html></html>\n')
  45. check_path('/foo', '200 OK', 'text/plain', '<html></html>\n')
  46. check_path('/static/index.html', '200 OK', 'text/html',
  47. '<html></html>\n')
  48. check_path('/static/foo.bar', '404 Not Found', 'text/plain',
  49. 'Not Found')
  50. check_path('/static/test/index.html', '200 OK', 'text/html',
  51. '<html></html>\n')
  52. check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')
  53. check_path('/bar/foo', '404 Not Found', 'text/plain', 'Not Found')
  54. check_path('', '404 Not Found', 'text/plain', 'Not Found')
  55. m.static_files[''] = 'index.html'
  56. check_path('/static/test/', '200 OK', 'text/html',
  57. '<html></html>\n')
  58. m.static_files[''] = {'filename': 'index.html'}
  59. check_path('/static/test/', '200 OK', 'text/html',
  60. '<html></html>\n')
  61. m.static_files[''] = {'filename': 'index.html',
  62. 'content_type': 'image/gif'}
  63. check_path('/static/test/', '200 OK', 'image/gif',
  64. '<html></html>\n')
  65. m.static_files[''] = {'filename': 'test.gif'}
  66. check_path('/static/test/', '404 Not Found', 'text/plain',
  67. 'Not Found')
  68. m.static_files = {}
  69. check_path('/static/test/index.html', '404 Not Found', 'text/plain',
  70. 'Not Found')
  71. def test_404(self):
  72. mock_wsgi_app = None
  73. mock_eio_app = mock.Mock()
  74. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
  75. environ = {'PATH_INFO': '/foo/bar'}
  76. start_response = mock.MagicMock()
  77. r = m(environ, start_response)
  78. self.assertEqual(r, [b'Not Found'])
  79. start_response.assert_called_once_with(
  80. "404 Not Found", [('Content-Type', 'text/plain')])
  81. def test_custom_eio_path(self):
  82. mock_wsgi_app = None
  83. mock_eio_app = mock.Mock()
  84. mock_eio_app.handle_request = mock.MagicMock()
  85. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='foo')
  86. environ = {'PATH_INFO': '/engine.io/'}
  87. start_response = mock.MagicMock()
  88. r = m(environ, start_response)
  89. self.assertEqual(r, [b'Not Found'])
  90. start_response.assert_called_once_with(
  91. "404 Not Found", [('Content-Type', 'text/plain')])
  92. environ = {'PATH_INFO': '/foo/'}
  93. m(environ, start_response)
  94. mock_eio_app.handle_request.assert_called_once_with(environ,
  95. start_response)
  96. def test_custom_eio_path_slashes(self):
  97. mock_wsgi_app = None
  98. mock_eio_app = mock.Mock()
  99. mock_eio_app.handle_request = mock.MagicMock()
  100. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app,
  101. engineio_path='/foo/')
  102. environ = {'PATH_INFO': '/foo/'}
  103. start_response = mock.MagicMock()
  104. m(environ, start_response)
  105. mock_eio_app.handle_request.assert_called_once_with(environ,
  106. start_response)
  107. def test_custom_eio_path_leading_slash(self):
  108. mock_wsgi_app = None
  109. mock_eio_app = mock.Mock()
  110. mock_eio_app.handle_request = mock.MagicMock()
  111. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='/foo')
  112. environ = {'PATH_INFO': '/foo/'}
  113. start_response = mock.MagicMock()
  114. m(environ, start_response)
  115. mock_eio_app.handle_request.assert_called_once_with(environ,
  116. start_response)
  117. def test_custom_eio_path_trailing_slash(self):
  118. mock_wsgi_app = None
  119. mock_eio_app = mock.Mock()
  120. mock_eio_app.handle_request = mock.MagicMock()
  121. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='foo/')
  122. environ = {'PATH_INFO': '/foo/'}
  123. start_response = mock.MagicMock()
  124. m(environ, start_response)
  125. mock_eio_app.handle_request.assert_called_once_with(environ,
  126. start_response)
  127. def test_gunicorn_socket(self):
  128. mock_wsgi_app = None
  129. mock_eio_app = mock.Mock()
  130. m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
  131. environ = {'gunicorn.socket': 123, 'PATH_INFO': '/foo/bar'}
  132. start_response = mock.MagicMock()
  133. m(environ, start_response)
  134. self.assertIn('eventlet.input', environ)
  135. self.assertEqual(environ['eventlet.input'].get_socket(), 123)
  136. def test_legacy_middleware_class(self):
  137. m = engineio.Middleware('eio', 'wsgi', 'eio_path')
  138. self.assertEqual(m.engineio_app, 'eio')
  139. self.assertEqual(m.wsgi_app, 'wsgi')
  140. self.assertEqual(m.static_files, {})
  141. self.assertEqual(m.engineio_path, 'eio_path')