Search in sources :

Example 1 with HeaderContentNegotiationStrategy

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

the class ProducesRequestConditionTests method matchAndCompare.

// gh-22853
@Test
public void matchAndCompare() {
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, manager);
    ProducesRequestCondition html = new ProducesRequestCondition(new String[] { "text/html" }, null, manager);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    request.addHeader("Accept", "*/*");
    ProducesRequestCondition noneMatch = none.getMatchingCondition(createContext(request));
    ProducesRequestCondition htmlMatch = html.getMatchingCondition(createContext(request));
    assertThat(noneMatch.compareTo(htmlMatch, createContext(request))).isEqualTo(1);
}
Also used : ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) FixedContentNegotiationStrategy(cn.taketoday.web.accept.FixedContentNegotiationStrategy) HeaderContentNegotiationStrategy(cn.taketoday.web.accept.HeaderContentNegotiationStrategy) Test(org.junit.jupiter.api.Test)

Example 2 with HeaderContentNegotiationStrategy

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

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

Example 3 with HeaderContentNegotiationStrategy

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

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

Example 4 with HeaderContentNegotiationStrategy

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

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

Example 5 with HeaderContentNegotiationStrategy

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

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);
    assertThat(result).as("Invalid view").isSameAs(viewMock);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) MappingMediaTypeFileExtensionResolver(cn.taketoday.web.accept.MappingMediaTypeFileExtensionResolver) MediaType(cn.taketoday.http.MediaType) HeaderContentNegotiationStrategy(cn.taketoday.web.accept.HeaderContentNegotiationStrategy) 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)6 HeaderContentNegotiationStrategy (cn.taketoday.web.accept.HeaderContentNegotiationStrategy)6 Test (org.junit.jupiter.api.Test)6 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)4 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)4 Locale (java.util.Locale)4 MediaType (cn.taketoday.http.MediaType)2 FixedContentNegotiationStrategy (cn.taketoday.web.accept.FixedContentNegotiationStrategy)2 MappingMediaTypeFileExtensionResolver (cn.taketoday.web.accept.MappingMediaTypeFileExtensionResolver)2 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)1 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1