use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class CharacterEncodingFilterTests method setForceEncodingOnRequestOnly.
// SPR-14240
@Test
public void setForceEncodingOnRequestOnly() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
request.setCharacterEncoding(ENCODING);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);
CharacterEncodingFilter filter = new CharacterEncodingFilter(ENCODING, true, false);
filter.init(new MockFilterConfig(FILTER_NAME));
filter.doFilter(request, response, filterChain);
verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
verify(request).removeAttribute(filteredName(FILTER_NAME));
verify(request, times(2)).setCharacterEncoding(ENCODING);
verify(response, never()).setCharacterEncoding(ENCODING);
verify(filterChain).doFilter(request, response);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class RequestLoggingFilterTests method allOptions.
@Test
void allOptions() throws Exception {
filter.setIncludeQueryString(true);
filter.setIncludeClientInfo(true);
filter.setIncludeHeaders(true);
filter.setIncludePayload(true);
request.setQueryString("booking=42");
request.setRemoteAddr("4.2.2.2");
request.setSession(new MockHttpSession(null, "42"));
request.setRemoteUser("Arthur");
request.setContentType("application/json");
String requestBody = "{\"msg\": \"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.beforeRequestMessage).isEqualTo("Before request [" + "POST /hotels?booking=42" + ", client=4.2.2.2" + ", session=42" + ", user=Arthur" + ", headers=[Content-Type:\"application/json;charset=ISO-8859-1\", Content-Length:\"22\"]" + "]");
assertThat(filter.afterRequestMessage).isEqualTo("After request [" + "POST /hotels?booking=42" + ", client=4.2.2.2" + ", session=42" + ", user=Arthur" + ", headers=[Content-Type:\"application/json;charset=ISO-8859-1\", Content-Length:\"22\"]" + ", payload={\"msg\": \"Hello World\"}" + "]");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterSendRedirect.
@Test
public void filterSendRedirect() 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).sendRedirect("https://www.google.com");
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(302);
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);
assertThat(response.getRedirectedUrl()).as("Invalid redirect URL").isEqualTo("https://www.google.com");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilterTests method filterNoMatchWeakETag.
@Test
public void filterNoMatchWeakETag() throws Exception {
this.filter.setWriteWeakETag(true);
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);
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
};
filter.doFilter(request, response, filterChain);
assertThat(response.getStatus()).as("Invalid status").isEqualTo(200);
assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("W/\"0b10a8db164e0754105b7a99be72e3fe5\"");
assertThat(response.getContentLength() > 0).as("Invalid Content-Length header").isTrue();
assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(responseBody);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class XlsViewTests method testXls.
@Test
@SuppressWarnings("resource")
public void testXls() throws Exception {
View excelView = new AbstractXlsView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), request, response);
Workbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
Aggregations