예제 #1
0
    def test_add_duplicate_route_raises_exception(self):
        router = Router()
        route = router.add_route({
            'id': 'id1',
            'path': '/endpoint_\\w*',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        with pytest.raises(DuplicateRouteError):
            route = router.add_route({
                'id': 'id1',
                'path': '/endpoint_\\w*',
                'responses': [
                    {
                        'id': 'response_1',
                        'body': {
                            'works': True
                        }
                    }
                ]
            })
예제 #2
0
 def __init__(self, config: Config) -> None:
     super().__init__(__name__)
     self.config.from_object(config)
     self.user_router = Router()
     self.load_routes()
     self._register_handlers()
     self._register_blueprints()
예제 #3
0
 def test_reset_router_with_default_routes(self):
     router = Router()
     router.reset([
         {
             "path": "/minimal_endpoint",
             "responses": [
                 {
                     "body": "response"
                 }
             ]
         }
     ])
     
     assert len(router.routes) == 1
예제 #4
0
    def test_update_route_not_present(self):
        router = Router()

        with pytest.raises(MissingRouteError):
            router.update_route({
                'id': 'id1',
                'path': '/endpoint\\w*',
                'responses': [
                    {
                        'id': 'response_1',
                        'body': {
                            'works': True
                        }
                    }
                ]
            }, 'id1')
예제 #5
0
    def test_get_route(self):
        router = Router()
        route = router.add_route({
            'id': 'id1',
            'path': '/endpoint_\\w*',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        assert router.get_route('id1') is route
예제 #6
0
    def test_add_route(self):
        router = Router()
        route = router.add_route({
            'id': 'id1',
            'path': '/endpoint_\\w*',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        assert list(router.routes) == [route]
예제 #7
0
 def test_match_with_no_matching_route(self):
     router = Router()
     route = router.add_route({
         'id': 'id1',
         'path': '/endpoint\\w*',
         'responses': [
             {
                 'id': 'response_1',
                 'body': {
                     'works': True
                 }
             }
         ]
     })
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET'
     )
     
     assert router.match(request) is None
예제 #8
0
    def test_update_route_and_change_its_id(self):
        router = Router()
        router.add_route({
            'id': 'id1',
            'path': '/endpoint\\w*',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        router.update_route({
            'id': 'id2',
            'path': '/endpoint\\w*',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        }, 'id1')
예제 #9
0
class ApiApp(Flask):
    """Flask application handling API endpoints."""
    def __init__(self, config: Config) -> None:
        super().__init__(__name__)
        self.config.from_object(config)
        self.user_router = Router()
        self.load_routes()
        self._register_handlers()
        self._register_blueprints()

    def load_routes(self) -> None:
        """Load configured default routes."""
        self.user_router.reset(self.config['DEFAULT_ROUTES'])

    def _register_handlers(self) -> None:
        """Register error page handlers."""
        self.register_error_handler(HTTPException, http_error_handler)

    def _register_blueprints(self) -> None:
        """Register api endpoints."""
        self.register_blueprint(internal.endpoints,
                                url_prefix=self.config['INTERNAL_PREFIX'])
        self.register_blueprint(utility.endpoints,
                                url_prefix=self.config['INTERNAL_PREFIX'])
        self.register_blueprint(external.endpoints, url_prefix='')

    def run(self) -> None:  # type: ignore # pragma: no cover
        """Start app.

        Run doesn't provide the full list of options of Flask on purpose.
        It violates the Liskov Substitution Principle. But this method is not
        intended for production use. It's used only for a local run using custom
        Click API which accounts for that.

        To change the configuration of the app, use `trickster.config.Config`.
        """
        super().run(port=self.config['PORT'])
예제 #10
0
    def test_update_route_to_that_already_exists(self):
        router = Router()
        router.add_route({
            'id': 'id1',
            'path': '/endpoint\\w*',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        router.add_route({
            'id': 'id2',
            'path': '/endpoint\\w*',
            'responses': [
                {
                    'id': 'response_2',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        with pytest.raises(DuplicateRouteError):
            router.update_route({
                'id': 'id1',
                'path': '/endpoint\\w*',
                'responses': [
                    {
                        'id': 'response_1',
                        'body': {
                            'works': True
                        }
                    }
                ]
            }, 'id2')
예제 #11
0
 def test_get_route(self):
     router = Router()
     assert router.get_route('id1') is None
예제 #12
0
 def test_initialize_empty_router(self):
     router = Router()
     assert list(router.routes) == []