Search in sources :

Example 1 with ParameterContentNegotiationStrategy

use of cn.taketoday.web.accept.ParameterContentNegotiationStrategy 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 ParameterContentNegotiationStrategy

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

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);
    assertThat(result).as("Invalid view").isSameAs(viewMock);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) MediaType(cn.taketoday.http.MediaType) 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 3 with ParameterContentNegotiationStrategy

use of cn.taketoday.web.accept.ParameterContentNegotiationStrategy in project today-framework 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 4 with ParameterContentNegotiationStrategy

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

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);
    assertThat(result).as("Invalid view").isSameAs(viewMock);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) MediaType(cn.taketoday.http.MediaType) 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)

Aggregations

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