Пример #1
0
    def test_derived(self):
        class Base(resource.Resource):
            @resource.GET(accept='text')
            def text(self, request):
                return http.ok([], 'Base')

            @resource.GET(accept='html')
            def html(self, request):
                return http.ok([], '<p>Base</p>')

        class Derived(Base):
            @resource.GET(accept='html')
            def html(self, request):
                return http.ok([], '<p>Derived</p>')

            @resource.GET(accept='json')
            def json(self, request):
                return http.ok([], '"Derived"')

        app = make_app(Derived())
        assert app.get('/', headers={
            'Accept': 'text/plain'
        }, status=200).body == 'Base'
        assert app.get('/', headers={
            'Accept': 'text/html'
        }, status=200).body == '<p>Derived</p>'
        assert app.get('/', headers={
            'Accept': 'application/json'
        }, status=200).body == '"Derived"'
Пример #2
0
 def test_bad_accept(self):
     class Resource(resource.Resource):
         @resource.GET()
         def text(self, request):
             return http.ok([('Content-Type', 'text/plain')], 'text')
     app = make_app(Resource())
     assert app.get('/', headers={'Accept': 'text/html, text/plain,'}, status=200).body == 'text'
     assert app.get('/', headers={'Accept': 'text/html, text/plain;'}, status=200).body == 'text'
 def test_derived_specificity(self):
     class Base(resource.Resource):
         @resource.GET(accept='text/*')
         def text(self, request):
             return http.ok([('Content-Type', 'text/html')], 'Base')
     class Derived(Base):
         @resource.GET(accept='text/plain')
         def html(self, request):
             return http.ok([], 'Derived')
     app = make_app(Derived())
     assert app.get('/', headers={'Accept': 'text/plain'}, status=200).body == 'Derived'
     assert app.get('/', headers={'Accept': 'text/html'}, status=200).body == 'Base'
Пример #4
0
    def test_urlfor_using_object(self):
        class Data(object):
            def __init__(self, a, b, c):
                self.a = a
                self.b = b
                self.c = c

            def __str__(self):
                return (u"%s %s %s " % (self.a, self.b, self.c)).encode("utf-8")

        class Abc(resource.Resource):
            def __init__(self, a, b, c):
                self.data = Data(a, b, c)

            @resource.GET()
            def get(self, request):
                return http.ok([("Content-type", "text/plain; charset=utf-8")],
                               str(self.data))

        class Root(resource.Resource):
            data = resource.child("{a}/{b}/{c}", Abc)
        
        tests = [(("a", "b", "c"), "/a/b/c"),
                 ((1, 2, 3), "/1/2/3"),
                 ((u"£", u"$", u"€"), url.join_path([u"£", u"$", u"€"]))
                ]

        app = make_app(Root())
        for data, path in tests:
            obj = Data(*data)
            # request
            response = app.get(path, status=200)
            assert response.body == str(obj)
            # reverse url
            assert resource.url_for("abc", obj) == path
Пример #5
0
    def test_canonical(self):
        class Book(resource.Resource):
            def __init__(self, title, **kwargs):
                self.title = title

            @resource.GET()
            def get(self, request):
                return http.ok([('Content-Type', 'text/plain')], self.title)

        class Resource(resource.Resource):
            category = resource.child("category/{category}/{title}", Book)
            permalink = resource.child("book/{title}", Book, canonical=True)
            shortlink = resource.child("b/{title}", Book)
            bydate = resource.child("{year}/{month}/{day}/{title}", Book)
        
        tests = [("/book/0", "0"),
                 ("/b/1", "1"),
                 ("/category/sleepy/2", "2"),
                 ("/2009/11/08/3", "3")
                ]

        app = make_app(Resource())
        for path, body in tests:
            response = app.get(path, status=200)
            assert response.body == body
        
        assert resource.url_for(Book, title="4") == "/book/4"
        assert resource.url_for("book", title="5") == "/book/5"
