Beispiel #1
0
    def test_route_method_verbose(self, internal):
        sv = inspect.StringVisitor(True, internal)
        rm = inspect.inspect_routes(make_app())[0].methods[0]

        assert sv.process(
            rm) == '{0.method} - {0.function_name} ({0.source_info})'.format(
                rm)
Beispiel #2
0
    def test_routes(self, asgi):
        routes = inspect.inspect_routes(
            make_app_async() if asgi else make_app())

        self.check_route(asgi, routes[0], '/foo', 'MyResponder',
                         ['GET', 'POST', 'DELETE'], 'on_{}')
        self.check_route(asgi, routes[1], '/foo/{id}', 'MyResponder',
                         ['GET', 'PUT', 'DELETE'], 'on_{}_id')
        self.check_route(asgi, routes[2], '/bar', 'OtherResponder', ['POST'],
                         'on_{}_id')
Beispiel #3
0
def main():
    parser = make_parser()
    args = parser.parse_args()
    app = load_app(parser, args)
    if args.route_only:
        routes = inspect_routes(app)
        visitor = StringVisitor(args.verbose, args.internal)
        for route in routes:
            print(visitor.process(route))
    else:
        print(inspect_app(app).to_string(args.verbose, args.internal))
Beispiel #4
0
    def test_routes_empty_paths(self, asgi):
        app = get_app(asgi)
        r = i_f.MyResponderAsync() if asgi else i_f.MyResponder()
        app.add_route('/foo/bar/baz', r)

        routes = inspect.inspect_routes(app)

        assert len(routes) == 1

        self.check_route(asgi, routes[0], '/foo/bar/baz', 'MyResponder',
                         ['GET', 'POST', 'DELETE'], 'on_{}')
Beispiel #5
0
    def test_route(self, internal):
        sv = inspect.StringVisitor(False, internal)
        r = inspect.inspect_routes(make_app())[0]

        ml = ['   ├── {}'.format(sv.process(m))
              for m in r.methods if not m.internal or internal][:-1]
        ml += ['   └── {}'.format(sv.process(m))
               for m in r.methods if not m.internal or internal][-1:]

        exp = '⇒ {0.path} - {0.class_name}:\n{1}'.format(r, '\n'.join(ml))
        assert sv.process(r) == exp
Beispiel #6
0
 def test_routes_only(self, verbose, internal, monkeypatch):
     args = ['some-file.py', '{}:{}'.format(_MODULE, '_APP'), '-r']
     if verbose:
         args.append('-v')
     if internal:
         args.append('-i')
     monkeypatch.setattr('sys.argv', args)
     output = io.StringIO()
     with redirected(stdout=output):
         inspect_app.main()
     routes = inspect.inspect_routes(_APP)
     sv = inspect.StringVisitor(verbose, internal)
     expect = '\n'.join([sv.process(r) for r in routes])
     self.check(output.getvalue().strip(), expect)
Beispiel #7
0
    def test_register_other_router(self, monkeypatch):
        monkeypatch.setattr(inspect, '_supported_routers', {})

        app = get_app(False)
        app._router = i_f.MyRouter()

        @inspect.register_router(i_f.MyRouter)
        def print_routes(r):
            assert r is app._router
            return [inspect.RouteInfo('foo', 'bar', '', [])]

        ri = inspect.inspect_routes(app)

        assert ri[0].source_info == ''
        assert ri[0].path == 'foo'
        assert ri[0].class_name == 'bar'
        assert ri[0].methods == []
Beispiel #8
0
 def test_route_no_methods(self, internal):
     sv = inspect.StringVisitor(False, internal)
     r = inspect.inspect_routes(make_app())[0]
     r.methods.clear()
     exp = '⇒ {0.path} - {0.class_name}'.format(r)
     assert sv.process(r) == exp
Beispiel #9
0
    def test_route_method(self, internal):
        sv = inspect.StringVisitor(False, internal)
        rm = inspect.inspect_routes(make_app())[0].methods[0]

        assert sv.process(rm) == '{0.method} - {0.function_name}'.format(rm)
Beispiel #10
0
    def test_register_router_not_found(self, monkeypatch):
        monkeypatch.setattr(inspect, '_supported_routers', {})

        app = get_app(False)
        with pytest.raises(TypeError, match='Unsupported router class'):
            inspect.inspect_routes(app)