-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: do not report violation on blocked request (#66)
- Loading branch information
Showing
14 changed files
with
364 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ava/com/getyourguide/openapi/validation/filter/MultiReadContentCachingRequestWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.getyourguide.openapi.validation.filter; | ||
|
||
import jakarta.servlet.ReadListener; | ||
import jakarta.servlet.ServletInputStream; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
|
||
public class MultiReadContentCachingRequestWrapper extends ContentCachingRequestWrapper { | ||
|
||
public MultiReadContentCachingRequestWrapper(HttpServletRequest request) { | ||
super(request); | ||
} | ||
|
||
public MultiReadContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit) { | ||
super(request, contentCacheLimit); | ||
} | ||
|
||
@Override | ||
public ServletInputStream getInputStream() throws IOException { | ||
var inputStream = super.getInputStream(); | ||
if (inputStream.isFinished()) { | ||
return new CachedServletInputStream(getContentAsByteArray()); | ||
} | ||
|
||
return inputStream; | ||
} | ||
|
||
@Override | ||
public BufferedReader getReader() throws IOException { | ||
return new BufferedReader(new InputStreamReader(getInputStream())); | ||
} | ||
|
||
private static class CachedServletInputStream extends ServletInputStream { | ||
private final ByteArrayInputStream buffer; | ||
|
||
public CachedServletInputStream(byte[] contents) { | ||
this.buffer = new ByteArrayInputStream(contents); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return buffer.read(); | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return buffer.available() == 0; | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setReadListener(ReadListener listener) { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
.../java/com/getyourguide/openapi/validation/integration/FailOnViolationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.getyourguide.openapi.validation.integration; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.getyourguide.openapi.validation.integration.controller.DefaultRestController; | ||
import com.getyourguide.openapi.validation.test.TestViolationLogger; | ||
import java.util.Optional; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@SpringBootTest(properties = { | ||
"openapi.validation.should-fail-on-request-violation=true", | ||
"openapi.validation.should-fail-on-response-violation=true", | ||
}) | ||
@AutoConfigureMockMvc | ||
@ExtendWith(SpringExtension.class) | ||
public class FailOnViolationIntegrationTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private TestViolationLogger openApiViolationLogger; | ||
|
||
@SpyBean | ||
private DefaultRestController defaultRestController; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
openApiViolationLogger.clearViolations(); | ||
} | ||
|
||
@Test | ||
public void whenValidRequestThenReturnSuccessfully() throws Exception { | ||
mockMvc.perform(post("/test") | ||
.content("{ \"value\": \"testing\", \"responseStatusCode\": 200 }").contentType(MediaType.APPLICATION_JSON)) | ||
.andExpectAll( | ||
status().isOk(), | ||
jsonPath("$.value").value("testing") | ||
); | ||
Thread.sleep(100); | ||
|
||
assertEquals(0, openApiViolationLogger.getViolations().size()); | ||
verify(defaultRestController).postTest(any()); | ||
} | ||
|
||
@Test | ||
public void whenInvalidRequestThenReturn400AndNoViolationLogged() throws Exception { | ||
mockMvc.perform(post("/test").content("{ \"value\": 1 }").contentType(MediaType.APPLICATION_JSON)) | ||
.andExpectAll( | ||
status().is4xxClientError(), | ||
content().string(Matchers.blankOrNullString()) | ||
); | ||
Thread.sleep(100); | ||
|
||
assertEquals(0, openApiViolationLogger.getViolations().size()); | ||
verify(defaultRestController, never()).postTest(any()); | ||
// TODO check that something else gets logged? | ||
} | ||
|
||
@Test | ||
public void whenInvalidResponseThenReturn500AndViolationLogged() throws Exception { | ||
mockMvc.perform(get("/test").queryParam("value", "invalid-response-value!")) | ||
.andExpectAll( | ||
status().is5xxServerError(), | ||
content().string(Matchers.blankOrNullString()) | ||
); | ||
Thread.sleep(100); | ||
|
||
assertEquals(1, openApiViolationLogger.getViolations().size()); | ||
var violation = openApiViolationLogger.getViolations().get(0); | ||
assertEquals("validation.response.body.schema.pattern", violation.getRule()); | ||
assertEquals(Optional.of(200), violation.getResponseStatus()); | ||
assertEquals(Optional.of("/value"), violation.getInstance()); | ||
verify(defaultRestController).getTest(any(), any(), any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.