Search in sources :

Example 1 with MappingMediaTypeFileExtensionResolver

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithAcceptHeader.

@Test
public void resolveViewNameWithAcceptHeader() throws Exception {
    request.addHeader("Accept", "application/vnd.ms-excel");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    MappingMediaTypeFileExtensionResolver extensionsResolver = new MappingMediaTypeFileExtensionResolver(mapping);
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy());
    manager.addFileExtensionResolvers(extensionsResolver);
    viewResolver.setContentNegotiationManager(manager);
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    View viewMock = mock(View.class, "application_xls");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isSameAs(viewMock);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) MappingMediaTypeFileExtensionResolver(cn.taketoday.web.accept.MappingMediaTypeFileExtensionResolver) MediaType(cn.taketoday.http.MediaType) HeaderContentNegotiationStrategy(cn.taketoday.web.accept.HeaderContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 2 with MappingMediaTypeFileExtensionResolver

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithAcceptHeader.

@Test
public void resolveViewNameWithAcceptHeader() throws Exception {
    request.addHeader("Accept", "application/vnd.ms-excel");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    MappingMediaTypeFileExtensionResolver extensionsResolver = new MappingMediaTypeFileExtensionResolver(mapping);
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy());
    manager.addFileExtensionResolvers(extensionsResolver);
    viewResolver.setContentNegotiationManager(manager);
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    View viewMock = mock(View.class, "application_xls");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isSameAs(viewMock);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) MappingMediaTypeFileExtensionResolver(cn.taketoday.web.accept.MappingMediaTypeFileExtensionResolver) MediaType(cn.taketoday.http.MediaType) HeaderContentNegotiationStrategy(cn.taketoday.web.accept.HeaderContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Aggregations

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