def test_wsgi_routing(self):
     mock_wsgi_app = mock.MagicMock()
     mock_eio_app = 'foo'
     m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
     environ = {'PATH_INFO': '/foo'}
     start_response = "foo"
     m(environ, start_response)
     mock_wsgi_app.assert_called_once_with(environ, start_response)
    def test_static_files(self):
        root_dir = os.path.dirname(__file__)
        m = engineio.WSGIApp(
            'foo',
            None,
            static_files={
                '/': root_dir + '/index.html',
                '/foo': {
                    'content_type': 'text/plain',
                    'filename': root_dir + '/index.html',
                },
                '/static': root_dir,
                '/static/test/': root_dir + '/',
            },
        )

        def check_path(path, status_code, content_type, body):
            environ = {'PATH_INFO': path}
            start_response = mock.MagicMock()
            r = m(environ, start_response)
            assert r == [body.encode('utf-8')]
            start_response.assert_called_once_with(
                status_code, [('Content-Type', content_type)])

        check_path('/', '200 OK', 'text/html', '<html></html>\n')
        check_path('/foo', '200 OK', 'text/plain', '<html></html>\n')
        check_path('/static/index.html', '200 OK', 'text/html',
                   '<html></html>\n')
        check_path('/static/foo.bar', '404 Not Found', 'text/plain',
                   'Not Found')
        check_path('/static/test/index.html', '200 OK', 'text/html',
                   '<html></html>\n')
        check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')
        check_path('/bar/foo', '404 Not Found', 'text/plain', 'Not Found')
        check_path('', '404 Not Found', 'text/plain', 'Not Found')

        m.static_files[''] = 'index.html'
        check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')

        m.static_files[''] = {'filename': 'index.html'}
        check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')

        m.static_files[''] = {
            'filename': 'index.html',
            'content_type': 'image/gif',
        }
        check_path('/static/test/', '200 OK', 'image/gif', '<html></html>\n')

        m.static_files[''] = {'filename': 'test.gif'}
        check_path('/static/test/', '404 Not Found', 'text/plain', 'Not Found')

        m.static_files = {}
        check_path(
            '/static/test/index.html',
            '404 Not Found',
            'text/plain',
            'Not Found',
        )
 def test_gunicorn_socket(self):
     mock_wsgi_app = None
     mock_eio_app = mock.Mock()
     m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
     environ = {'gunicorn.socket': 123, 'PATH_INFO': '/foo/bar'}
     start_response = mock.MagicMock()
     m(environ, start_response)
     assert 'eventlet.input' in environ
     assert environ['eventlet.input'].get_socket() == 123
 def test_404(self):
     mock_wsgi_app = None
     mock_eio_app = mock.Mock()
     m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
     environ = {'PATH_INFO': '/foo/bar'}
     start_response = mock.MagicMock()
     r = m(environ, start_response)
     assert r == [b'Not Found']
     start_response.assert_called_once_with(
         "404 Not Found", [('Content-Type', 'text/plain')])
 def test_custom_eio_path_trailing_slash(self):
     mock_wsgi_app = None
     mock_eio_app = mock.Mock()
     mock_eio_app.handle_request = mock.MagicMock()
     m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='foo/')
     environ = {'PATH_INFO': '/foo/'}
     start_response = mock.MagicMock()
     m(environ, start_response)
     mock_eio_app.handle_request.assert_called_once_with(
         environ, start_response)
 def test_static_files(self):
     root_dir = os.path.dirname(__file__)
     m = engineio.WSGIApp('foo', None, static_files={
         '/': {'content_type': 'text/html',
               'filename': root_dir + '/index.html'},
         '/foo': {'content_type': 'text/html',
                  'filename': root_dir + '/index.html'},
     })
     environ = {'PATH_INFO': '/'}
     start_response = mock.MagicMock()
     r = m(environ, start_response)
     self.assertEqual(r, [b'<html></html>\n'])
     start_response.assert_called_once_with(
         "200 OK", [('Content-Type', 'text/html')])
    def test_custom_eio_path(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        mock_eio_app.handle_request = mock.MagicMock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='foo')
        environ = {'PATH_INFO': '/engine.io/'}
        start_response = mock.MagicMock()
        r = m(environ, start_response)
        assert r == [b'Not Found']
        start_response.assert_called_once_with(
            "404 Not Found", [('Content-Type', 'text/plain')])

        environ = {'PATH_INFO': '/foo/'}
        m(environ, start_response)
        mock_eio_app.handle_request.assert_called_once_with(
            environ, start_response)
Beispiel #8
0
from flask import Flask, render_template

import engineio

# set async_mode to 'threading', 'eventlet' or 'gevent' to force a mode
# else, the best mode is selected automatically from what's installed
async_mode = None

eio = engineio.Server(async_mode=async_mode)
app = Flask(__name__)
app.wsgi_app = engineio.WSGIApp(eio, app.wsgi_app)


@app.route('/')
def index():
    return render_template('latency.html')


@eio.on('message')
def message(sid, data):
    eio.send(sid, 'pong', binary=False)


if __name__ == '__main__':
    if eio.async_mode == 'threading':
        # deploy with Werkzeug
        app.run(threaded=True)
    elif eio.async_mode == 'eventlet':
        # deploy with eventlet
        import eventlet
        from eventlet import wsgi