예제 #1
0
파일: test_app.py 프로젝트: mtwtkman/tomoyo
    def setUp(self):
        self.solo_path = '/solo'
        self.solo_handler = lambda: 1
        self.multiple_path = '/multi/index.html'
        self.multiple_handler = lambda: 10
        self.scoped_parent_path = '/nested'
        self.scoped_child_path = '/scoped'
        self.scoped_handler = lambda: 2

        self.solo_service = resource(self.solo_path).get(self.solo_handler)
        self.multiple_service = resource(self.multiple_path).get(self.multiple_handler)
        self.scoped_service = resource(self.scoped_child_path).get(self.scoped_handler)
        self.app = App() \
            .service(self.solo_service) \
            .service(scope(self.scoped_parent_path).service(self.scoped_service)) \
            .service(self.multiple_service)
예제 #2
0
파일: test_app.py 프로젝트: mtwtkman/tomoyo
 def test_simple(self):
     path = '/hoge'
     one = self._makeOne(path)
     child = resource('/x').get(lambda req: 'x')
     one.service(child)
     self.assertEqual(one.path, path)
     self.assertTrue(child.path in one.resource_map)
     self.assertTrue(one.resource_map[child.path] is child)
예제 #3
0
파일: test_app.py 프로젝트: mtwtkman/tomoyo
 def test_nested(self):
     path = '/hoge'
     one = self._makeOne(path)
     child_path = '/fuga'
     child_one = self._makeOne(child_path)
     child_resource = resource('/x').get(lambda req: 'x')
     child_one.service(child_resource)
     one.service(child_one)
     self.assertEqual(one.path, path)
     self.assertTrue(child_one.path in one.resource_map)
     self.assertTrue(one.resource_map[child_one.path] is child_one)
예제 #4
0
파일: test_app.py 프로젝트: mtwtkman/tomoyo
    def setUp(self):
        class First(Middleware):
            def pre_request(self, request):
                request.via_first = 'hi'
                return request

            def post_response(self, response):
                response.via_first = 'hi'
                return response

        class Second(Middleware):
            def pre_request(self, request):
                request.via_second = 'yo'
                return request

            def post_response(self, response):
                response.via_second = 'yo'
                return response


        self.app = App().wrap(Hi).service(resource('/').get(lambda req: 'done'))
예제 #5
0
파일: test_app.py 프로젝트: mtwtkman/tomoyo
    def setUp(self):
        def root(request):
            return 'GET: root'

        def get_handler(request):
            x = json.dumps(request.body) if request.body else 'no param'
            return f'GET: {x}'

        def post_handler(request):
            x = json.dumps(request.body) if request.body else 'no param'
            return f'POST: {x}'

        def get_json_handler(request):
            return {'x': 1, y: 'x'}

        @get('/decorated')
        def decorated_handler(request):
            return 'GET: decorated'

        def scoped_handler(request):
            return 'GET: scoped'

        def regex_handler(request, id_):
            return f'GET: {id_}'

        def stream_handler(request, word):
            return StringIO(f'Stream: {word}')

        scoped = scope('/nest1').service(resource('/scoped').get(scoped_handler))

        self.dummy_app = App() \
            .service(resource('/').get(root)) \
            .service(resource('/get').get(get_handler)) \
            .service(resource('/post').post(post_handler)) \
            .service(decorated_handler) \
            .service(scoped) \
            .service(resource(r'/(?P<id_>\d+)').get(regex_handler)) \
            .service(resource(f'/stream/(?P<word>\w+)').get(stream_handler))
예제 #6
0
 def _callFUT(self, handler, method):
     from tomoyo.resource import resource
     return resource('')._to(handler, method)
예제 #7
0
 def test_error(self):
     path = '/foo'
     self._callFUT(resource(path).get(lambda: 1))
     with self.assertRaises(ReservedPathError):
         self._callFUT(resource(path).get(lambda: 2))
예제 #8
0
 def test_ok(self):
     path = '/foo'
     actual = self._callFUT(resource(path).get(lambda: 1))
     self.assertTrue(path in actual.resource_map)