Search in sources :

Example 91 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class BaseViewTests method renderWithStaticAttributesNoCollision.

/**
 * Test attribute passing, NOT CSV parsing.
 */
@Test
public void renderWithStaticAttributesNoCollision() 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("foo", "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);
    checkContainsAll(model, tv.model);
    checkContainsAll(p, tv.model);
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Properties(java.util.Properties) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 92 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class BaseViewTests method pathVarsOverrideStaticAttributes.

@Test
public void pathVarsOverrideStaticAttributes() 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> pathVars = new HashMap<>();
    pathVars.put("one", new HashMap<>());
    pathVars.put("two", new Object());
    request.setAttribute(View.PATH_VARIABLES, pathVars);
    tv.render(new HashMap<>(), request, response);
    checkContainsAll(pathVars, tv.model);
    assertThat(tv.model.size()).isEqualTo(3);
    assertThat(tv.model.get("something")).isEqualTo("else");
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Properties(java.util.Properties) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 93 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class BaseViewTests method renderWithoutStaticAttributes.

@Test
public void renderWithoutStaticAttributes() 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);
    // Check superclass handles duplicate init
    tv.setApplicationContext(wac);
    tv.setApplicationContext(wac);
    Map<String, Object> model = new HashMap<>();
    model.put("foo", "bar");
    model.put("something", new Object());
    tv.render(model, request, response);
    checkContainsAll(model, tv.model);
    assertThat(tv.initialized).isTrue();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 94 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.

the class CharacterEncodingFilterTests method forceEncodingAlwaysSetsEncoding.

@Test
public void forceEncodingAlwaysSetsEncoding() 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);
    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(response).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 95 with HttpServletRequest

use of jakarta.servlet.http.HttpServletRequest 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)

Aggregations

HttpServletRequest (jakarta.servlet.http.HttpServletRequest)289 Test (org.junit.jupiter.api.Test)160 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)93 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)88 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)67 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)44 Authentication (org.springframework.security.core.Authentication)31 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)31 Test (org.junit.Test)28 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)26 IOException (java.io.IOException)21 ServletException (jakarta.servlet.ServletException)20 HttpServlet (jakarta.servlet.http.HttpServlet)19 HashMap (java.util.HashMap)17 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)16 FilterChain (jakarta.servlet.FilterChain)15 HttpSession (jakarta.servlet.http.HttpSession)14 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)14 ServletRequest (jakarta.servlet.ServletRequest)13