|
| 1 | +package com.networknt.aws.lambda.handler.middleware.proxy; |
| 2 | + |
| 3 | +import com.amazonaws.services.lambda.runtime.Context; |
| 4 | +import com.networknt.aws.lambda.InvocationResponse; |
| 5 | +import com.networknt.aws.lambda.LambdaContext; |
| 6 | +import com.networknt.aws.lambda.LightLambdaExchange; |
| 7 | +import com.networknt.aws.lambda.TestUtils; |
| 8 | +import com.networknt.status.Status; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * Routing tests for {@link LambdaProxyMiddleware}. |
| 16 | + * <p> |
| 17 | + * These exercise the real middleware rather than {@code MockLambdaProxyMiddleware}, which resolves |
| 18 | + * functions through a plain map lookup and therefore cannot cover the matcher logic at all. |
| 19 | + */ |
| 20 | +public class LambdaProxyMiddlewareTest { |
| 21 | + |
| 22 | + private static final Map<String, String> FUNCTIONS = Map.of( |
| 23 | + "/v1/pets@get", "PetsGetFunction", |
| 24 | + "/v1/pets@post", "PetsPostFunction", |
| 25 | + "/v1/pets/{petId}@get", "PetsPetIdGetFunction"); |
| 26 | + |
| 27 | + private static LightLambdaExchange exchangeFor(final String path, final String method) { |
| 28 | + var requestEvent = TestUtils.createTestRequestEvent(); |
| 29 | + requestEvent.setPath(path); |
| 30 | + requestEvent.setHttpMethod(method); |
| 31 | + |
| 32 | + InvocationResponse invocation = InvocationResponse.builder() |
| 33 | + .requestId("12345") |
| 34 | + .event(requestEvent) |
| 35 | + .build(); |
| 36 | + |
| 37 | + Context lambdaContext = new LambdaContext(invocation.getRequestId()); |
| 38 | + final var exchange = new LightLambdaExchange(lambdaContext, null); |
| 39 | + exchange.setInitialRequest(requestEvent); |
| 40 | + return exchange; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testResolveFunctionNameMatchesConfiguredRoute() { |
| 45 | + var middleware = new LambdaProxyMiddleware(FUNCTIONS); |
| 46 | + Assertions.assertEquals("PetsGetFunction", middleware.resolveFunctionName("/v1/pets", "get")); |
| 47 | + Assertions.assertEquals("PetsPostFunction", middleware.resolveFunctionName("/v1/pets", "post")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testResolveFunctionNameMatchesPathTemplate() { |
| 52 | + var middleware = new LambdaProxyMiddleware(FUNCTIONS); |
| 53 | + Assertions.assertEquals("PetsPetIdGetFunction", middleware.resolveFunctionName("/v1/pets/123", "get")); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Regression test for the NPE reported in issue #173. The path matches a configured function but |
| 58 | + * no matcher is registered for the method, which used to dereference a null matcher. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + public void testResolveFunctionNameReturnsNullWhenMethodHasNoMatcher() { |
| 62 | + var middleware = new LambdaProxyMiddleware(FUNCTIONS); |
| 63 | + Assertions.assertNull(middleware.resolveFunctionName("/v1/pets", "delete")); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testResolveFunctionNameReturnsNullWhenPathDoesNotMatch() { |
| 68 | + var middleware = new LambdaProxyMiddleware(FUNCTIONS); |
| 69 | + Assertions.assertNull(middleware.resolveFunctionName("/v1/unknown", "get")); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Regression test for issue #173 at the middleware boundary. An unmapped method must produce a |
| 74 | + * routing failure status rather than throwing. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testExecuteReturnsFailureStatusWhenMethodHasNoMatcher() { |
| 78 | + var middleware = new LambdaProxyMiddleware(FUNCTIONS); |
| 79 | + Status status = Assertions.assertDoesNotThrow( |
| 80 | + () -> middleware.execute(exchangeFor("/v1/pets", "DELETE"))); |
| 81 | + Assertions.assertEquals(LambdaProxyMiddleware.FAILED_TO_INVOKE_LAMBDA, status.getCode()); |
| 82 | + Assertions.assertTrue(status.getDescription().contains("/v1/pets@delete"), |
| 83 | + "expected the failure description to identify the unroutable endpoint, but was: " |
| 84 | + + status.getDescription()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testExecuteReturnsFailureStatusWhenPathDoesNotMatch() { |
| 89 | + var middleware = new LambdaProxyMiddleware(FUNCTIONS); |
| 90 | + Status status = Assertions.assertDoesNotThrow( |
| 91 | + () -> middleware.execute(exchangeFor("/v1/unknown", "GET"))); |
| 92 | + Assertions.assertEquals(LambdaProxyMiddleware.FAILED_TO_INVOKE_LAMBDA, status.getCode()); |
| 93 | + } |
| 94 | +} |
0 commit comments