Ejemplo n.º 1
0
 def add(self, method: str, pattern: BytesOrStr, handler: Any):
     self.mark_handler(handler)
     method = ensure_bytes(method)
     pattern = ensure_bytes(pattern)
     new_route = Route(pattern, handler)
     self._check_duplicate(method, new_route)
     self.add_route(method, new_route)
Ejemplo n.º 2
0
 def get_matching_route(self, method: AnyStr,
                        value: AnyStr) -> Optional[Route]:
     for route in self.routes[ensure_bytes(method)]:
         match = route.match(ensure_bytes(value))
         if match:
             return route
     return None
Ejemplo n.º 3
0
    def get_controller_handler_pattern(
        self, controller_type: Type, route: RegisteredRoute
    ) -> bytes:
        """
        Returns the full pattern to be used for a route handler,
        defined as controller method.
        """
        base_route = getattr(controller_type, "route", None)

        if base_route is not None:
            if callable(base_route):
                value = base_route()
            elif isinstance(base_route, (str, bytes)):
                value = base_route
            else:
                raise RuntimeError(
                    f"Invalid controller `route` attribute. "
                    f"Controller `{controller_type.__name__}` "
                    f"has an invalid route attribute: it should "
                    f"be callable, or str, or bytes."
                )

            if value:
                return ensure_bytes(join_fragments(value, route.pattern))
        return ensure_bytes(route.pattern)
Ejemplo n.º 4
0
    def get_match(self, method: AnyStr, value: AnyStr) -> Optional[RouteMatch]:
        for route in self.routes[ensure_bytes(method)]:
            match = route.match(ensure_bytes(value))
            if match:
                return match
        if self._fallback is None:
            return None

        return RouteMatch(self._fallback, None)
Ejemplo n.º 5
0
    def get_match(self, method: BytesOrStr, value: BytesOrStr) -> RouteMatch:
        method = ensure_bytes(method)
        value = ensure_bytes(value)

        for route in self.routes[method]:
            match = route.match(value)
            if match:
                return match
        return RouteMatch(self._fallback, None) if self.fallback else None
Ejemplo n.º 6
0
 def add_route(self, method: AnyStr, route: Route):
     self.routes[ensure_bytes(method)].append(route)
Ejemplo n.º 7
0
 def add(self, method: str, pattern: AnyStr, handler: Any):
     self.mark_handler(handler)
     method_name = ensure_bytes(method)
     new_route = Route(ensure_bytes(pattern), handler)
     self._check_duplicate(method_name, new_route)
     self.add_route(method_name, new_route)
Ejemplo n.º 8
0
def test_ensure_bytes_throws_for_invalid_value():
    with pytest.raises(ValueError):
        ensure_bytes(True)
Ejemplo n.º 9
0
def test_ensure_bytes(value, expected_result):
    assert ensure_bytes(value) == expected_result