Add Unit Tests for SecurityHeadersFilter - #141
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds tests for SecurityHeadersFilter and changes the filter to set four security headers only when missing, preserving existing header values and still applying defaults even if the delegated FilterChain throws an IOException. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
rect rgba(200,200,255,0.5)
participant Filter as SecurityHeadersFilter
end
rect rgba(200,255,200,0.5)
participant Chain as FilterChain
end
participant Response as HttpServletResponse
Client->>Filter: HTTP request
Filter->>Chain: chain.doFilter(request, response)
alt Chain succeeds
Chain-->>Filter: returns
else Chain throws IOException
Chain-->>Filter: throws IOException
end
Note over Filter,Response: finally block calls applyDefaultHeader for each header if missing
Filter->>Response: set default headers only when absent
Filter-->>Client: continue or propagate exception
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)
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/test/java/org/juv25d/filter/SecurityHeadersFilterTest.java (1)
37-40: Preferresponse.getHeader(...)for assertions.Line 37–40 and Line 55 bypass
HttpResponse’s case-insensitive header lookup by callingresponse.headers().get(...)directly. UsinggetHeader(...)makes the tests more robust.Also applies to: 55-55
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/juv25d/filter/SecurityHeadersFilterTest.java` around lines 37 - 40, In SecurityHeadersFilterTest update the assertions that currently call response.headers().get(...) to use HttpResponse's case-insensitive lookup method response.getHeader(...); replace each assertEquals(..., response.headers().get("Header-Name")) with assertEquals(..., response.getHeader("Header-Name")) for the X-Content-Type-Options, X-Frame-Options, X-XSS-Protection, Referrer-Policy assertions (and the other occurrence near the end of the test) so the tests use the intended case-insensitive header accessor.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/test/java/org/juv25d/filter/SecurityHeadersFilterTest.java`:
- Around line 44-56: The test should not swallow the IOException and should
validate all security headers on the exception path: replace the try/catch
around filter.doFilter(mockRequest, response, mockChain) with an assertion that
the call throws (e.g., use assertThrows against IOException for
filter.doFilter(...)) so failures propagate, and after capturing the exception
assert that response.headers() contains the expected security headers (not just
"X-Frame-Options") — update should reference the existing test method
shouldAddHeadersEvenIfChainThrowsException, mockChain, filter.doFilter, and
response.headers() to locate and modify the assertions.
---
Nitpick comments:
In `@src/test/java/org/juv25d/filter/SecurityHeadersFilterTest.java`:
- Around line 37-40: In SecurityHeadersFilterTest update the assertions that
currently call response.headers().get(...) to use HttpResponse's
case-insensitive lookup method response.getHeader(...); replace each
assertEquals(..., response.headers().get("Header-Name")) with assertEquals(...,
response.getHeader("Header-Name")) for the X-Content-Type-Options,
X-Frame-Options, X-XSS-Protection, Referrer-Policy assertions (and the other
occurrence near the end of the test) so the tests use the intended
case-insensitive header accessor.
…e tests are not sensitive to header key casing.
DennSel
left a comment
There was a problem hiding this comment.
Great tests already! One addition could be a test case for when headers already exist in the response? Should the filter overwrite existing headers or preserve them? This could be relevant if other filters in the chain set similar security headers.
Tyreviel
left a comment
There was a problem hiding this comment.
Good work! found a small bug that could lead to duplicate headers, (that would require this test suite to have another test if adressed), but maybe that should be a separate issue and PR?
Added a new test case 'shouldPreserveExistingHeaders
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/main/java/org/juv25d/filter/SecurityHeadersFilter.java`:
- Around line 30-35: The comment for applyDefaultHeader says it checks for
missing or empty headers but the code only checks for null; update
applyDefaultHeader to treat empty strings as missing by checking if
res.getHeader(headerName) is null or empty (e.g., trim().isEmpty()) before
calling res.setHeader(headerName, defaultValue), or if empty values are
intentional, change the comment to only mention "missing (null)" to keep them
consistent; refer to the applyDefaultHeader(HttpResponse res, String headerName,
String defaultValue) method and the res.getHeader(headerName) call when making
the change.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/main/java/org/juv25d/filter/SecurityHeadersFilter.javasrc/test/java/org/juv25d/filter/SecurityHeadersFilterTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
- src/test/java/org/juv25d/filter/SecurityHeadersFilterTest.java
DennSel
left a comment
There was a problem hiding this comment.
Great additions, maybe remove the comments made in swedish. Approved.
eafalkens
left a comment
There was a problem hiding this comment.
I think it looks good, great job. Approved.
The tests should verify that security headers are correctly appended to the HttpResponse and that the filter handles various scenarios, such as exceptions in the filter chain.
Currently, the filter is implemented but lacks automated verification, making it difficult to detect if changes in the Pipeline or HttpResponse classes break our security configurations.
Summary by CodeRabbit