當前位置: 首頁>>代碼示例>>Python>>正文


Python rest.RestController方法代碼示例

本文整理匯總了Python中pecan.rest.RestController方法的典型用法代碼示例。如果您正苦於以下問題:Python rest.RestController方法的具體用法?Python rest.RestController怎麽用?Python rest.RestController使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pecan.rest的用法示例。


在下文中一共展示了rest.RestController方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_getall_with_trailing_slash

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_getall_with_trailing_slash(self):

        class ThingsController(RestController):

            data = ['zero', 'one', 'two', 'three']

            @expose('json')
            def get_all(self):
                return dict(items=self.data)

        class RootController(object):
            things = ThingsController()

        # create the app
        app = TestApp(make_app(RootController()))

        # test get_all
        r = app.get('/things/')
        assert r.status_int == 200
        assert r.body == b_(dumps(dict(items=ThingsController.data))) 
開發者ID:pecan,項目名稱:pecan,代碼行數:22,代碼來源:test_rest.py

示例2: test_nested_rest_with_default

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_nested_rest_with_default(self):

        class FooController(RestController):

            @expose()
            def _default(self, *remainder):
                return "DEFAULT %s" % remainder

        class RootController(RestController):
            foo = FooController()

        app = TestApp(make_app(RootController()))

        r = app.get('/foo/missing')
        assert r.status_int == 200
        assert r.body == b_("DEFAULT missing") 
開發者ID:pecan,項目名稱:pecan,代碼行數:18,代碼來源:test_rest.py

示例3: test_rest_with_non_utf_8_body

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_rest_with_non_utf_8_body(self):
        if PY3:
            # webob+PY3 doesn't suffer from this bug; the POST parsing in PY3
            # seems to more gracefully detect the bytestring
            return

        class FooController(RestController):

            @expose()
            def post(self):
                return "POST"

        class RootController(RestController):
            foo = FooController()

        app = TestApp(make_app(RootController()))

        data = struct.pack('255h', *range(0, 255))
        r = app.post('/foo/', data, expect_errors=True)
        assert r.status_int == 400 
開發者ID:pecan,項目名稱:pecan,代碼行數:22,代碼來源:test_rest.py

示例4: test_405_with_lookup

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_405_with_lookup(self):

        class LookupController(RestController):

            def __init__(self, _id):
                self._id = _id

            @expose()
            def get_all(self):
                return 'ID: %s' % self._id

        class ThingsController(RestController):

            @expose()
            def _lookup(self, _id, *remainder):
                return LookupController(_id), remainder

        class RootController(object):
            things = ThingsController()

        # create the app
        app = TestApp(make_app(RootController()))

        # these should 405
        for path in ('/things', '/things/'):
            r = app.get(path, expect_errors=True)
            assert r.status_int == 405

        r = app.get('/things/foo')
        assert r.status_int == 200
        assert r.body == b_('ID: foo') 
開發者ID:pecan,項目名稱:pecan,代碼行數:33,代碼來源:test_rest.py

示例5: test_getall_with_lookup

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_getall_with_lookup(self):

        class LookupController(RestController):

            def __init__(self, _id):
                self._id = _id

            @expose()
            def get_all(self):
                return 'ID: %s' % self._id

        class ThingsController(RestController):

            data = ['zero', 'one', 'two', 'three']

            @expose()
            def _lookup(self, _id, *remainder):
                return LookupController(_id), remainder

            @expose('json')
            def get_all(self):
                return dict(items=self.data)

        class RootController(object):
            things = ThingsController()

        # create the app
        app = TestApp(make_app(RootController()))

        # test get_all
        for path in ('/things', '/things/'):
            r = app.get(path)
            assert r.status_int == 200
            assert r.body == b_(dumps(dict(items=ThingsController.data)))

        r = app.get('/things/foo')
        assert r.status_int == 200
        assert r.body == b_('ID: foo') 
開發者ID:pecan,項目名稱:pecan,代碼行數:40,代碼來源:test_rest.py

示例6: test_get_with_var_args

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_get_with_var_args(self):

        class OthersController(object):

            @expose()
            def index(self, one, two, three):
                return 'NESTED: %s, %s, %s' % (one, two, three)

        class ThingsController(RestController):

            others = OthersController()

            @expose()
            def get_one(self, *args):
                return ', '.join(args)

        class RootController(object):
            things = ThingsController()

        # create the app
        app = TestApp(make_app(RootController()))

        # test get request
        r = app.get('/things/one/two/three')
        assert r.status_int == 200
        assert r.body == b_('one, two, three')

        # test nested get request
        r = app.get('/things/one/two/three/others/')
        assert r.status_int == 200
        assert r.body == b_('NESTED: one, two, three') 
開發者ID:pecan,項目名稱:pecan,代碼行數:33,代碼來源:test_rest.py

示例7: test_sub_nested_rest

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_sub_nested_rest(self):

        class BazsController(RestController):

            data = [[['zero-zero-zero']]]

            @expose()
            def get_one(self, foo_id, bar_id, id):
                return self.data[int(foo_id)][int(bar_id)][int(id)]

        class BarsController(RestController):

            data = [['zero-zero']]

            bazs = BazsController()

            @expose()
            def get_one(self, foo_id, id):
                return self.data[int(foo_id)][int(id)]

        class FoosController(RestController):

            data = ['zero']

            bars = BarsController()

            @expose()
            def get_one(self, id):
                return self.data[int(id)]

        class RootController(object):
            foos = FoosController()

        # create the app
        app = TestApp(make_app(RootController()))

        # test sub-nested get_one
        r = app.get('/foos/0/bars/0/bazs/0')
        assert r.status_int == 200
        assert r.body == b_('zero-zero-zero') 
