Search in sources :

Example 6 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.

@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    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(org.springframework.web.accept.ContentNegotiationManager) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 7 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

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(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 8 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

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();
    request.addHeader("Accept", "*/*");
    ProducesRequestCondition noneMatch = none.getMatchingCondition(request);
    ProducesRequestCondition htmlMatch = html.getMatchingCondition(request);
    assertThat(noneMatch.compareTo(htmlMatch, request)).isEqualTo(1);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) Test(org.junit.jupiter.api.Test)

Example 9 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

the class ViewResolutionTests method contentNegotiation.

@Test
void contentNegotiation() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    List<View> viewList = new ArrayList<>();
    viewList.add(new MappingJackson2JsonView());
    viewList.add(new MarshallingView(marshaller));
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
    cnViewResolver.setDefaultViews(viewList);
    cnViewResolver.setContentNegotiationManager(manager);
    cnViewResolver.afterPropertiesSet();
    MockMvc mockMvc = standaloneSetup(new PersonController()).setViewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
    mockMvc.perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.person.name").value("Corea"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
Also used : ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) View(org.springframework.web.servlet.View) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Aggregations

ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)9 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)9 Test (org.junit.jupiter.api.Test)6 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)6 View (org.springframework.web.servlet.View)5 ArrayList (java.util.ArrayList)3 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)3 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)3 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)3 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)3 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)3 Locale (java.util.Locale)2 CustomPathExtensionContentNegotiationStrategy (org.hisp.dhis.webapi.view.CustomPathExtensionContentNegotiationStrategy)2 Bean (org.springframework.context.annotation.Bean)2 MockMvc (org.springframework.test.web.servlet.MockMvc)2 CustomRequestMappingHandlerMapping (org.hisp.dhis.webapi.mvc.CustomRequestMappingHandlerMapping)1 Test (org.junit.Test)1 Primary (org.springframework.context.annotation.Primary)1 MediaType (org.springframework.http.MediaType)1 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)1