Exemplo n.º 1
0
    def test_bindable(self):
        """
        L{bindable} is a decorator which allows a function decorated by @route
        to have a uniform signature regardless of whether it is receiving a
        bound object from its L{Klein} or not.
        """
        k = Klein()
        calls = []

        @k.route("/test")
        @bindable
        def method(*args):
            calls.append(args)
            return 7

        req = object()
        k.execute_endpoint("method", req)

        class BoundTo(object):
            app = k

        b = BoundTo()
        b.app.execute_endpoint("method", req)
        self.assertEquals(calls, [(None, req), (b, req)])
        self.assertEqual(originalName(method), "method")
Exemplo n.º 2
0
    def test_stackedRoute(self):
        """
        L{Klein.route} can be stacked to create multiple endpoints of
        a single function.
        """
        app = Klein()

        @app.route("/foo")
        @app.route("/bar", endpoint="bar")
        def foobar(request):
            return "foobar"

        self.assertEqual(len(app.endpoints), 2)

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo"), ("foobar", {}))
        self.assertEqual(app.execute_endpoint("foobar", DummyRequest(1)), "foobar")

        self.assertEqual(c.match("/bar"), ("bar", {}))
        self.assertEqual(app.execute_endpoint("bar", DummyRequest(2)), "foobar")
Exemplo n.º 3
0
    def test_stackedRoute(self):
        """
        L{Klein.route} can be stacked to create multiple endpoints of
        a single function.
        """
        app = Klein()

        @app.route("/foo")
        @app.route("/bar", endpoint="bar")
        def foobar(request):
            return "foobar"

        self.assertEqual(len(app.endpoints), 2)

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo"), ("foobar", {}))
        self.assertEqual(app.execute_endpoint("foobar", DummyRequest(1)), "foobar")

        self.assertEqual(c.match("/bar"), ("bar", {}))
        self.assertEqual(app.execute_endpoint("bar", DummyRequest(2)), "foobar")
Exemplo n.º 4
0
    def test_route(self):
        """
        L{Klein.route} adds functions as routable endpoints.
        """
        app = Klein()

        @app.route("/foo")
        def foo(request):
            return "foo"

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo"), ("foo", {}))
        self.assertEqual(len(app.endpoints), 1)

        self.assertEqual(app.execute_endpoint("foo", DummyRequest(1)), "foo")
Exemplo n.º 5
0
    def test_route(self):
        """
        L{Klein.route} adds functions as routable endpoints.
        """
        app = Klein()

        @app.route("/foo")
        def foo(request):
            return "foo"

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo"), ("foo", {}))
        self.assertEqual(len(app.endpoints), 1)

        self.assertEqual(app.execute_endpoint("foo", DummyRequest(1)), "foo")
Exemplo n.º 6
0
    def test_submountedRoute(self):
        """
        L{Klein.subroute} adds functions as routable endpoints.
        """
        app = Klein()

        with app.subroute("/sub") as app:

            @app.route("/prefixed_uri")
            def foo_endpoint(request):
                return b"foo"

        c = app.url_map.bind("sub/prefixed_uri")
        self.assertEqual(c.match("/sub/prefixed_uri"), ("foo_endpoint", {}))
        self.assertEqual(len(app.endpoints), 1)
        self.assertEqual(app.execute_endpoint("foo_endpoint", DummyRequest(1)),
                         b"foo")
Exemplo n.º 7
0
    def test_submountedRoute(self):
        """
        L{Klein.subroute} adds functions as routable endpoints.
        """
        app = Klein()

        with app.subroute("/sub") as app:
            @app.route("/prefixed_uri")
            def foo_endpoint(request):
                return b"foo"

        c = app.url_map.bind("sub/prefixed_uri")
        self.assertEqual(
            c.match("/sub/prefixed_uri"), ("foo_endpoint", {}))
        self.assertEqual(
            len(app.endpoints), 1)
        self.assertEqual(
            app.execute_endpoint("foo_endpoint", DummyRequest(1)), b"foo")
Exemplo n.º 8
0
    def test_copy(self):
        """
        L{Klein.__copy__} returns a new L{Klein} with all the registered endpoints
        """
        app = Klein()

        @app.route("/foo")
        def foo(request):
            return "foo"

        app_copy = copy.copy(app)

        @app.route('/bar')
        def bar(request):
            return 'bar'

        dr1 = DummyRequest(1)
        dr2 = DummyRequest(2)
        dr3 = DummyRequest(3)

        self.assertEquals(app.execute_endpoint('foo', dr1), 'foo')
        self.assertEquals(app.execute_endpoint('bar', dr2), 'bar')
        self.assertRaises(KeyError, app_copy.execute_endpoint, 'bar', dr3)
Exemplo n.º 9
0
    def test_copy(self):
        """
        L{Klein.__copy__} returns a new L{Klein} with all the registered endpoints
        """
        app = Klein()

        @app.route("/foo")
        def foo(request):
            return "foo"

        app_copy = copy.copy(app)

        @app.route('/bar')
        def bar(request):
            return 'bar'

        dr1 = DummyRequest(1)
        dr2 = DummyRequest(2)
        dr3 = DummyRequest(3)

        self.assertEquals(app.execute_endpoint('foo', dr1), 'foo')
        self.assertEquals(app.execute_endpoint('bar', dr2), 'bar')
        self.assertRaises(KeyError, app_copy.execute_endpoint, 'bar', dr3)
Exemplo n.º 10
0
    def test_branchRoute(self):
        """
        L{Klein.route} should create a branch path which consumes all children
        when the branch keyword argument is True.
        """
        app = Klein()

        @app.route("/foo/", branch=True)
        def branchfunc(request):
            return "foo"

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo/"), ("branchfunc", {}))
        self.assertEqual(c.match("/foo/bar"), ("branchfunc_branch", {
            '__rest__': 'bar'
        }))

        self.assertEquals(app.endpoints["branchfunc"].__name__,
                          "route '/foo/' executor for branchfunc")
        self.assertEquals(app.endpoints["branchfunc_branch"].__name__,
                          "branch route '/foo/' executor for branchfunc")
        self.assertEquals(
            app.execute_endpoint("branchfunc_branch",
                                 DummyRequest("looking for foo")), "foo")