Add If-Match support with 412 response for precondition failures, imp…#155
Conversation
…rove ETag parsing. Add tests for If-Match
📝 WalkthroughWalkthroughThis pull request introduces If-Match HTTP precondition handling to static file responses, returning 412 Precondition Failed when the ETag does not match. Additionally, ETag normalization logic is refined to properly strip weak indicators and quotes before comparison, and the precondition check order is adjusted to evaluate If-Match before If-None-Match. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/test/java/org/juv25d/handler/StaticFileHandlerTest.java (1)
131-131: Consider adding test for If-Match: * wildcard.The current tests don't cover the
If-Match: *case, which should return 200 when the resource exists. This is an important edge case per RFC 7232.🧪 Suggested additional test
`@Test` void shouldReturn200WhenIfMatchIsWildcard() { Map<String, String> headers = new HashMap<>(); headers.put("If-Match", "*"); HttpRequest request = createRequest("GET", "/index.html", headers); HttpResponse response = StaticFileHandler.handle(request); assertThat(response.statusCode()).isEqualTo(200); assertThat(response.statusText()).isEqualTo("OK"); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/juv25d/handler/StaticFileHandlerTest.java` at line 131, Add a unit test in StaticFileHandlerTest that verifies the If-Match: * wildcard returns 200 when the resource exists: create a headers map with "If-Match" -> "*" (use the existing createRequest helper), call StaticFileHandler.handle(request), and assert response.statusCode() == 200 and response.statusText().equals("OK"); name the test e.g. shouldReturn200WhenIfMatchIsWildcard and place it alongside the other handler tests to cover this RFC 7232 edge case.src/main/java/org/juv25d/handler/StaticFileHandler.java (1)
251-273: Consider renaming method for clarity.The method
etagMatchesis now used for both If-Match (line 81) and If-None-Match (line 115) header processing, but the parameter is still namedifNoneMatchHeader. This could cause confusion for future maintainers.♻️ Suggested rename for clarity
- private static boolean etagMatches(`@Nullable` String ifNoneMatchHeader, String currentEtag) { - if (ifNoneMatchHeader == null || ifNoneMatchHeader.isBlank()) { + private static boolean etagMatches(`@Nullable` String headerValue, String currentEtag) { + if (headerValue == null || headerValue.isBlank()) { return false; } - String value = ifNoneMatchHeader.trim(); + String value = headerValue.trim();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main/java/org/juv25d/handler/StaticFileHandler.java` around lines 251 - 273, Rename the etagMatches method parameter from ifNoneMatchHeader to a neutral name (e.g., headerValue or matchHeader) and optionally rename the method to something unambiguous like etagMatchesHeader or matchesEtagHeader; update all call sites that use etagMatches (including the If-Match and If-None-Match usages) to pass the new parameter name and new method name if changed, and ensure opaqueTag(...) references remain correct inside the method.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/main/java/org/juv25d/handler/StaticFileHandler.java`:
- Around line 251-273: Rename the etagMatches method parameter from
ifNoneMatchHeader to a neutral name (e.g., headerValue or matchHeader) and
optionally rename the method to something unambiguous like etagMatchesHeader or
matchesEtagHeader; update all call sites that use etagMatches (including the
If-Match and If-None-Match usages) to pass the new parameter name and new method
name if changed, and ensure opaqueTag(...) references remain correct inside the
method.
In `@src/test/java/org/juv25d/handler/StaticFileHandlerTest.java`:
- Line 131: Add a unit test in StaticFileHandlerTest that verifies the If-Match:
* wildcard returns 200 when the resource exists: create a headers map with
"If-Match" -> "*" (use the existing createRequest helper), call
StaticFileHandler.handle(request), and assert response.statusCode() == 200 and
response.statusText().equals("OK"); name the test e.g.
shouldReturn200WhenIfMatchIsWildcard and place it alongside the other handler
tests to cover this RFC 7232 edge case.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/main/java/org/juv25d/handler/StaticFileHandler.javasrc/test/java/org/juv25d/handler/StaticFileHandlerTest.java
Add If-Match support with 412 response for precondition failures, improve ETag parsing. Add tests for If-Match
Summary by CodeRabbit
Bug Fixes
Tests