Search in sources :

Example 1 with ContentNegotiationManager

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithPathExtension.

@Test
public void resolveViewNameWithPathExtension() throws Exception {
    request.setRequestURI("/test");
    request.setParameter("format", "xls");
    String mediaType = "application/vnd.ms-excel";
    ContentNegotiationManager manager = new ContentNegotiationManager(new ParameterContentNegotiationStrategy(Collections.singletonMap("xls", MediaType.parseMediaType(mediaType))));
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setContentNegotiationManager(manager);
    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(mediaType);
    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) ParameterContentNegotiationStrategy(cn.taketoday.web.accept.ParameterContentNegotiationStrategy) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 2 with ContentNegotiationManager

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

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

Example 3 with ContentNegotiationManager

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

the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.

@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
    StaticWebServletApplicationContext webAppContext = new StaticWebServletApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();
    InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
    nestedResolver.setApplicationContext(webAppContext);
    nestedResolver.setViewClass(InternalResourceView.class);
    viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));
    FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
    viewResolver.afterPropertiesSet();
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isNotNull();
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) FixedContentNegotiationStrategy(cn.taketoday.web.accept.FixedContentNegotiationStrategy) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) MockServletContext(cn.taketoday.web.mock.MockServletContext) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 4 with ContentNegotiationManager

use of cn.taketoday.web.accept.ContentNegotiationManager 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 5 with ContentNegotiationManager

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

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    request.setRequestURI("foo.html");
    handler.handleRequest(requestContext);
    assertThat(this.response.getContentType()).isEqualTo("text/html");
}
Also used : ContentNegotiationManagerFactoryBean(cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) Resource(cn.taketoday.core.io.Resource) UrlBasedResource(cn.taketoday.core.io.UrlBasedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Aggregations

ContentNegotiationManager (cn.taketoday.web.accept.ContentNegotiationManager)43 Test (org.junit.jupiter.api.Test)38 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)16 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)16 Locale (java.util.Locale)16 MediaType (cn.taketoday.http.MediaType)10 FixedContentNegotiationStrategy (cn.taketoday.web.accept.FixedContentNegotiationStrategy)8 ContentNegotiationManagerFactoryBean (cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean)6 HeaderContentNegotiationStrategy (cn.taketoday.web.accept.HeaderContentNegotiationStrategy)6 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ClassPathResource (cn.taketoday.core.io.ClassPathResource)4 FileSystemResource (cn.taketoday.core.io.FileSystemResource)4 Resource (cn.taketoday.core.io.Resource)4 UrlBasedResource (cn.taketoday.core.io.UrlBasedResource)4 ParameterContentNegotiationStrategy (cn.taketoday.web.accept.ParameterContentNegotiationStrategy)4 PathExtensionContentNegotiationStrategy (cn.taketoday.web.accept.PathExtensionContentNegotiationStrategy)4 ReactiveAdapterRegistry (cn.taketoday.core.ReactiveAdapterRegistry)2 SyncTaskExecutor (cn.taketoday.core.task.SyncTaskExecutor)2 MappingMediaTypeFileExtensionResolver (cn.taketoday.web.accept.MappingMediaTypeFileExtensionResolver)2 StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)2