Search in sources :

Example 1 with MockSessionCookieConfig

use of org.springframework.mock.web.MockSessionCookieConfig in project spring-boot by spring-projects.

the class MockServletWebServer method initialize.

private void initialize() {
    try {
        this.servletContext = mock(ServletContext.class);
        lenient().doAnswer((invocation) -> {
            RegisteredServlet registeredServlet = new RegisteredServlet(invocation.getArgument(1));
            MockServletWebServer.this.registeredServlets.add(registeredServlet);
            return registeredServlet.getRegistration();
        }).when(this.servletContext).addServlet(anyString(), any(Servlet.class));
        lenient().doAnswer((invocation) -> {
            RegisteredFilter registeredFilter = new RegisteredFilter(invocation.getArgument(1));
            MockServletWebServer.this.registeredFilters.add(registeredFilter);
            return registeredFilter.getRegistration();
        }).when(this.servletContext).addFilter(anyString(), any(Filter.class));
        final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig();
        given(this.servletContext.getSessionCookieConfig()).willReturn(sessionCookieConfig);
        final Map<String, String> initParameters = new HashMap<>();
        lenient().doAnswer((invocation) -> {
            initParameters.put(invocation.getArgument(0), invocation.getArgument(1));
            return null;
        }).when(this.servletContext).setInitParameter(anyString(), anyString());
        given(this.servletContext.getInitParameterNames()).willReturn(Collections.enumeration(initParameters.keySet()));
        lenient().doAnswer((invocation) -> initParameters.get(invocation.getArgument(0))).when(this.servletContext).getInitParameter(anyString());
        given(this.servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
        for (Initializer initializer : this.initializers) {
            initializer.onStartup(this.servletContext);
        }
    } catch (ServletException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HashMap(java.util.HashMap) MockSessionCookieConfig(org.springframework.mock.web.MockSessionCookieConfig) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServletException(jakarta.servlet.ServletException) Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) Servlet(jakarta.servlet.Servlet) SessionCookieConfig(jakarta.servlet.SessionCookieConfig) MockSessionCookieConfig(org.springframework.mock.web.MockSessionCookieConfig)

Aggregations

Filter (jakarta.servlet.Filter)1 Servlet (jakarta.servlet.Servlet)1 ServletContext (jakarta.servlet.ServletContext)1 ServletException (jakarta.servlet.ServletException)1 SessionCookieConfig (jakarta.servlet.SessionCookieConfig)1 HashMap (java.util.HashMap)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 MockSessionCookieConfig (org.springframework.mock.web.MockSessionCookieConfig)1