def setUp(self):
        self.routing_table = TreeRoutingTable()
        self.routing_table.register_route(GET, PATH, RESOURCE_CLASS,
                                          ROUTE_METHOD)
        self.route_handler_factory = RouteHandlerFactory(
            ServiceLocator(), Deserializer(Registry()),
            [RoutingRule("/", self.routing_table)],
            CorsRequestHandlerFactory([CorsRule("/", {})]))

        self.request_builder = RequestBuilder().method("GET").path(PATH)
Ejemplo n.º 2
0
    def test_givenPrefixedRule_whenCreatingRouteHandlers_thenCreateResourceInvokerWithTruncatedPathForEveryRoute(
            self):
        request = self.request_builder.path("/prefix" + PATH).build()
        rule = RoutingRule("/prefix", self.routing_table)
        self.route_handler_factory = RouteHandlerFactory(
            ServiceLocator(), Deserializer(Registry()), [rule],
            CorsHandler([CorsRule("/", {})]))

        handlers: List[PathRewritingRouteHandlerDecorator] = [
            x
            for x in self.route_handler_factory.create_route_handlers(request)
        ]

        self.assertEqual(1, len(handlers))
        self.assertEqual(PATH, handlers[0].new_path)
Ejemplo n.º 3
0
 def build(self, registry: Registry,
           service_locator: ServiceLocator) -> Router:
     filter_chain_factory = FilterChainFactory(
         self.filtering_rules, service_locator,
         RouteHandlerFactory(service_locator, Deserializer(registry),
                             self.routing_rules,
                             CorsRequestHandlerFactory(self.cors_rules)))
     return Router(service_locator, RequestFactory(), filter_chain_factory)
    def invoke(self, request: Request) -> Response:
        response = ResourceClass.the_response
        FilterChain(
            [],
            RouteHandlerFactory(self.service_locator, self.deserializer,
                                [RoutingRule("", self.routing_table)],
                                CorsHandler([]))).doFilter(request, response)

        return response
class RouteHandlerFactoryTest(unittest.TestCase):
    def setUp(self):
        self.routing_table = TreeRoutingTable()
        self.routing_table.register_route(GET, PATH, RESOURCE_CLASS,
                                          ROUTE_METHOD)
        self.route_handler_factory = RouteHandlerFactory(
            ServiceLocator(), Deserializer(Registry()),
            [RoutingRule("/", self.routing_table)],
            CorsRequestHandlerFactory([CorsRule("/", {})]))

        self.request_builder = RequestBuilder().method("GET").path(PATH)

    def test_givenNoMatchingRoutes_whenCreatingRouteHandlers_thenRaiseUnknownPathException(
            self):
        request = self.request_builder.path("/unknown").build()

        with self.assertRaises(UnknownPathException):
            self.route_handler_factory.create_route_handlers(request)

    def test_givenDisallowedMethod_whenCreatingRouteHandlers_thenRaiseMethodNotAllowedException(
            self):
        request = self.request_builder.method("POST").build()

        with self.assertRaises(MethodNotAllowedException):
            self.route_handler_factory.create_route_handlers(request)

    def test_givenPrefixedRule_whenCreatingRouteHandlers_thenCreateResourceInvokerWithTruncatedPathForEveryRoute(
            self):
        request = self.request_builder.path("/prefix" + PATH).build()
        rule = RoutingRule("/prefix", self.routing_table)
        self.route_handler_factory = RouteHandlerFactory(
            ServiceLocator(), Deserializer(Registry()), [rule],
            CorsRequestHandlerFactory([CorsRule("/", {})]))

        handlers: List[PathRewritingRouteHandlerDecorator] = [
            x
            for x in self.route_handler_factory.create_route_handlers(request)
        ]

        self.assertEqual(1, len(handlers))
        # TODO do assertion which does not imply internal structure
        self.assertEqual(PATH, handlers[0].route_handler.new_path)

    def test_givenCorsRequestOnUnknownPath_whenCreatingRouteHandlers_thenRaiseUnknownPathException(
            self):
        request = self.request_builder.method("OPTIONS").path(
            "/unknown").build()

        with self.assertRaises(UnknownPathException):
            self.route_handler_factory.create_route_handlers(request)

    def test_givenCorsRequest_whenCreatingRouteHandlers_thenReturnSingleCorsHandler(
            self):
        request = self.request_builder.method("OPTIONS").build()

        handlers = [
            x
            for x in self.route_handler_factory.create_route_handlers(request)
        ]

        self.assertEqual(1, len(handlers))
        self.assertIsInstance(handlers[0], CorsPreflightRequestHandler)