Пример #6
0
    def test_with_matchers(self):
        class Entry(resource.Resource):
            def __init__(self, id, slug):
                self.id = id
                self.slug = slug

            @resource.GET()
            def get(self, request):
                return http.ok([('Content-Type', 'text/plain')],
                               "entry (%s): %s" % (self.id, self.slug))

        class Blog(resource.Resource):
            def __init__(self, blogname):
                self.blogname = blogname

            @resource.GET()
            def get(self, request):
                return http.ok([('Content-Type', 'text/plain')],
                               "blog: %s" % self.blogname)

            entry = resource.child("{id}/{slug}", Entry)
 
        class Resource(resource.Resource):
            @resource.GET()
            def index(self, request):
                return http.ok([('Content-Type', 'text/plain')],
                               "index")

            blog = resource.child("{blogname}", Blog)

        tests = [("/", "index"),
                 ("/blog", "blog: blog"),
                 ("/wordpress", "blog: wordpress"),
                 ("/blog/1/hello world", "entry (1): hello world"),
                 ("/wordpress/2/hello world", "entry (2): hello world")
                ]
        
        app = make_app(Resource())
        for path, body in tests:
            response = app.get(path, status=200)
            assert response.body == body
        
        
        tests = [((Resource, {}), "/"),
                 (("resource", {}), "/"),
                 ((Blog, {"blogname": "b2"}), "/b2"),
                 (("blog", {"blogname": "b2"}), "/b2"),
                 ((Entry, {"blogname": "b2evolution",
                           "id": "1",
                           "slug": "foo"}),
                  "/b2evolution/1/foo"),
                 (("entry", {"blogname": "b2evolution",
                           "id": "1",
                           "slug": "foo"}),
                  "/b2evolution/1/foo"),
                ]

        for (klass, args), url in tests:
            assert resource.url_for(klass, **args) == url, url
Пример #7
0
    def test_bad_accept(self):
        class Resource(resource.Resource):
            @resource.GET()
            def text(self, request):
                return http.ok([('Content-Type', 'text/plain')], 'text')

        app = make_app(Resource())
        assert app.get('/',
                       headers={
                           'Accept': 'text/html, text/plain,'
                       },
                       status=200).body == 'text'
        assert app.get('/',
                       headers={
                           'Accept': 'text/html, text/plain;'
                       },
                       status=200).body == 'text'
Пример #8
0
    def test_derived_specificity(self):
        class Base(resource.Resource):
            @resource.GET(accept='text/*')
            def text(self, request):
                return http.ok([('Content-Type', 'text/html')], 'Base')

        class Derived(Base):
            @resource.GET(accept='text/plain')
            def html(self, request):
                return http.ok([], 'Derived')

        app = make_app(Derived())
        assert app.get('/', headers={
            'Accept': 'text/plain'
        }, status=200).body == 'Derived'
        assert app.get('/', headers={
            'Accept': 'text/html'
        }, status=200).body == 'Base'
 def test_derived(self):
     class Base(resource.Resource):
         @resource.GET(accept='text')
         def text(self, request):
             return http.ok([], 'Base')
         @resource.GET(accept='html')
         def html(self, request):
             return http.ok([], '<p>Base</p>')
     class Derived(Base):
         @resource.GET(accept='html')
         def html(self, request):
             return http.ok([], '<p>Derived</p>')
         @resource.GET(accept='json')
         def json(self, request):
             return http.ok([], '"Derived"')
     app = make_app(Derived())
     assert app.get('/', headers={'Accept': 'text/plain'}, status=200).body == 'Base'
     assert app.get('/', headers={'Accept': 'text/html'}, status=200).body == '<p>Derived</p>'
     assert app.get('/', headers={'Accept': 'application/json'}, status=200).body == '"Derived"'
Пример #10
0
    def test_sample(self):
        class Bar2(resource.Resource):
            @resource.GET()
            def get(self, request):
                return http.ok([("Content-Type", "text/plain")],
                               "bar")

        class Spam2(resource.Resource):
            def __init__(self, id, _parent):
                self.id = id
                self.parent = _parent

            @resource.GET()
            def get(self, request):
                return http.ok([("Content-Type", "text/plain")],
                               "%s %s" % (self.parent.foo, self.id))

        class Resource2(resource.Resource):
            def __init__(self, foo):
                self.foo = foo

            @resource.GET()
            def get(self, request):
                return http.ok([("Content-Type", "text/plain")],
                               "resource")

            foo = resource.redirect(Bar2)
            foo2 = resource.redirect("foo2", Bar2)
            bar = resource.child(Bar2)

            spum = resource.redirect("spum{id}", Spam2)
            spam = resource.child("spam{id}", Spam2, with_parent=True)

        app = make_app(Resource2("who's your daddy"))
        
        R = app.get("/", status=200)
        assert R.body == 'resource'

        R = app.get("/bar", status=200)
        assert R.body == 'bar'
        
        R = app.get("/foo", status=302)
        R = app.get("/foo2", status=302)
        
        R = app.get("/spam42", status=200)
        assert R.body == 'who\'s your daddy 42'
        
        R = app.get("/spum42", status=302)
        assert R.headers["location"].startswith('http://')
        assert R.headers["location"].endswith('/spam42')
