Search in sources :

Example 56 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class CommonsMultipartResolverTests method withServletContextAndFilterWithCustomBeanName.

@Test
public void withServletContextAndFilterWithCustomBeanName() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    wac.registerSingleton("myMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("multipartResolverBeanName", "myMultipartResolver");
    final List<MultipartFile> files = new ArrayList<>();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest originalRequest, ServletResponse response) {
            if (originalRequest instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest request = (MultipartHttpServletRequest) originalRequest;
                files.addAll(request.getFileMap().values());
            }
        }
    };
    MultipartFilter filter = new MultipartFilter() {

        private boolean invoked = false;

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            super.doFilterInternal(request, response, filterChain);
            super.doFilterInternal(request, response, filterChain);
            if (invoked) {
                throw new ServletException("Should not have been invoked twice");
            }
            invoked = true;
        }
    };
    filter.init(filterConfig);
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    HttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(originalRequest, response, filterChain);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Also used : MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MultipartFilter(org.springframework.web.multipart.support.MultipartFilter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) ArrayList(java.util.ArrayList) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletException(javax.servlet.ServletException) MultipartFile(org.springframework.web.multipart.MultipartFile) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 57 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class CommonsMultipartResolverTests method withServletContextAndFilter.

@Test
public void withServletContextAndFilter() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("class", "notWritable");
    filterConfig.addInitParameter("unknownParam", "someValue");
    final MultipartFilter filter = new MultipartFilter();
    filter.init(filterConfig);
    final List<MultipartFile> files = new ArrayList<>();
    final FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
            files.addAll(request.getFileMap().values());
        }
    };
    FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    filter.doFilter(originalRequest, response, filterChain2);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Also used : MultipartFilter(org.springframework.web.multipart.support.MultipartFilter) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) ArrayList(java.util.ArrayList) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) MultipartFile(org.springframework.web.multipart.MultipartFile) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 58 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class HandlerMappingIntrospectorTests method getMatchable.

@Test
public void getMatchable() throws Exception {
    MutablePropertyValues pvs = new MutablePropertyValues(Collections.singletonMap("urlMap", Collections.singletonMap("/path", new Object())));
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.registerSingleton("hm", SimpleUrlHandlerMapping.class, pvs);
    cxt.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
    MatchableHandlerMapping hm = new HandlerMappingIntrospector(cxt).getMatchableHandlerMapping(request);
    assertEquals(cxt.getBean("hm"), hm);
    assertNull("Attributes changes not ignored", request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 59 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class HandlerMappingIntrospectorTests method defaultHandlerMappings.

@Test
@SuppressWarnings("deprecation")
public void defaultHandlerMappings() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.refresh();
    List<HandlerMapping> actual = new HandlerMappingIntrospector(cxt).getHandlerMappings();
    assertEquals(2, actual.size());
    assertEquals(BeanNameUrlHandlerMapping.class, actual.get(0).getClass());
    assertEquals(RequestMappingHandlerMapping.class, actual.get(1).getClass());
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 60 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class CorsAbstractHandlerMappingTests method setup.

@Before
public void setup() {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    this.handlerMapping = new TestHandlerMapping();
    this.handlerMapping.setApplicationContext(context);
    this.request = new MockHttpServletRequest();
    this.request.setRemoteHost("domain1.com");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Before(org.junit.Before)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5