use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class RequestLoggingFilterTests method payloadInputStream.
@Test
void payloadInputStream() throws Exception {
filter.setIncludePayload(true);
byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8);
request.setContent(requestBody);
FilterChain filterChain = (filterRequest, filterResponse) -> {
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
assertThat(buf).isEqualTo(requestBody);
};
filter.doFilter(request, response, filterChain);
assertThat(filter.afterRequestMessage).contains("Hello World");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class RequestLoggingFilterTests method payloadMaxLength.
@Test
void payloadMaxLength() throws Exception {
filter.setIncludePayload(true);
filter.setMaxPayloadLength(3);
byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8);
request.setContent(requestBody);
FilterChain filterChain = (filterRequest, filterResponse) -> {
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
assertThat(buf).isEqualTo(requestBody);
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class);
assertThat(wrapper.getContentAsByteArray()).isEqualTo("Hel".getBytes(StandardCharsets.UTF_8));
};
filter.doFilter(request, response, filterChain);
assertThat(filter.afterRequestMessage).contains("Hel");
assertThat(filter.afterRequestMessage).doesNotContain("Hello World");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class RequestLoggingFilterTests method payloadReader.
@Test
void payloadReader() throws Exception {
filter.setIncludePayload(true);
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
FilterChain filterChain = (filterRequest, filterResponse) -> {
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
String buf = FileCopyUtils.copyToString(filterRequest.getReader());
assertThat(buf).isEqualTo(requestBody);
};
filter.doFilter(request, response, filterChain);
assertThat(filter.afterRequestMessage).contains(requestBody);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterWriter.
@Test
public void filterWriter() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\"";
request.addHeader("If-None-Match", etag);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(filterRequest).as("Invalid request passed").isEqualTo(request);
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
String responseBody = "Hello World";
FileCopyUtils.copy(responseBody, filterResponse.getWriter());
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(304);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\"");
assertThat(response.containsHeader("Content-Length")).as("Response has Content-Length header").isFalse();
byte[] expecteds = new byte[0];
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(expecteds);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterSendError.
@Test
public void filterSendError() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
MockHttpServletResponse response = new MockHttpServletResponse();
final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8);
FilterChain filterChain = (filterRequest, filterResponse) -> {
assertThat(filterRequest).as("Invalid request passed").isEqualTo(request);
response.setContentLength(100);
FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
((HttpServletResponse) filterResponse).sendError(HttpServletResponse.SC_FORBIDDEN);
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(403);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isNull();
assertThat(response.getContentLength()).as("Invalid Content-Length header").isEqualTo(100);
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(responseBody);
}
Aggregations