use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ProducesRequestConditionTests method matchByRequestParameter.
@Test
public void matchByRequestParameter() {
String[] produces = { "text/plain" };
String[] headers = {};
ProducesRequestCondition condition = new ProducesRequestCondition(produces, headers);
HttpServletRequest request = new MockHttpServletRequest("GET", "/foo.txt");
assertThat(condition.getMatchingCondition(request)).isNotNull();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ProducesRequestConditionTests method createRequest.
private MockHttpServletRequest createRequest(String... headerValue) {
MockHttpServletRequest request = new MockHttpServletRequest();
Arrays.stream(headerValue).forEach(value -> request.addHeader("Accept", headerValue));
return request;
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest 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.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class MvcNamespaceTests method testInterceptors.
@Test
public void testInterceptors() throws Exception {
loadBeanDefinitions("mvc-config-interceptors.xml");
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertThat(mapping).isNotNull();
mapping.setDefaultHandler(handlerMethod);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
request.setRequestURI("/accounts/12345");
request.addParameter("locale", "en");
request.addParameter("theme", "green");
HandlerExecutionChain chain = mapping.getHandler(request);
assertThat(chain.getInterceptorList().size()).isEqualTo(4);
assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
assertThat(chain.getInterceptorList().get(1) instanceof LocaleChangeInterceptor).isTrue();
assertThat(chain.getInterceptorList().get(2) instanceof ThemeChangeInterceptor).isTrue();
assertThat(chain.getInterceptorList().get(3) instanceof UserRoleAuthorizationInterceptor).isTrue();
request.setRequestURI("/admin/users");
chain = mapping.getHandler(request);
assertThat(chain.getInterceptorList().size()).isEqualTo(2);
request.setRequestURI("/logged/accounts/12345");
chain = mapping.getHandler(request);
assertThat(chain.getInterceptorList().size()).isEqualTo(3);
request.setRequestURI("/foo/logged");
chain = mapping.getHandler(request);
assertThat(chain.getInterceptorList().size()).isEqualTo(3);
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class MvcNamespaceTests method testContentNegotiationManager.
@Test
public void testContentNegotiationManager() throws Exception {
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml");
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setParameter("format", "xml");
NativeWebRequest webRequest = new ServletWebRequest(request);
assertThat(manager.resolveMediaTypes(webRequest)).containsExactly(MediaType.valueOf("application/rss+xml"));
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
assertThat(compositeResolver).isNotNull();
assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(1);
ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
assertThat(resolver.getClass()).isEqualTo(ContentNegotiatingViewResolver.class);
ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
assertThat(cnvr.getContentNegotiationManager()).isSameAs(manager);
}
Aggregations