Exemplo n.º 1
0
    def test_startswith_strip_route(self):
        from wsgigo import AppRouter

        app1 = TestWsgiApp('First app')
        app2 = TestWsgiApp('Second app')

        router = AppRouter(app1)
        router.add_startswith(app2, '/foobar/', strip_startswith=True)

        environ = {'HTTP_HOST': 'localhost', 'PATH_INFO': '/foobar/cheese'}
        self.assertEqual(router.get_route_app(environ), app2)
        self.assertEqual(environ['PATH_INFO'], '/cheese')
Exemplo n.º 2
0
    def test_host_route(self):
        from wsgigo import AppRouter

        app1 = TestWsgiApp('First app')
        app2 = TestWsgiApp('Second app')
        app3 = TestWsgiApp('Third')

        router = AppRouter(app1)
        router.add_hostname(app2, 'test1.local')
        router.add_hostname(app3, 'test2.local')

        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'foobar',
                'PATH_INFO': '/'
            }), app1)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'test1.local',
                'PATH_INFO': '/'
            }), app2)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'test2.local',
                'PATH_INFO': '/'
            }), app3)
Exemplo n.º 3
0
    def test_custom_router(self):
        from wsgigo import AppRouter, Route

        class InternetExplorerRouter(Route):
            def claim(self, environ):
                user_agent = environ['HTTP_USER_AGENT']
                if 'Trident/7.0' in user_agent or 'MSIE' in user_agent:
                    return True

        internet_explorer_app = TestWsgiApp("<b>really simple webpage</b>")
        real_app = TestWsgiApp("<b>really ADVANCED webpage</b>")

        router = AppRouter(default_app=real_app)
        router.add_route(InternetExplorerRouter(internet_explorer_app))

        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST':
                'localhost',
                'PATH_INFO':
                '/',
                'HTTP_USER_AGENT':
                'Lynx/2.8.1pre.9 libwww-FM/2.14'
            }), real_app)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST':
                'localhost',
                'PATH_INFO':
                '/',
                'HTTP_USER_AGENT':
                'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'
            }), internet_explorer_app)
Exemplo n.º 4
0
    def test_startswith_route(self):
        from wsgigo import AppRouter

        app1 = TestWsgiApp('First app')
        app2 = TestWsgiApp('Second app')
        app3 = TestWsgiApp('Third')

        router = AppRouter(app1)
        router.add_startswith(app2, '/foobar/')
        router.add_startswith(app3, '/spam/')

        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/'
            }), app1)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/monkey/'
            }), app1)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/foobar/cheese'
            }), app2)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/foobar/'
            }), app2)
        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/spam/'
            }), app3)
Exemplo n.º 5
0
    return "These are your documentation files."


#####################################################################
# API Wsgi app
api_app = flask.Flask('api_app')


@api_app.route("/api/test")
def api_test():
    return flask.jsonify({'Hello': 'World'})


#####################################################################
# PUT IT ALL TOGETHER
app = AppRouter(default_app=main_website)
app.add_startswith(docs_app, '/docs/')
app.add_hostname(api_app, 'api.local')

if __name__ == '__main__':
    from wsgiref.simple_server import make_server

    httpd = make_server('', 8000, app)
    print(
        "Serving HTTP on port 8000...\n"
        "Add api.local to your /etc/hosts file pointing to 127.0.0.1 and try one of these URLs:\n"
        "    http://api.local:8000/api/test\n"
        "    http://localhost:8000/\n"
        "    http://localhost:8000/docs/\n"
        "ENJOY!\n\n")
Exemplo n.º 6
0
    def test_regexp_route(self):
        from wsgigo import AppRouter

        app1 = TestWsgiApp('First app')
        app2 = TestWsgiApp('Second app')
        app3 = TestWsgiApp('Third')

        router = AppRouter(app1)
        router.add_regexp(app2, '/(?:monkey|ape)/')
        router.add_regexp(app3, re.compile('/cheese/(\w+)/'))

        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/'
            }), app1)

        environ = {'HTTP_HOST': 'localhost', 'PATH_INFO': '/monkey/george/'}
        self.assertEqual(router.get_route_app(environ), app2)
        self.assertEqual(environ['PATH_INFO'], '/monkey/george/')

        environ = {'HTTP_HOST': 'localhost', 'PATH_INFO': '/ape/fred/'}
        self.assertEqual(router.get_route_app(environ), app2)
        self.assertEqual(environ['PATH_INFO'], '/ape/fred/')

        environ = {'HTTP_HOST': 'localhost', 'PATH_INFO': '/cheese/brie/'}
        self.assertEqual(router.get_route_app(environ), app3)
        self.assertEqual(environ['PATH_INFO'], '/brie')

        self.assertEqual(
            router.get_route_app({
                'HTTP_HOST': 'localhost',
                'PATH_INFO': '/cheese/cheddar'
            }), app1)