Exemplo n.º 1
0
def test_do_dispatch():
    url_map = wu.UrlMap()
    url_map.expose('/foo/bar')(lambda r: ("Foo Bar", r))
    url_map.expose('/foo/<path:path_info>')(lambda r: ("Foo Bar Baz", r))
    url_map.expose('/foo/flibble/<path:path_info>')(
        lambda r: ("Foo Flibble Wibble Dibble", r))
    app = utils.MockObject(url_map=url_map)

    req = wz.Request.from_values(path="/foo/bar")
    req.map_adapter = url_map.bind_to_environ(req.environ)
    (res, r) = wu._do_dispatch(app, req)
    assert res == "Foo Bar"
    assert r.environ["SCRIPT_NAME"] == "/foo/bar"
    assert r.environ["PATH_INFO"] == ""

    req = wz.Request.from_values(path="/foo/bar/baz")
    req.map_adapter = url_map.bind_to_environ(req.environ)
    (res, r) = wu._do_dispatch(app, req)
    assert res == "Foo Bar Baz"
    assert r.environ["SCRIPT_NAME"] == "/foo"
    assert r.environ["PATH_INFO"] == "/bar/baz"

    req = wz.Request.from_values(path="/foo/flibble/wibble/dibble")
    req.map_adapter = url_map.bind_to_environ(req.environ)
    (res, r) = wu._do_dispatch(app, req)
    assert res == "Foo Flibble Wibble Dibble"
    assert r.environ["SCRIPT_NAME"] == "/foo/flibble"
    assert r.environ["PATH_INFO"] == "/wibble/dibble"
Exemplo n.º 2
0
def test_url_map_submount():
    url_map = wu.UrlMap()

    @url_map.expose_submount('/foo')
    class FooController(object):
        url_map = wu.UrlMap()

        @url_map.expose("/flibble")
        def flibble_view():
            return "Hullo"

    environ = wz.test.create_environ('/flibble')
    endpoint_name, kwargs = FooController.url_map.bind_to_environ(
        environ).match()
    endpoint = url_map.views[endpoint_name]
    result = endpoint(**kwargs)
    assert result == "Hullo"

    environ = wz.test.create_environ('/foo/flibble')
    endpoint_name, kwargs = url_map.bind_to_environ(environ).match()
    endpoint = url_map.views[endpoint_name]
    result = endpoint(**kwargs)
    assert result == "Hullo"

    @url_map.expose_submount('')
    class BarController(object):
        url_map = wu.UrlMap()

        @url_map.expose("/flibble")
        def flibble_view():
            return "Hullo"

    environ = wz.test.create_environ('/flibble')
    endpoint_name, kwargs = url_map.bind_to_environ(environ).match()
    endpoint = url_map.views[endpoint_name]
    result = endpoint(**kwargs)
    assert result == "Hullo"

    @url_map.expose_submount('/flamble')
    class FlambleController(object):
        url_map = wu.UrlMap()

        @url_map.expose("/flibble")
        def flibble_view():
            return "Hullo"

    @url_map.expose_submount('/flooble')
    class FloobleController(FlambleController):
        pass

    environ = wz.test.create_environ('/flooble/flibble')
    endpoint_name, kwargs = url_map.bind_to_environ(environ).match()
    endpoint = url_map.views[endpoint_name]
    result = endpoint(**kwargs)
    assert result == "Hullo"
Exemplo n.º 3
0
    class FooController(wu.Controller):
        url_map = wu.UrlMap()

        @url_map.expose(u"/")
        def index(request):
            assert request.foo
            return "This is the Index"

        @url_map.expose(u"/bar")
        def bar(request):
            assert request.foo
            return "This is the bar controller"
Exemplo n.º 4
0
def test_make_url_map():
    flibble_conv = lambda: "flibble"
    sub_url_map = wu.UrlMap([
        wz.routing.Rule('/', endpoint="."),
        wz.routing.Rule('/foo', endpoint="foo")
    ],
                            converters=dict(flibble=flibble_conv))
    url_map = wu.make_url_map("/blammo", sub_url_map)
    adapter = url_map.bind('example.com')
    assert adapter.match('/blammo/') == (".", {})
    assert adapter.match('/blammo/foo') == ("foo", {})
    assert url_map.converters['flibble'] == flibble_conv
