Skip to content

Add Regression Test Coverage For LambdaProxyMiddleware Route Miss#177

Merged
stevehu merged 1 commit into
masterfrom
176-add-regression-test-coverage-for-lambdaproxymiddleware-route-miss
Jul 21, 2026
Merged

Add Regression Test Coverage For LambdaProxyMiddleware Route Miss#177
stevehu merged 1 commit into
masterfrom
176-add-regression-test-coverage-for-lambdaproxymiddleware-route-miss

Conversation

@stevehu

@stevehu stevehu commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #176.

The NPE fixed in #174 shipped without a test. This adds one that actually reproduces it, plus the seam needed to write it.

Why a seam was needed

LambdaProxyMiddleware's only constructor calls initClient, which builds a real Netty-backed LambdaAsyncClient, so the class could not be instantiated in a unit test. That is why the existing coverage lives in MockLambdaProxyMiddleware — a separate class with its own execute that resolves functions through a plain map lookup:

var functionName = CONFIG.getFunctions().get(path + "@" + method);

The mock never exercises the matcher logic, so no test written against it could have caught #173 or would catch a regression.

Changes

  • resolveFunctionName(path, method) — extracted from execute. Returns null for both miss cases (no matcher for the method, and matcher present but no template matches), so the caller keeps treating every routing miss identically. Pure extraction, no behaviour change.
  • Package-private constructor taking the function map — builds only the route table, no AWS client. The routing-miss path returns before client or config is touched, so a null-client instance is sufficient to test it end to end through execute.
  • LambdaProxyMiddlewareTest — six cases: happy path, path template matching, both miss paths at the resolveFunctionName level, and both miss paths through execute asserting ERR10086 and that the description identifies the unroutable endpoint.
  • MockLambdaProxyMiddlewareTest — the existing assertion used the same unguarded map.get(method).match(path) pattern that NPE Occurs When A Call Path Matches A Function But Not The Method #173 was about, so it would NPE identically for an unmapped method. Now guarded.

Verification

The two miss-path tests were run against the pre-#174 code by temporarily reverting the null guard, and they fail with exactly the reported error:

LambdaProxyMiddlewareTest.testResolveFunctionNameReturnsNullWhenMethodHasNoMatcher:63 » NullPointer
LambdaProxyMiddlewareTest.testExecuteReturnsFailureStatusWhenMethodHasNoMatcher:79 Unexpected exception thrown:
  java.lang.NullPointerException: Cannot invoke "com.networknt.utility.PathTemplateMatcher.match(String)"
  because the return value of "java.util.Map.get(Object)" is null

With the guard restored: Tests run: 79, Failures: 0, Errors: 0, Skipped: 4 — 73 before this PR, 6 added. BUILD SUCCESS, git diff --check clean.

Note for reviewers

The package-private constructor is test-only visibility rather than a public API addition. If you would rather not carry a test seam on the production class, the alternative is making MockLambdaProxyMiddleware delegate to the real routing logic instead of duplicating a divergent copy — happy to switch to that if you prefer.

🤖 Generated with Claude Code

…te miss

Extract the matcher lookup from execute into a package-private
resolveFunctionName so the routing behaviour can be asserted directly, and
add a package-private constructor that builds only the route table without
creating an AWS Lambda client.

Add LambdaProxyMiddlewareTest covering the happy path, path template
matching, and both routing-miss paths. The two miss cases reproduce the
NPE from #173 when run against the pre-#174 code.

Also guard the existing MockLambdaProxyMiddlewareTest assertion, which used
the same unguarded map.get(method).match(path) pattern that #173 was about.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 21, 2026 19:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds regression coverage for the previously reported LambdaProxyMiddleware routing miss NPE (method has no matcher), and introduces a small test seam so the real routing/matcher logic can be exercised without constructing an AWS Lambda client.

Changes:

  • Extracts resolveFunctionName(path, method) from execute() to centralize route resolution and safely handle “no matcher” / “no match” miss cases.
  • Adds a package-private constructor that builds only the route table (no AWS client) to enable unit testing of route-miss behavior end-to-end through execute().
  • Adds new LambdaProxyMiddlewareTest coverage and hardens MockLambdaProxyMiddlewareTest to avoid the same null-deref pattern.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/main/java/com/networknt/aws/lambda/handler/middleware/proxy/LambdaProxyMiddleware.java Adds route-resolution helper and a test-only seam constructor; execute() now uses extracted routing logic.
src/test/java/com/networknt/aws/lambda/handler/middleware/proxy/LambdaProxyMiddlewareTest.java New tests covering match and miss paths (including regression for missing method matcher).
src/test/java/com/networknt/aws/lambda/middleware/proxy/MockLambdaProxyMiddlewareTest.java Guards test setup against missing matcher/match result to avoid null dereferences.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 114 to 115
LOG.trace("Function name: {}", functionName);
var res = this.invokeFunction(this.client, functionName, exchange);
Comment on lines +89 to +93
var middleware = new LambdaProxyMiddleware(FUNCTIONS);
Status status = Assertions.assertDoesNotThrow(
() -> middleware.execute(exchangeFor("/v1/unknown", "GET")));
Assertions.assertEquals(LambdaProxyMiddleware.FAILED_TO_INVOKE_LAMBDA, status.getCode());
}
@stevehu

stevehu commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

The routing extraction preserves existing behavior while adding regression coverage for missing method and path matchers. The focused tests and full Maven test suite pass.

@stevehu
stevehu merged commit 276c7a8 into master Jul 21, 2026
1 check passed
@stevehu
stevehu deleted the 176-add-regression-test-coverage-for-lambdaproxymiddleware-route-miss branch July 21, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Regression Test Coverage For LambdaProxyMiddleware Route Miss

2 participants