Search in sources :

Example 6 with ViewResolver

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);
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 7 with ViewResolver

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);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) PathExtensionContentNegotiationStrategy(org.springframework.web.accept.PathExtensionContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 8 with ViewResolver

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);
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 9 with ViewResolver

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());
}
Also used : Locale(java.util.Locale) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 10 with ViewResolver

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());
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ViewResolver (org.springframework.web.servlet.ViewResolver)21 Test (org.junit.Test)18 View (org.springframework.web.servlet.View)17 Locale (java.util.Locale)14 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)7 MediaType (org.springframework.http.MediaType)6 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)4 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)4 ViewResolverComposite (org.springframework.web.servlet.view.ViewResolverComposite)4 ArrayList (java.util.ArrayList)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 BeanNameViewResolver (org.springframework.web.servlet.view.BeanNameViewResolver)3 FreeMarkerViewResolver (org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver)3 GroovyMarkupViewResolver (org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver)3 ScriptTemplateViewResolver (org.springframework.web.servlet.view.script.ScriptTemplateViewResolver)3 TilesViewResolver (org.springframework.web.servlet.view.tiles3.TilesViewResolver)3 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)2 List (java.util.List)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1