Search in sources :

Example 16 with View

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

the class ContentNegotiatingViewResolverTests method resolveViewNoMatch.

@Test
public void resolveViewNoMatch() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    View viewMock = mock(View.class, "application_xml");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/pdf");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertNull("Invalid view", result);
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 17 with View

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

the class ContentNegotiatingViewResolverTests method resolveViewNameFilenameDefaultView.

@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
    request.setRequestURI("/test.json");
    Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
    PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));
    ViewResolver viewResolverMock1 = mock(ViewResolver.class);
    ViewResolver viewResolverMock2 = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
    View viewMock1 = mock(View.class, "application_xml");
    View viewMock2 = mock(View.class, "text_html");
    View viewMock3 = mock(View.class, "application_json");
    List<View> defaultViews = new ArrayList<>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);
    viewResolver.afterPropertiesSet();
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
    given(viewResolverMock1.resolveViewName(viewName + ".json", locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
    given(viewResolverMock2.resolveViewName(viewName + ".json", locale)).willReturn(null);
    given(viewMock1.getContentType()).willReturn("application/xml");
    given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
    given(viewMock3.getContentType()).willReturn("application/json");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) PathExtensionContentNegotiationStrategy(org.springframework.web.accept.PathExtensionContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 18 with View

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

the class ContentNegotiatingViewResolverTests method resolveViewContentTypeNull.

@Test
public void resolveViewContentTypeNull() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    View viewMock = mock(View.class, "application_xml");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn(null);
    View result = viewResolver.resolveViewName(viewName, locale);
    assertNull("Invalid view", result);
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 19 with View

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

the class ContentNegotiatingViewResolverTests method resolveViewNameRedirectView.

@Test
public void resolveViewNameRedirectView() throws Exception {
    request.addHeader("Accept", "application/json");
    request.setRequestURI("/test");
    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();
    UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
    urlViewResolver.setApplicationContext(webAppContext);
    ViewResolver xmlViewResolver = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));
    View xmlView = mock(View.class, "application_xml");
    View jsonView = mock(View.class, "application_json");
    viewResolver.setDefaultViews(Arrays.asList(jsonView));
    viewResolver.afterPropertiesSet();
    String viewName = "redirect:anotherTest";
    Locale locale = Locale.ENGLISH;
    given(xmlViewResolver.resolveViewName(viewName, locale)).willReturn(xmlView);
    given(jsonView.getContentType()).willReturn("application/json");
    View actualView = viewResolver.resolveViewName(viewName, locale);
    assertEquals("Invalid view", RedirectView.class, actualView.getClass());
}
Also used : Locale(java.util.Locale) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 20 with View

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

the class ContentNegotiatingViewResolverTests method resolveViewNoMatchUseUnacceptableStatus.

@Test
public void resolveViewNoMatchUseUnacceptableStatus() throws Exception {
    viewResolver.setUseNotAcceptableStatusCode(true);
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    View viewMock = mock(View.class, "application_xml");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/pdf");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertNotNull("Invalid view", result);
    MockHttpServletResponse response = new MockHttpServletResponse();
    result.render(null, request, response);
    assertEquals("Invalid status code set", 406, response.getStatus());
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

View (org.springframework.web.servlet.View)71 Test (org.junit.Test)51 ModelAndView (org.springframework.web.servlet.ModelAndView)19 HashMap (java.util.HashMap)18 RedirectView (org.springframework.web.servlet.view.RedirectView)18 Locale (java.util.Locale)17 ViewResolver (org.springframework.web.servlet.ViewResolver)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 MockServletContext (org.springframework.mock.web.test.MockServletContext)13 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)13 Map (java.util.Map)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)10 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)10 MediaType (org.springframework.http.MediaType)7 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)7 Principal (org.apereo.cas.authentication.principal.Principal)6 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)6