Search in sources :

Example 1 with MappingMediaTypeFileExtensionResolver

use of org.springframework.web.accept.MappingMediaTypeFileExtensionResolver in project spring-framework by spring-projects.

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);
    assertSame("Invalid view", viewMock, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MappingMediaTypeFileExtensionResolver(org.springframework.web.accept.MappingMediaTypeFileExtensionResolver) MediaType(org.springframework.http.MediaType) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Aggregations

Locale (java.util.Locale)1 Test (org.junit.Test)1 MediaType (org.springframework.http.MediaType)1 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)1 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)1 MappingMediaTypeFileExtensionResolver (org.springframework.web.accept.MappingMediaTypeFileExtensionResolver)1 View (org.springframework.web.servlet.View)1 ViewResolver (org.springframework.web.servlet.ViewResolver)1