use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class BaseViewTests method dynamicModelOverridesStaticAttributesIfCollision.
@Test
public void dynamicModelOverridesStaticAttributesIfCollision() throws Exception {
WebApplicationContext wac = mock(WebApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
Properties p = new Properties();
p.setProperty("one", "bar");
p.setProperty("something", "else");
tv.setAttributes(p);
Map<String, Object> model = new HashMap<>();
model.put("one", new HashMap<>());
model.put("two", new Object());
tv.render(model, request, response);
// Check it contains all
checkContainsAll(model, tv.model);
assertThat(tv.model.size()).isEqualTo(3);
assertThat(tv.model.get("something")).isEqualTo("else");
assertThat(tv.initialized).isTrue();
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class XlsViewTests method testXlsxStreamingView.
@Test
@SuppressWarnings("resource")
public void testXlsxStreamingView() throws Exception {
View excelView = new AbstractXlsxStreamingView() {
@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 XSSFWorkbook(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");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class ShallowEtagHeaderFilter method updateResponse.
private void updateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
ConditionalContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ConditionalContentCachingResponseWrapper.class);
Assert.notNull(wrapper, "ContentCachingResponseWrapper not found");
HttpServletResponse rawResponse = (HttpServletResponse) wrapper.getResponse();
if (isEligibleForEtag(request, wrapper, wrapper.getStatus(), wrapper.getContentInputStream())) {
String eTag = wrapper.getHeader(HttpHeaders.ETAG);
if (!StringUtils.hasText(eTag)) {
eTag = generateETagHeaderValue(wrapper.getContentInputStream(), this.writeWeakETag);
rawResponse.setHeader(HttpHeaders.ETAG, eTag);
}
if (new ServletWebRequest(request, rawResponse).checkNotModified(eTag)) {
return;
}
}
wrapper.copyBodyToResponse();
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class RelativeRedirectFilterTests method sendRedirect.
private void sendRedirect(String location) throws Exception {
MockFilterChain chain = new MockFilterChain();
this.filter.doFilterInternal(new MockHttpServletRequest(), this.response, chain);
HttpServletResponse wrappedResponse = (HttpServletResponse) chain.getResponse();
wrappedResponse.sendRedirect(location);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class RelativeRedirectFilterTests method wrapOnceOnly.
@Test
public void wrapOnceOnly() throws Exception {
HttpServletResponse original = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
this.filter.doFilterInternal(new MockHttpServletRequest(), original, chain);
HttpServletResponse wrapped1 = (HttpServletResponse) chain.getResponse();
assertThat(wrapped1).isNotSameAs(original);
chain.reset();
this.filter.doFilterInternal(new MockHttpServletRequest(), wrapped1, chain);
HttpServletResponse current = (HttpServletResponse) chain.getResponse();
assertThat(current).isSameAs(wrapped1);
chain.reset();
HttpServletResponse wrapped2 = new HttpServletResponseWrapper(wrapped1);
this.filter.doFilterInternal(new MockHttpServletRequest(), wrapped2, chain);
current = (HttpServletResponse) chain.getResponse();
assertThat(current).isSameAs(wrapped2);
}
Aggregations