Search in sources :

Example 1 with ParameterContentNegotiationStrategy

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithRequestParameter.

@Test
public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    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) MediaType(org.springframework.http.MediaType) ParameterContentNegotiationStrategy(org.springframework.web.accept.ParameterContentNegotiationStrategy) 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 ParameterContentNegotiationStrategy (org.springframework.web.accept.ParameterContentNegotiationStrategy)1 View (org.springframework.web.servlet.View)1 ViewResolver (org.springframework.web.servlet.ViewResolver)1