use of org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter in project spring-framework by spring-projects.
the class MvcNamespaceTests method testResources.
@Test
@SuppressWarnings("deprecation")
public void testResources() throws Exception {
loadBeanDefinitions("mvc-config-resources.xml");
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertThat(adapter).isNotNull();
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
assertThat(handler).isNotNull();
assertThat(handler.getContentNegotiationManager()).isSameAs(manager);
SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(resourceMapping).isNotNull();
assertThat(resourceMapping.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 1);
BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
assertThat(beanNameMapping).isNotNull();
assertThat(beanNameMapping.getOrder()).isEqualTo(2);
ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
assertThat(urlProvider).isNotNull();
Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
List<Class<?>> interceptors = beans.values().stream().map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass()).collect(Collectors.toList());
assertThat(interceptors).contains(ConversionServiceExposingInterceptor.class, ResourceUrlProviderExposingInterceptor.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/resources/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = resourceMapping.getHandler(request);
assertThat(chain).isNotNull();
assertThat(chain.getHandler() instanceof ResourceHttpRequestHandler).isTrue();
MockHttpServletResponse response = new MockHttpServletResponse();
for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
interceptor.preHandle(request, response, chain.getHandler());
}
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertThat((Object) mv).isNull();
}
use of org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter in project spring-framework by spring-projects.
the class MvcNamespaceTests method testDefaultServletHandlerWithOptionalAttributes.
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml");
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertThat(adapter).isNotNull();
DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
assertThat(handler).isNotNull();
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(mapping).isNotNull();
assertThat(mapping.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = mapping.getHandler(request);
assertThat(chain.getHandler() instanceof DefaultServletHttpRequestHandler).isTrue();
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertThat((Object) mv).isNull();
}
use of org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter in project spring-framework by spring-projects.
the class HandlerMethodMappingTests method ambiguousMatchOnPreFlightRequestWithCorsConfig.
// gh-26490
@Test
public void ambiguousMatchOnPreFlightRequestWithCorsConfig() throws Exception {
this.mapping.registerMapping("/f?o", this.handler, this.method1);
this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
request.addHeader(HttpHeaders.ORIGIN, "https://domain.com");
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
MockHttpServletResponse response = new MockHttpServletResponse();
HandlerExecutionChain chain = this.mapping.getHandler(request);
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isInstanceOf(HttpRequestHandler.class);
new HttpRequestHandlerAdapter().handle(request, response, chain.getHandler());
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("https://domain.com");
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isEqualTo("GET");
}
use of org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter in project spring-framework by spring-projects.
the class HandlerMethodMappingTests method ambiguousMatchOnPreFlightRequestWithoutCorsConfig.
// gh-26490
@Test
public void ambiguousMatchOnPreFlightRequestWithoutCorsConfig() throws Exception {
this.mapping.registerMapping("/foo", this.handler, this.method1);
this.mapping.registerMapping("/f??", this.handler, this.method2);
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
request.addHeader(HttpHeaders.ORIGIN, "https://domain.com");
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
MockHttpServletResponse response = new MockHttpServletResponse();
HandlerExecutionChain chain = this.mapping.getHandler(request);
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isInstanceOf(HttpRequestHandler.class);
new HttpRequestHandlerAdapter().handle(request, response, chain.getHandler());
assertThat(response.getStatus()).isEqualTo(403);
}
use of org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter in project spring-framework by spring-projects.
the class MvcNamespaceTests method testDefaultServletHandler.
@Test
public void testDefaultServletHandler() throws Exception {
loadBeanDefinitions("mvc-config-default-servlet.xml");
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertThat(adapter).isNotNull();
DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
assertThat(handler).isNotNull();
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(mapping).isNotNull();
assertThat(mapping.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = mapping.getHandler(request);
assertThat(chain.getHandler() instanceof DefaultServletHttpRequestHandler).isTrue();
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertThat((Object) mv).isNull();
}
Aggregations