Search in sources :

Example 1 with FixedContentNegotiationStrategy

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);
}
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 2 with FixedContentNegotiationStrategy

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();
}
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 3 with FixedContentNegotiationStrategy

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);
}
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 4 with FixedContentNegotiationStrategy

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);
}
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 5 with FixedContentNegotiationStrategy

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();
}
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.testfixture.servlet.MockServletContext) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Aggregations

ContentNegotiationManager (cn.taketoday.web.accept.ContentNegotiationManager)8 FixedContentNegotiationStrategy (cn.taketoday.web.accept.FixedContentNegotiationStrategy)8 Test (org.junit.jupiter.api.Test)8 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 HeaderContentNegotiationStrategy (cn.taketoday.web.accept.HeaderContentNegotiationStrategy)2 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)2 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)1 MockServletContext (cn.taketoday.web.mock.MockServletContext)1 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)1