Search in sources :

Example 21 with ContentNegotiationManager

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithDefaultContentType.

@Test
public void resolveViewNameWithDefaultContentType() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    MediaType mediaType = new MediaType("application", "xml");
    FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
    ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
    ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
    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(viewResolverMock2.resolveViewName(viewName, 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);
    assertSame("Invalid view", viewMock1, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 22 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager 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)

Example 23 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager 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)

Example 24 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method customMediaTypes.

@Test
public void customMediaTypes() throws Exception {
    load("spring.mvc.mediaTypes.yaml:text/yaml");
    RequestMappingHandlerAdapter adapter = this.context.getBean(RequestMappingHandlerAdapter.class);
    ContentNegotiationManager actual = (ContentNegotiationManager) ReflectionTestUtils.getField(adapter, "contentNegotiationManager");
    assertThat(actual.getAllFileExtensions().contains("yaml")).isTrue();
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Aggregations

ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)24 Test (org.junit.Test)23 MediaType (org.springframework.http.MediaType)8 ViewResolver (org.springframework.web.servlet.ViewResolver)8 Locale (java.util.Locale)7 View (org.springframework.web.servlet.View)7 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockServletContext (org.springframework.mock.web.test.MockServletContext)4 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)4 ClassPathResource (org.springframework.core.io.ClassPathResource)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 PathExtensionContentNegotiationStrategy (org.springframework.web.accept.PathExtensionContentNegotiationStrategy)3 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)3 BeanNameViewResolver (org.springframework.web.servlet.view.BeanNameViewResolver)3 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Method (java.lang.reflect.Method)2