Search in sources :

Example 1 with ServletRegistration

use of jakarta.servlet.ServletRegistration in project spring-security by spring-projects.

the class InterceptUrlConfigTests method mockServletContext.

private MockServletContext mockServletContext(String servletPath) {
    MockServletContext servletContext = spy(new MockServletContext());
    final ServletRegistration registration = mock(ServletRegistration.class);
    given(registration.getMappings()).willReturn(Collections.singleton(servletPath));
    Answer<Map<String, ? extends ServletRegistration>> answer = (invocation) -> Collections.singletonMap("spring", registration);
    given(servletContext.getServletRegistrations()).willAnswer(answer);
    return servletContext;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) ServletRegistration(jakarta.servlet.ServletRegistration) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Mockito.spy(org.mockito.Mockito.spy) MockMvc(org.springframework.test.web.servlet.MockMvc) Answer(org.mockito.stubbing.Answer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) BDDMockito.given(org.mockito.BDDMockito.given) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MockServletContext(org.springframework.mock.web.MockServletContext) RestController(org.springframework.web.bind.annotation.RestController) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) MockMvcRequestBuilders.patch(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch) Test(org.junit.jupiter.api.Test) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) RequestPostProcessor(org.springframework.test.web.servlet.request.RequestPostProcessor) SpringTestContext(org.springframework.security.config.test.SpringTestContext) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) SpringTestContextExtension(org.springframework.security.config.test.SpringTestContextExtension) SecurityMockMvcRequestPostProcessors.httpBasic(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ServletRegistration(jakarta.servlet.ServletRegistration) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 2 with ServletRegistration

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

the class ServletHttpHandlerAdapter method getServletPath.

private String getServletPath(ServletConfig config) {
    String name = config.getServletName();
    ServletRegistration registration = config.getServletContext().getServletRegistration(name);
    if (registration == null) {
        throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
    }
    Collection<String> mappings = registration.getMappings();
    if (mappings.size() == 1) {
        String mapping = mappings.iterator().next();
        if (mapping.equals("/")) {
            return "";
        }
        if (mapping.endsWith("/*")) {
            String path = mapping.substring(0, mapping.length() - 2);
            if (!path.isEmpty() && logger.isDebugEnabled()) {
                logger.debug("Found servlet mapping prefix '" + path + "' for '" + name + "'");
            }
            return path;
        }
    }
    throw new IllegalArgumentException("Expected a single Servlet mapping: " + "either the default Servlet mapping (i.e. '/'), " + "or a path based mapping (e.g. '/*', '/foo/*'). " + "Actual mappings: " + mappings + " for Servlet '" + name + "'");
}
Also used : ServletRegistration(jakarta.servlet.ServletRegistration)

Aggregations

ServletRegistration (jakarta.servlet.ServletRegistration)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Test (org.junit.jupiter.api.Test)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 BDDMockito.given (org.mockito.BDDMockito.given)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.spy (org.mockito.Mockito.spy)1 Answer (org.mockito.stubbing.Answer)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 BeanDefinitionParsingException (org.springframework.beans.factory.parsing.BeanDefinitionParsingException)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 SpringTestContext (org.springframework.security.config.test.SpringTestContext)1 SpringTestContextExtension (org.springframework.security.config.test.SpringTestContextExtension)1 SecurityMockMvcRequestPostProcessors.httpBasic (org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic)1 MockMvc (org.springframework.test.web.servlet.MockMvc)1 MockMvcRequestBuilders.get (org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get)1 MockMvcRequestBuilders.patch (org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch)1 MockMvcRequestBuilders.post (org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post)1