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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations