def test_router() -> None: def test_controller() -> None: pass router = Router() router.append(Route("/pets/{pet_id}"), test_controller) router.append(Route("/pets"), test_controller) match = router.match("/pets/12") assert match[0]["pet_id"] == 12 assert router.match("/pets")
def test_router_prioritise_routes_with_no_wildcards() -> None: def test_controller(): pass router = Router() router.append(Route("/pets/*"), test_controller) router.append(Route("/pets/{pet_id}"), test_controller) router.append(Route("*"), test_controller) route, controller = router.match("/pets/11a22") assert route.route == "/pets/{pet_id}"
def test_can_copy_route() -> None: # given base_route = Route("/test/{parameter}") # when match_route = base_route.match("/test/test-1") route_copy = copy(match_route) # then assert match_route assert route_copy.attributes == match_route.attributes assert route_copy.route == match_route.route assert route_copy.parameters == match_route.parameters
def test_middleware_for_serverless() -> None: route = Route("/test/{id}") def cors_middleware( request: HttpRequest, next: Callable[[HttpRequest], HttpResponse]) -> HttpResponse: assert hasattr(request, "route") assert request.route is not None assert request.route == route assert request.path_parameters == {"id": "123"} assert request.route.parameters == {"id": "123"} response = next(request) response._headers.set("Access-Control-Allow-Origin", "*") return response def ok_handler(request: HttpRequest) -> HttpResponse: return HttpResponse(status=200) middleware_pipeline = MiddlewarePipeline() middleware_pipeline.append(cors_middleware) serverless_callback = AwsServerlessFunction(ok_handler, route, middleware_pipeline) dir_path = os.path.dirname(os.path.realpath(__file__)) event_json = json.load( open(os.path.join(dir_path, "../fixtures/lambda_http_api_event.json"))) response = serverless_callback(event_json, {}) assert "headers" in response assert "access-control-allow-origin" in response["headers"]
def test_router_fail_matching() -> None: def test_controller(): pass router = Router() router.append(Route("/pets/{pet_id}"), test_controller) with pytest.raises(NotFoundError): router.match("/pet/12")
def test_route_match() -> None: route = Route("/pets/{pet_id}") route = route.match("/pets/11a22") assert route["pet_id"] == "11a22" route = Route("/pets/{pet_id}") route = route.match("/pets/22") assert route._parameters == {"pet_id": 22}
def test_make_serverless_callback() -> None: route = Route("/test/{id}") def test_callaback(request: HttpRequest) -> HttpResponse: assert hasattr(request, "route") assert request.route == route return HttpResponse(request.path) serverless_callback = AwsServerlessFunction(test_callaback, route) dir_path = os.path.dirname(os.path.realpath(__file__)) event_json = json.load( open(os.path.join(dir_path, "../fixtures/lambda_http_api_event.json"))) response = serverless_callback(event_json, {}) assert "statusCode" in response assert response["statusCode"] == 200 assert "headers" in response assert "body" in response assert response["body"] == "/test/123"
def test_route_match_multiple_parameters() -> None: route = Route("/pets/{pet_id}/{category}") route = route.match("/pets/11a22/test") assert route["pet_id"] == "11a22" assert route["category"] == "test"
def test_route_is_wildcard() -> None: route = Route("*") assert route.is_wildcard assert route.pattern == re.compile(r"^.*?$", re.I | re.M)
def test_route_parsing_with_wildcards() -> None: route = Route("/example/{a}*") assert route.pattern == re.compile(r"^/example/([^/]+).*?$", re.I | re.M) assert route.match("/example/test/1/2/3") assert route.match("/example/11")
def test_route_parsing() -> None: route = Route("/example/{pattern}") assert route.match("/example/test")