class RouteRegistrationTest(unittest.TestCase): def setUp(self): self.routeRegistration = RouteRegistration(A_RESOURCE_CLASS, A_ROUTE_FUNCTION, A_PATH_WITH_PARAMETERS, HTTP_METHOD) def test_givenAPathWithParameters_whenParsingPathParameters_thenReturnAllMatchingTokensAsParameters( self): gotten_path = "/hello/paul-atreides" parameters = self.routeRegistration.parse_path_parameters(gotten_path) self.assertEqual(1, len(parameters)) self.assertEqual("paul-atreides", parameters['name']) def test_givenAPathWithoutParameters_whenParsingPathParameters_thenReturnEmptyDictionary( self): self.routeRegistration = RouteRegistration(A_RESOURCE_CLASS, A_ROUTE_FUNCTION, A_PATH_WITHOUT_PARAMETERS, HTTP_METHOD) gotten_path = "/hello/goodbye" parameters = self.routeRegistration.parse_path_parameters(gotten_path) self.assertEqual(0, len(parameters))
def test_givenAPathWithoutParameters_whenParsingPathParameters_thenReturnEmptyDictionary( self): self.routeRegistration = RouteRegistration(A_RESOURCE_CLASS, A_ROUTE_FUNCTION, A_PATH_WITHOUT_PARAMETERS, HTTP_METHOD) gotten_path = "/hello/goodbye" parameters = self.routeRegistration.parse_path_parameters(gotten_path) self.assertEqual(0, len(parameters))
def register_route(self, primitive: HttpMethod, path: str, resource_class: Union[type, object], route_method: Callable): path = split_path(path) self.root_node.register_child( path, primitive, RouteRegistration(resource_class, route_method, path, primitive))
class RouteNodeTest(unittest.TestCase): A_SIMPLE_PATH = ['hello'] HTTP_PRIMITIVE = GET A_ROUTE_REGISTRATION = RouteRegistration(None, None, [""], HTTP_PRIMITIVE) A_LONGER_PATH = ['hello', 'goodbye'] A_PATH_WITH_PARAMETERS = ["hello", "{id}", "delete"] A_PATH_WITH_DIFFERENT_PARAMETERS = ["hello", "{name}", "delete"] def setUp(self): self.rootNode = RouteNode() def test_whenRegisteringAPath_thenMethodInvocatorIsSavedLastChildNode( self): self.rootNode.register_child(self.A_SIMPLE_PATH, self.HTTP_PRIMITIVE, self.A_ROUTE_REGISTRATION) self.assertEqual({self.HTTP_PRIMITIVE: [self.A_ROUTE_REGISTRATION]}, self.rootNode.children['hello'].invocators) def test_givenPathOfMultipleElements_whenRegistering_thenMethodInvocatorIsOnlySavedAtLastChildNode( self): self.rootNode.register_child(self.A_LONGER_PATH, self.HTTP_PRIMITIVE, self.A_ROUTE_REGISTRATION) self.assertEqual( {self.HTTP_PRIMITIVE: [self.A_ROUTE_REGISTRATION]}, self.rootNode.children['hello'].children['goodbye'].invocators) self.assertEqual(1, len(self.rootNode.children)) self.assertEqual(1, len(self.rootNode.children['hello'].children)) def test_whenExploring_thenWalkTheTreeWordByWord(self): self.rootNode.register_child(self.A_LONGER_PATH, self.HTTP_PRIMITIVE, self.A_ROUTE_REGISTRATION) node = self.rootNode.explore(self.A_LONGER_PATH) self.assertEqual(self.A_ROUTE_REGISTRATION, node.invocators[self.HTTP_PRIMITIVE][0]) def test_givenUnknownPath_whenExploring_thenThrowUnknownPathException( self): with self.assertRaises(UnknownPathException): self.rootNode.explore(self.A_SIMPLE_PATH) def test_givenParameters_whenExploring_thenFollowPathRegardlessOfParameter( self): self.rootNode.register_child(self.A_PATH_WITH_PARAMETERS, self.HTTP_PRIMITIVE, self.A_ROUTE_REGISTRATION) node = self.rootNode.explore(self.A_PATH_WITH_PARAMETERS) node2 = self.rootNode.explore(self.A_PATH_WITH_DIFFERENT_PARAMETERS) self.assertEqual(self.A_ROUTE_REGISTRATION, node.invocators[self.HTTP_PRIMITIVE][0]) self.assertEqual(node, node2)
def get_route_registrations(self, path: str) -> List[RouteRegistration]: filepath = os.path.join(self.folder_root, path.lstrip("/")) if not os.path.exists(filepath) or self.__is_disallowed_extension( path) or os.path.isdir(filepath): return [] return [ RouteRegistration(StaticFileServingResource(filepath), StaticFileServingResource.serve_file, split_path(path), GET) ]
@GET @Path("/overloaded") def overloaded_param(self, name: QueryParam[str]) -> int: return 5 OVERLOADED_RETURN = 6 @GET @Path("/overloaded") def overloaded_without_name_parameter(self, query: QueryParam[str]) -> int: return self.OVERLOADED_RETURN @GET @Path("/nullable-query") def nullable_query(self, query: OptionalQueryParam[str]) -> Optional[str]: return query @GET @Path("/error") def raises_error(self) -> str: raise AnException @GET @Path("/headers") def get_with_headers(self, headers: Headers) -> list: return headers.items() ROUTE_REGISTRATION = RouteRegistration(ResourceClass, ResourceClass.a_method, [""], GET)
def setUp(self): self.routeRegistration = RouteRegistration(A_RESOURCE_CLASS, A_ROUTE_FUNCTION, A_PATH_WITH_PARAMETERS, HTTP_METHOD)