Skip to content

update: ignore unexpected body on GET/DELETE/OPTIONS/HEAD/TRACE #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ private List<OpenApiViolation> validateRequest(
}

private static String readBodyCatchingException(MultiReadContentCachingRequestWrapper request) {
if (!"POST".equalsIgnoreCase(request.getMethod())
&& !"PUT".equalsIgnoreCase(request.getMethod())
&& !"PATCH".equalsIgnoreCase(request.getMethod())) {
return null;
}

try {
return StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private static void mockRequestAttributes(ServletRequest request, HashMap<String

protected MockSetupData mockSetup(MockConfiguration configuration) {
var request = mock(MultiReadContentCachingRequestWrapper.class);
when(request.getMethod()).thenReturn(configuration.requestMethod);
var response = mock(ContentCachingResponseWrapper.class);
var cachingRequest = mockContentCachingRequest(request, configuration);
var cachingResponse = mockContentCachingResponse(response, configuration);
Expand Down Expand Up @@ -108,6 +109,7 @@ private MultiReadContentCachingRequestWrapper mockContentCachingRequest(
MockConfiguration configuration
) {
var cachingRequest = mock(MultiReadContentCachingRequestWrapper.class);
when(cachingRequest.getMethod()).thenReturn(configuration.requestMethod);
when(contentCachingWrapperFactory.buildContentCachingRequestWrapper(request)).thenReturn(cachingRequest);
if (configuration.requestBody != null) {
try {
Expand Down Expand Up @@ -160,6 +162,8 @@ protected static class MockConfiguration {
@Builder.Default
private String requestBody = REQUEST_BODY;
@Builder.Default
private String requestMethod = "POST";
@Builder.Default
private String responseBody = RESPONSE_BODY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ public void testShouldFailOnRequestViolationWithViolation() {
verifyNoResponseValidation();
}

@Test
public void testShouldIgnoreBodyOnGetRequests() {
var mockData = mockSetup(MockConfiguration.builder().requestBody("{\"field\": 1}}").requestMethod("GET").build());

httpInterceptor.preHandle(mockData.request(), mockData.response(), new Object());
httpInterceptor.postHandle(mockData.request(), mockData.response(), new Object(), null);
httpInterceptor.afterCompletion(mockData.request(), mockData.response(), new Object(), null);

verifyRequestValidatedAsync(mockData, null);
verifyResponseValidatedAsync(mockData);
}

@Test
public void testShouldFailOnResponseViolationWithViolation() {
var mockData = mockSetup(MockConfiguration.builder().shouldFailOnResponseViolation(true).build());
Expand Down Expand Up @@ -140,10 +152,14 @@ private void verifyNoResponseValidation() {
}

private void verifyRequestValidatedAsync(MockSetupData mockData) {
verifyRequestValidatedAsync(mockData, REQUEST_BODY);
}

private void verifyRequestValidatedAsync(MockSetupData mockData, String requestBody) {
verify(validator).validateRequestObjectAsync(
eq(mockData.requestMetaData()),
eq(mockData.responseMetaData()),
eq(REQUEST_BODY),
eq(requestBody),
eq(openApiViolationHandler)
);
}
Expand Down