Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ensure_bytes(value, expected_result):
assert ensure_bytes(value) == expected_result
def test_ensure_bytes_throws_for_invalid_value():
with pytest.raises(ValueError):
ensure_bytes(True)
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
def add_route(self, method: BytesOrStr, route: Route):
self.routes[ensure_bytes(method)].append(route)
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:
if callable(base_route):
value = base_route()
elif isinstance(base_route, (str, bytes)):
value = base_route
else:
raise RuntimeError(f'Invalid controller `route` attribute. Controller `{controller_type.__name__}` '
f'has an invalid route attribute: it should be callable, or str, or bytes.')
if value:
return ensure_bytes(join_fragments(value, route.pattern))
return route.pattern
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)