開發者ID:pecan,項目名稱:pecan,代碼行數:42,代碼來源:test_rest.py

示例8: test_method_not_allowed_get

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_method_not_allowed_get(self):
        class ThingsController(RestController):

            @expose()
            def put(self, id_, value):
                response.status = 200

            @expose()
            def delete(self, id_):
                response.status = 200

        app = TestApp(make_app(ThingsController()))
        r = app.get('/', status=405)
        assert r.status_int == 405
        assert r.headers['Allow'] == 'DELETE, PUT' 
開發者ID:pecan,項目名稱:pecan,代碼行數:17,代碼來源:test_rest.py

示例9: test_method_not_allowed_post

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_method_not_allowed_post(self):
        class ThingsController(RestController):

            @expose()
            def get_one(self):
                return dict()

        app = TestApp(make_app(ThingsController()))
        r = app.post('/', {'foo': 'bar'}, status=405)
        assert r.status_int == 405
        assert r.headers['Allow'] == 'GET' 
開發者ID:pecan,項目名稱:pecan,代碼行數:13,代碼來源:test_rest.py

示例10: test_method_not_allowed_put

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_method_not_allowed_put(self):
        class ThingsController(RestController):

            @expose()
            def get_one(self):
                return dict()

        app = TestApp(make_app(ThingsController()))
        r = app.put('/123', status=405)
        assert r.status_int == 405
        assert r.headers['Allow'] == 'GET' 
開發者ID:pecan,項目名稱:pecan,代碼行數:13,代碼來源:test_rest.py

示例11: test_proper_allow_header_multiple_gets

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_proper_allow_header_multiple_gets(self):
        class ThingsController(RestController):

            @expose()
            def get_all(self):
                return dict()

            @expose()
            def get(self):
                return dict()

        app = TestApp(make_app(ThingsController()))
        r = app.put('/123', status=405)
        assert r.status_int == 405
        assert r.headers['Allow'] == 'GET' 
開發者ID:pecan,項目名稱:pecan,代碼行數:17,代碼來源:test_rest.py

示例12: test_alternate_route

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_alternate_route(self):

        class RootController(RestController):

            @expose(route='some-path')
            def get_all(self):
                return "Hello, World!"

        self.assertRaises(
            ValueError,
            RootController
        ) 
開發者ID:pecan,項目名稱:pecan,代碼行數:14,代碼來源:test_rest.py

示例13: setUp

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def setUp(self):
        super(TestRestControllerStateAccess, self).setUp()
        self.args = None

        class RootController(rest.RestController):

            @expose()
            def _default(self, _id, *args, **kw):
                return 'Default'

            @expose()
            def get_all(self, **kw):
                return 'All'

            @expose()
            def get_one(self, _id, *args, **kw):
                return 'One'

            @expose()
            def post(self, *args, **kw):
                return 'POST'

            @expose()
            def put(self, _id, *args, **kw):
                return 'PUT'

            @expose()
            def delete(self, _id, *args, **kw):
                return 'DELETE'

        class SimpleHook(PecanHook):
            def before(inself, state):
                self.args = (state.controller, state.arguments)

        self.root = RootController()
        self.app = TestApp(make_app(self.root, hooks=[SimpleHook()])) 
開發者ID:pecan,項目名稱:pecan,代碼行數:38,代碼來源:test_hooks.py

示例14: test_simple_nested_rest

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_simple_nested_rest(self):

        class BarController(RestController):

            @expose()
            def post(self):
                return "BAR-POST"

            @expose()
            def delete(self, id_):
                return "BAR-%s" % id_

        class FooController(RestController):

            bar = BarController()

            @expose()
            def post(self):
                return "FOO-POST"

            @expose()
            def delete(self, id_):
                return "FOO-%s" % id_

        class RootController(object):
            foo = FooController()

        # create the app
        app = TestApp(make_app(RootController()))

        r = app.post('/foo')
        assert r.status_int == 200
        assert r.body == b_("FOO-POST")

        r = app.delete('/foo/1')
        assert r.status_int == 200
        assert r.body == b_("FOO-1")

        r = app.post('/foo/bar')
        assert r.status_int == 200
        assert r.body == b_("BAR-POST")

        r = app.delete('/foo/bar/2')
        assert r.status_int == 200
        assert r.body == b_("BAR-2") 
開發者ID:pecan,項目名稱:pecan,代碼行數:47,代碼來源:test_rest.py

示例15: test_nested_get_all

# 需要導入模塊: from pecan import rest [as 別名]
# 或者: from pecan.rest import RestController [as 別名]
def test_nested_get_all(self):

        class BarsController(RestController):

            @expose()
            def get_one(self, foo_id, id):
                return '4'

            @expose()
            def get_all(self, foo_id):
                return '3'

        class FoosController(RestController):

            bars = BarsController()

            @expose()
            def get_one(self, id):
                return '2'

            @expose()
            def get_all(self):
                return '1'

        class RootController(object):
            foos = FoosController()

        # create the app
        app = TestApp(make_app(RootController()))

        r = app.get('/foos/')
        assert r.status_int == 200
        assert r.body == b_('1')

        r = app.get('/foos/1/')
        assert r.status_int == 200
        assert r.body == b_('2')

        r = app.get('/foos/1/bars/')
        assert r.status_int == 200
        assert r.body == b_('3')

        r = app.get('/foos/1/bars/2/')
        assert r.status_int == 200
        assert r.body == b_('4')

        r = app.get('/foos/bars/', status=404)
        assert r.status_int == 404

        r = app.get('/foos/bars/1', status=404)
        assert r.status_int == 404 
開發者ID:pecan,項目名稱:pecan,代碼行數:53,代碼來源:test_rest.py


注:本文中的pecan.rest.RestController方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。