use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNoMatch.
@Test
public void resolveViewNoMatch() throws Exception {
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xml");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn("application/pdf");
View result = viewResolver.resolveViewName(viewName, locale);
assertNull("Invalid view", result);
}
use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNameFilenameDefaultView.
@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
request.setRequestURI("/test.json");
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));
ViewResolver viewResolverMock1 = mock(ViewResolver.class);
ViewResolver viewResolverMock2 = mock(ViewResolver.class);
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
View viewMock1 = mock(View.class, "application_xml");
View viewMock2 = mock(View.class, "text_html");
View viewMock3 = mock(View.class, "application_json");
List<View> defaultViews = new ArrayList<>();
defaultViews.add(viewMock3);
viewResolver.setDefaultViews(defaultViews);
viewResolver.afterPropertiesSet();
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
given(viewResolverMock1.resolveViewName(viewName + ".json", locale)).willReturn(null);
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
given(viewResolverMock2.resolveViewName(viewName + ".json", locale)).willReturn(null);
given(viewMock1.getContentType()).willReturn("application/xml");
given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
given(viewMock3.getContentType()).willReturn("application/json");
View result = viewResolver.resolveViewName(viewName, locale);
assertSame("Invalid view", viewMock3, result);
}
use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewContentTypeNull.
@Test
public void resolveViewContentTypeNull() throws Exception {
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xml");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn(null);
View result = viewResolver.resolveViewName(viewName, locale);
assertNull("Invalid view", result);
}
use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNameRedirectView.
@Test
public void resolveViewNameRedirectView() throws Exception {
request.addHeader("Accept", "application/json");
request.setRequestURI("/test");
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
webAppContext.setServletContext(new MockServletContext());
webAppContext.refresh();
UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
urlViewResolver.setApplicationContext(webAppContext);
ViewResolver xmlViewResolver = mock(ViewResolver.class);
viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));
View xmlView = mock(View.class, "application_xml");
View jsonView = mock(View.class, "application_json");
viewResolver.setDefaultViews(Arrays.asList(jsonView));
viewResolver.afterPropertiesSet();
String viewName = "redirect:anotherTest";
Locale locale = Locale.ENGLISH;
given(xmlViewResolver.resolveViewName(viewName, locale)).willReturn(xmlView);
given(jsonView.getContentType()).willReturn("application/json");
View actualView = viewResolver.resolveViewName(viewName, locale);
assertEquals("Invalid view", RedirectView.class, actualView.getClass());
}
use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNoMatchUseUnacceptableStatus.
@Test
public void resolveViewNoMatchUseUnacceptableStatus() throws Exception {
viewResolver.setUseNotAcceptableStatusCode(true);
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xml");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn("application/pdf");
View result = viewResolver.resolveViewName(viewName, locale);
assertNotNull("Invalid view", result);
MockHttpServletResponse response = new MockHttpServletResponse();
result.render(null, request, response);
assertEquals("Invalid status code set", 406, response.getStatus());
}
Aggregations