Exemplo n.º 5
0
def test_UrlMap():
    url_map = wu.UrlMap()

    @url_map.expose(u"/test")
    def get(param1):
        u"This is the get function"
        return "GET " + param1

    assert get.__doc__ == u"This is the get function"
    assert get.__name__ == "get"

    @url_map.expose(u"/test", methods=[u"POST"])
    def post(param1):
        u"This is the post function"
        return "POST " + param1

    assert post.__doc__ == u"This is the post function"
    assert post.__name__ == "post"

    @url_map.expose(u"/test_both", methods=[u"GET", "POST"])
    def post_and_get(param1):
        u"This is the post_and_get function"
        return "GET POST " + param1

    assert post_and_get.__doc__ == u"This is the post_and_get function"
    assert post_and_get.__name__ == "post_and_get"

    lambda_func = url_map.expose('/test_lambda')(
        lambda request: "This is a lambda")
    assert lambda_func.__name__ == "<lambda>"

    class FooClass(object):
        def __call__(self, request):
            return "This is a callable object"

    url_map.expose(u"/test_callable", endpoint="callable")(FooClass())

    def check_url(_url, _method, _endpoint_name, _response):
        urls = url_map.bind_to_environ(utils.MockEnv(_url, _method))
        endpoint_name, kwargs = urls.match()
        if _endpoint_name:
            assert endpoint_name == _endpoint_name, u"Should have chosen the correct function"
        endpoint = url_map.views[endpoint_name]
        res = endpoint("p1", **kwargs)
        assert res == _response

    check_url(u"/test", u"GET", 'get', u"GET p1")
    check_url(u"/test", u"POST", 'post', u"POST p1")
    check_url(u"/test_both", u"GET", 'post_and_get', u"GET POST p1")
    check_url(u"/test_both", u"POST", 'post_and_get', u"GET POST p1")
    check_url(u"/test_lambda", u"GET", '', u"This is a lambda")
    check_url(u"/test_callable", u"GET", '', u"This is a callable object")
Exemplo n.º 6
0
def test_base_app():
    class TestRequest(wz.BaseRequest):
        def __init__(self, environ, **kwargs):
            super(self.__class__, self).__init__(environ)
            self.url = wz.Href("/submount")

    url_map = wu.UrlMap()
    url_map.expose('/')(lambda r: wz.Response("Index"))
    url_map.expose('/foo')(lambda r: wz.Response("Foo Page"))
    url_map.expose('/bar')(lambda r: ('bar', 'text/html', {
        'bar_var': "flumble"
    }))
    url_map.expose('/wrong')(lambda request: wz.redirect(request.url('/baz')))
    url_map.expose('/config')(
        lambda request: wz.Response(request.app.config['mountpoint']))

    env = wu.JinjaEnv(
        j2.Environment(loader=j2.DictLoader(dict(bar="Bar Page {{bar_var}}")),
                       extensions=['jinja2.ext.i18n']))

    app = wu.BaseApp(config=dict(mountpoint=u"/submount"),
                     url_map=url_map,
                     env=env)
    wsgi_app = wu.wsgi_wrapper(app, request_class=TestRequest)

    assert app.mountpoint() == u"/submount"
    assert app.mountpoint(u"bar") == u"/submount/bar"

    assert app.config['mountpoint'] == u"/submount"
    assert 'url' in app.env.globals

    for rule in app.url_map.iter_rules():
        assert rule.rule.startswith('/submount')

    environ = wz.test.create_environ('/submount/')
    response = wsgi_app(environ, lambda s, h: s)
    assert list(response) == ["Index"]

    environ = wz.test.create_environ('/submount')
    response = wsgi_app(environ, lambda s, h: s)
    response = list(response)[0]
    assert "Redirecting..." in response
    assert "/submount/" in response

    environ = wz.test.create_environ('/submount/foo')
    response = wsgi_app(environ, lambda s, h: s)
    assert list(response) == ["Foo Page"]

    environ = wz.test.create_environ('/submount/bar')
    response = wsgi_app(environ, lambda s, h: s)
    assert list(response) == ["Bar Page flumble"]

    environ = wz.test.create_environ('/submount/config')
    response = wsgi_app(environ, lambda s, h: s)
    assert list(response) == ["/submount"]

    environ = wz.test.create_environ('/submount/wrong')
    response = wsgi_app(environ, lambda s, h: s)
    response = list(response)[0]
    assert "Redirecting..." in response
    assert "/submount/baz" in response
Exemplo n.º 7
0
 class BarHandler(wu.Handler):
     url_map = wu.UrlMap()
     foo = FooHandler
Exemplo n.º 8
0
    class FlambleController(object):
        url_map = wu.UrlMap()

        @url_map.expose("/flibble")
        def flibble_view():
            return "Hullo"
Exemplo n.º 9
0
def test_base_app_minimal():
    url_map = wu.UrlMap()
    env = j2.Environment()
    application = wu.BaseApp(config=dict(), url_map=url_map, env=env)