use of cn.taketoday.web.accept.FixedContentNegotiationStrategy 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);
}
use of cn.taketoday.web.accept.FixedContentNegotiationStrategy 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();
}
use of cn.taketoday.web.accept.FixedContentNegotiationStrategy 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.FixedContentNegotiationStrategy in project today-framework 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);
}
use of cn.taketoday.web.accept.FixedContentNegotiationStrategy in project today-framework 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();
}
Aggregations