Search in sources :

Example 1 with PathExtensionContentNegotiationStrategy

use of cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy in project today-infrastructure by TAKETODAY.

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);
    assertThat(result).as("Invalid view").isSameAs(viewMock3);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) ArrayList(java.util.ArrayList) MediaType(cn.taketoday.http.MediaType) PathExtensionContentNegotiationStrategy(cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 2 with PathExtensionContentNegotiationStrategy

use of cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy in project today-framework by TAKETODAY.

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);
    assertThat(result).as("Invalid view").isSameAs(viewMock3);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) ArrayList(java.util.ArrayList) MediaType(cn.taketoday.http.MediaType) PathExtensionContentNegotiationStrategy(cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 3 with PathExtensionContentNegotiationStrategy

use of cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy in project today-framework by TAKETODAY.

the class ContentNegotiatingViewResolverTests method resolveViewNameFilename.

@Test
@SuppressWarnings("deprecation")
public void resolveViewNameFilename() throws Exception {
    request.setRequestURI("/test.html");
    ContentNegotiationManager manager = new ContentNegotiationManager(new PathExtensionContentNegotiationStrategy());
    ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
    ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
    viewResolver.setContentNegotiationManager(manager);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
    viewResolver.afterPropertiesSet();
    View viewMock1 = mock(View.class, "application_xml");
    View viewMock2 = mock(View.class, "text_html");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
    given(viewResolverMock1.resolveViewName(viewName + ".html", locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName + ".html", locale)).willReturn(viewMock2);
    given(viewMock1.getContentType()).willReturn("application/xml");
    given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isSameAs(viewMock2);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) PathExtensionContentNegotiationStrategy(cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 4 with PathExtensionContentNegotiationStrategy

use of cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy in project today-infrastructure by TAKETODAY.

the class ContentNegotiatingViewResolverTests method resolveViewNameFilename.

@Test
@SuppressWarnings("deprecation")
public void resolveViewNameFilename() throws Exception {
    request.setRequestURI("/test.html");
    ContentNegotiationManager manager = new ContentNegotiationManager(new PathExtensionContentNegotiationStrategy());
    ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
    ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
    viewResolver.setContentNegotiationManager(manager);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
    viewResolver.afterPropertiesSet();
    View viewMock1 = mock(View.class, "application_xml");
    View viewMock2 = mock(View.class, "text_html");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
    given(viewResolverMock1.resolveViewName(viewName + ".html", locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName + ".html", locale)).willReturn(viewMock2);
    given(viewMock1.getContentType()).willReturn("application/xml");
    given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isSameAs(viewMock2);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) PathExtensionContentNegotiationStrategy(cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Aggregations

ContentNegotiationManager (cn.taketoday.web.accept.ContentNegotiationManager)4 PathExtensionContentNegotiationStrategy (cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy)4 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)4 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)4 Locale (java.util.Locale)4 Test (org.junit.jupiter.api.Test)4 MediaType (cn.taketoday.http.MediaType)2 ArrayList (java.util.ArrayList)2