Search in sources :

Example 21 with ViewResolver

use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method resolveViewNameAcceptHeaderDefaultView.

@Test
public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
    request.addHeader("Accept", "application/json");
    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(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
    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) ArrayList(java.util.ArrayList) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 22 with ViewResolver

use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method resolveViewNameAcceptHeaderSortByQuality.

// SPR-9160
@Test
public void resolveViewNameAcceptHeaderSortByQuality() throws Exception {
    request.addHeader("Accept", "text/plain;q=0.5, application/json");
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(new HeaderContentNegotiationStrategy()));
    ViewResolver htmlViewResolver = mock(ViewResolver.class);
    ViewResolver jsonViewResolver = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));
    View htmlView = mock(View.class, "text_html");
    View jsonViewMock = mock(View.class, "application_json");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(htmlViewResolver.resolveViewName(viewName, locale)).willReturn(htmlView);
    given(jsonViewResolver.resolveViewName(viewName, locale)).willReturn(jsonViewMock);
    given(htmlView.getContentType()).willReturn("text/html");
    given(jsonViewMock.getContentType()).willReturn("application/json");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", jsonViewMock, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Aggregations

ViewResolver (org.springframework.web.servlet.ViewResolver)22 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