Search in sources :

Example 96 with View

use of org.springframework.web.servlet.View 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 97 with View

use of org.springframework.web.servlet.View in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method resolveViewNameWithRequestParameter.

@Test
public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    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(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) ParameterContentNegotiationStrategy(org.springframework.web.accept.ParameterContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 98 with View

use of org.springframework.web.servlet.View in project spring-framework by spring-projects.

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

Example 99 with View

use of org.springframework.web.servlet.View in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method resolveViewNameWithInvalidAcceptHeader.

@Test
public void resolveViewNameWithInvalidAcceptHeader() throws Exception {
    request.addHeader("Accept", "application");
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    View result = viewResolver.resolveViewName("test", Locale.ENGLISH);
    assertThat(result).isNull();
}
Also used : ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 100 with View

use of org.springframework.web.servlet.View in project spring-framework by spring-projects.

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

Aggregations

View (org.springframework.web.servlet.View)114 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 HashMap (java.util.HashMap)29 ModelAndView (org.springframework.web.servlet.ModelAndView)27 Locale (java.util.Locale)20 RedirectView (org.springframework.web.servlet.view.RedirectView)20 ViewResolver (org.springframework.web.servlet.ViewResolver)19 Map (java.util.Map)13 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)11 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 ArrayList (java.util.ArrayList)8 MediaType (org.springframework.http.MediaType)8 MockServletContext (org.springframework.mock.web.test.MockServletContext)8 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)7