|
17 | 17 | ProxyEventType,
|
18 | 18 | Response,
|
19 | 19 | ResponseBuilder,
|
| 20 | + Router, |
20 | 21 | )
|
21 | 22 | from aws_lambda_powertools.event_handler.exceptions import (
|
22 | 23 | BadRequestError,
|
@@ -860,3 +861,136 @@ def base():
|
860 | 861 | # THEN process event correctly
|
861 | 862 | assert result["statusCode"] == 200
|
862 | 863 | assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
|
| 864 | + |
| 865 | + |
| 866 | +def test_api_gateway_app_router(): |
| 867 | + # GIVEN a Router with registered routes |
| 868 | + app = ApiGatewayResolver() |
| 869 | + router = Router() |
| 870 | + |
| 871 | + @router.get("/my/path") |
| 872 | + def foo(): |
| 873 | + return {} |
| 874 | + |
| 875 | + app.include_router(router) |
| 876 | + # WHEN calling the event handler after applying routes from router object |
| 877 | + result = app(LOAD_GW_EVENT, {}) |
| 878 | + |
| 879 | + # THEN process event correctly |
| 880 | + assert result["statusCode"] == 200 |
| 881 | + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON |
| 882 | + |
| 883 | + |
| 884 | +def test_api_gateway_app_router_with_params(): |
| 885 | + # GIVEN a Router with registered routes |
| 886 | + app = ApiGatewayResolver() |
| 887 | + router = Router() |
| 888 | + req = "foo" |
| 889 | + event = deepcopy(LOAD_GW_EVENT) |
| 890 | + event["resource"] = "/accounts/{account_id}" |
| 891 | + event["path"] = f"/accounts/{req}" |
| 892 | + lambda_context = {} |
| 893 | + |
| 894 | + @router.route(rule="/accounts/<account_id>", method=["GET", "POST"]) |
| 895 | + def foo(account_id): |
| 896 | + assert router.current_event.raw_event == event |
| 897 | + assert router.lambda_context == lambda_context |
| 898 | + assert account_id == f"{req}" |
| 899 | + return {} |
| 900 | + |
| 901 | + app.include_router(router) |
| 902 | + # WHEN calling the event handler after applying routes from router object |
| 903 | + result = app(event, lambda_context) |
| 904 | + |
| 905 | + # THEN process event correctly |
| 906 | + assert result["statusCode"] == 200 |
| 907 | + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON |
| 908 | + |
| 909 | + |
| 910 | +def test_api_gateway_app_router_with_prefix(): |
| 911 | + # GIVEN a Router with registered routes |
| 912 | + # AND a prefix is defined during the registration |
| 913 | + app = ApiGatewayResolver() |
| 914 | + router = Router() |
| 915 | + |
| 916 | + @router.get(rule="/path") |
| 917 | + def foo(): |
| 918 | + return {} |
| 919 | + |
| 920 | + app.include_router(router, prefix="/my") |
| 921 | + # WHEN calling the event handler after applying routes from router object |
| 922 | + result = app(LOAD_GW_EVENT, {}) |
| 923 | + |
| 924 | + # THEN process event correctly |
| 925 | + assert result["statusCode"] == 200 |
| 926 | + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON |
| 927 | + |
| 928 | + |
| 929 | +def test_api_gateway_app_router_with_prefix_equals_path(): |
| 930 | + # GIVEN a Router with registered routes |
| 931 | + # AND a prefix is defined during the registration |
| 932 | + app = ApiGatewayResolver() |
| 933 | + router = Router() |
| 934 | + |
| 935 | + @router.get(rule="/") |
| 936 | + def foo(): |
| 937 | + return {} |
| 938 | + |
| 939 | + app.include_router(router, prefix="/my/path") |
| 940 | + # WHEN calling the event handler after applying routes from router object |
| 941 | + # WITH the request path matching the registration prefix |
| 942 | + result = app(LOAD_GW_EVENT, {}) |
| 943 | + |
| 944 | + # THEN process event correctly |
| 945 | + assert result["statusCode"] == 200 |
| 946 | + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON |
| 947 | + |
| 948 | + |
| 949 | +def test_api_gateway_app_router_with_different_methods(): |
| 950 | + # GIVEN a Router with all the possible HTTP methods |
| 951 | + app = ApiGatewayResolver() |
| 952 | + router = Router() |
| 953 | + |
| 954 | + @router.get("/not_matching_get") |
| 955 | + def get_func(): |
| 956 | + raise RuntimeError() |
| 957 | + |
| 958 | + @router.post("/no_matching_post") |
| 959 | + def post_func(): |
| 960 | + raise RuntimeError() |
| 961 | + |
| 962 | + @router.put("/no_matching_put") |
| 963 | + def put_func(): |
| 964 | + raise RuntimeError() |
| 965 | + |
| 966 | + @router.delete("/no_matching_delete") |
| 967 | + def delete_func(): |
| 968 | + raise RuntimeError() |
| 969 | + |
| 970 | + @router.patch("/no_matching_patch") |
| 971 | + def patch_func(): |
| 972 | + raise RuntimeError() |
| 973 | + |
| 974 | + app.include_router(router) |
| 975 | + |
| 976 | + # Also check check the route configurations |
| 977 | + routes = app._routes |
| 978 | + assert len(routes) == 5 |
| 979 | + for route in routes: |
| 980 | + if route.func == get_func: |
| 981 | + assert route.method == "GET" |
| 982 | + elif route.func == post_func: |
| 983 | + assert route.method == "POST" |
| 984 | + elif route.func == put_func: |
| 985 | + assert route.method == "PUT" |
| 986 | + elif route.func == delete_func: |
| 987 | + assert route.method == "DELETE" |
| 988 | + elif route.func == patch_func: |
| 989 | + assert route.method == "PATCH" |
| 990 | + |
| 991 | + # WHEN calling the handler |
| 992 | + # THEN return a 404 |
| 993 | + result = app(LOAD_GW_EVENT, None) |
| 994 | + assert result["statusCode"] == 404 |
| 995 | + # AND cors headers are not returned |
| 996 | + assert "Access-Control-Allow-Origin" not in result["headers"] |
0 commit comments