Пример #11
0
    def test_unicode(self):
        class Moo(resource.Resource):
            def __init__(self, arg):
                self.arg = arg
    
            def __call__(self, segments):
                return http.ok([("Content-type", "text/plain; charset=utf-8")],
                                self.arg)

        class Root(resource.Resource):
            moo = resource.child(u"£-{arg}", Moo)

        tests = [(u"£-a", u"a"),
                 (u"£-ä", u"ä")
                ]

        app = make_app(Root())
        for path, body in tests:
            response = app.get(url.join_path([path]), status=200)
            assert response.body == body.encode("utf-8")
             
            assert resource.url_for("moo", arg=body) == url.join_path([path])
Пример #12
0
    def test_full(self):
        class Resource(resource.Resource):
            @resource.GET()
            def get(self, request):
                return http.ok([("Content-Type", "text/plain")],
                               "resource")

            @resource.redirect("foo", "bar")
            def foo(self, request):
                """Redirect to bar"""
            
            @resource.redirect("bar")
            def foo2(self, request):
                """Redirect to bar"""

            @resource.redirect("spam/or/eggs", ("spam", "and", "eggs"))
            def spam_or_eggs(self, request):
                """Redirect to spam *and* eggs"""

            @resource.child()
            def bar(self, request, segments):
                return http.ok([("Content-Type", "text/plain")],
                               "bar")

            @resource.child("spam/and/eggs")
            def spam_and_eggs(self, request, segments):
                return http.ok([("Content-Type", "text/plain")],
                               "spam")


        app = make_app(Resource())
        
        R = app.get("/", status=200)
        assert R.body == 'resource'
        
        R = app.get("/bar", status=200)
        assert R.body == 'bar'
        
        R = app.get("/foo", status=302) 
        R = app.get("/foo2", status=302)
        
        R = app.get("/spam/and/eggs", status=200)
        assert R.body == 'spam'
        
        R = app.get("/spam/or/eggs", status=302)
Пример #13
0
    def test_simple(self):
        class Index(resource.Resource):
            @resource.GET()
            def get(self, request):
                return http.ok([('Content-Type', 'text/plain')],
                               "index")

        class Entry(resource.Resource):
            @resource.GET()
            def get(self, request):
                return http.ok([('Content-Type', 'text/plain')],
                               "entry")

        class Blog(resource.Resource):
            @resource.GET()
            def get(self, request):
                return http.ok([('Content-Type', 'text/plain')],
                               "blog")

            @resource.child()
            def entry2(self, request, segments):
                return Entry(), segments

            entry = resource.child(Entry)
 
        class Resource(resource.Resource):
            @resource.child()
            def index2(self, request, segments):
                return Index(), segments
            @resource.child()
            def blog2(self, request, segments):
                return Blog(), segments

            index = resource.child(Index)
            blog = resource.child(Blog)
 
        tests = [("/index2", "index"),
                 ("/index", "index"),
                 ("/blog2", "blog"),
                 ("/blog", "blog"),
                 ("/blog2/entry2", "entry"),
                 ("/blog2/entry", "entry"),
                 ("/blog/entry2", "entry"),
                 ("/blog/entry", "entry"),
                ]
        
        app = make_app(Resource())
        for path, body in tests:
            response = app.get(path, status=200)
            assert response.body == body
        
        tests = [(Resource, "/"),
                 (Index, "/index"),
                 (Blog, "/blog"),
                 (Entry, "/blog/entry"),
                 ("Resource", "/"),
                 ("Index", "/index"),
                 ("Blog", "/blog"),
                 ("Entry", "/blog/entry"),
                 ("resource", "/"),
                 ("index", "/index"),
                 ("blog", "/blog"),
                 ("entry", "/blog/entry")
                ]

        for klass, url in tests:
            assert resource.url_for(klass) == url