Search in sources :

Example 46 with HttpServletResponse

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);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 47 with HttpServletResponse

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\"}" + "]");
}
Also used : Test(org.junit.jupiter.api.Test) WebUtils(org.springframework.web.util.WebUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) FilterChain(jakarta.servlet.FilterChain) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 48 with HttpServletResponse

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");
}
Also used : Test(org.junit.jupiter.api.Test) StreamUtils(org.springframework.util.StreamUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 49 with HttpServletResponse

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);
}
Also used : Test(org.junit.jupiter.api.Test) StreamUtils(org.springframework.util.StreamUtils) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) FileCopyUtils(org.springframework.util.FileCopyUtils) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 50 with HttpServletResponse

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");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Row(org.apache.poi.ss.usermodel.Row) View(org.springframework.web.servlet.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletResponse (jakarta.servlet.http.HttpServletResponse)118 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)76 Test (org.junit.jupiter.api.Test)47 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)34 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)31 FilterChain (jakarta.servlet.FilterChain)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 ServletException (jakarta.servlet.ServletException)14 StandardCharsets (java.nio.charset.StandardCharsets)14 HttpServlet (jakarta.servlet.http.HttpServlet)13 IOException (java.io.IOException)12 HashMap (java.util.HashMap)12 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 Test (org.junit.Test)10 Authentication (org.springframework.security.core.Authentication)10 FileCopyUtils (org.springframework.util.FileCopyUtils)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Collections (java.util.Collections)7