@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2011 the original author or authors .
* Copyright 2002 - 2012 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -16,6 +16,16 @@
package org.springframework.web.servlet.view ;
package org.springframework.web.servlet.view ;
import static org.easymock.EasyMock.createMock ;
import static org.easymock.EasyMock.expect ;
import static org.easymock.EasyMock.replay ;
import static org.easymock.EasyMock.verify ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertNull ;
import static org.junit.Assert.assertSame ;
import static org.junit.Assert.assertTrue ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.Collections ;
@ -28,7 +38,6 @@ import java.util.Set;
import org.junit.After ;
import org.junit.After ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
import org.springframework.http.MediaType ;
import org.springframework.http.MediaType ;
import org.springframework.mock.web.MockHttpServletRequest ;
import org.springframework.mock.web.MockHttpServletRequest ;
import org.springframework.mock.web.MockHttpServletResponse ;
import org.springframework.mock.web.MockHttpServletResponse ;
@ -40,9 +49,6 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.View ;
import org.springframework.web.servlet.View ;
import org.springframework.web.servlet.ViewResolver ;
import org.springframework.web.servlet.ViewResolver ;
import static org.easymock.EasyMock.* ;
import static org.junit.Assert.* ;
/ * *
/ * *
* @author Arjen Poutsma
* @author Arjen Poutsma
* /
* /
@ -100,7 +106,7 @@ public class ContentNegotiatingViewResolverTests {
}
}
// SPR-8678
// SPR-8678
@Test
@Test
public void getMediaTypeFilenameWithContextPath ( ) {
public void getMediaTypeFilenameWithContextPath ( ) {
request . setContextPath ( "/project-1.0.0.M3" ) ;
request . setContextPath ( "/project-1.0.0.M3" ) ;
@ -110,6 +116,15 @@ public class ContentNegotiatingViewResolverTests {
assertTrue ( "Context path should be excluded" , viewResolver . getMediaTypes ( request ) . isEmpty ( ) ) ;
assertTrue ( "Context path should be excluded" , viewResolver . getMediaTypes ( request ) . isEmpty ( ) ) ;
}
}
// SPR-9390
@Test
public void getMediaTypeFilenameWithEncodedURI ( ) {
request . setRequestURI ( "/quo%20vadis%3f.html" ) ;
List < MediaType > result = viewResolver . getMediaTypes ( request ) ;
assertEquals ( "Invalid content type" , Collections . singletonList ( new MediaType ( "text" , "html" ) ) , result ) ;
}
@Test
@Test
public void getMediaTypeParameter ( ) {
public void getMediaTypeParameter ( ) {
viewResolver . setFavorParameter ( true ) ;
viewResolver . setFavorParameter ( true ) ;
@ -139,7 +154,6 @@ public class ContentNegotiatingViewResolverTests {
request . setAttribute ( HandlerMapping . PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE , producibleTypes ) ;
request . setAttribute ( HandlerMapping . PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE , producibleTypes ) ;
request . addHeader ( "Accept" , "text/html,application/xml;q=0.9,application/xhtml+xml,*/*;q=0.8" ) ;
request . addHeader ( "Accept" , "text/html,application/xml;q=0.9,application/xhtml+xml,*/*;q=0.8" ) ;
List < MediaType > result = viewResolver . getMediaTypes ( request ) ;
List < MediaType > result = viewResolver . getMediaTypes ( request ) ;
assertEquals ( "Invalid amount of media types" , 1 , result . size ( ) ) ;
assertEquals ( "Invalid content type" , new MediaType ( "application" , "xhtml+xml" ) , result . get ( 0 ) ) ;
assertEquals ( "Invalid content type" , new MediaType ( "application" , "xhtml+xml" ) , result . get ( 0 ) ) ;
}
}
@ -294,6 +308,35 @@ public class ContentNegotiatingViewResolverTests {
verify ( viewResolverMock1 , viewResolverMock2 , viewMock1 , viewMock2 ) ;
verify ( viewResolverMock1 , viewResolverMock2 , viewMock1 , viewMock2 ) ;
}
}
// SPR-9160
@Test
public void resolveViewNameAcceptHeaderSortByQuality ( ) throws Exception {
request . addHeader ( "Accept" , "text/plain;q=0.5, application/json" ) ;
ViewResolver htmlViewResolver = createMock ( ViewResolver . class ) ;
ViewResolver jsonViewResolver = createMock ( ViewResolver . class ) ;
viewResolver . setViewResolvers ( Arrays . asList ( htmlViewResolver , jsonViewResolver ) ) ;
View htmlView = createMock ( "text_html" , View . class ) ;
View jsonViewMock = createMock ( "application_json" , View . class ) ;
String viewName = "view" ;
Locale locale = Locale . ENGLISH ;
expect ( htmlViewResolver . resolveViewName ( viewName , locale ) ) . andReturn ( htmlView ) ;
expect ( jsonViewResolver . resolveViewName ( viewName , locale ) ) . andReturn ( jsonViewMock ) ;
expect ( htmlView . getContentType ( ) ) . andReturn ( "text/html" ) . anyTimes ( ) ;
expect ( jsonViewMock . getContentType ( ) ) . andReturn ( "application/json" ) . anyTimes ( ) ;
replay ( htmlViewResolver , jsonViewResolver , htmlView , jsonViewMock ) ;
viewResolver . setFavorPathExtension ( false ) ;
View result = viewResolver . resolveViewName ( viewName , locale ) ;
assertSame ( "Invalid view" , jsonViewMock , result ) ;
verify ( htmlViewResolver , jsonViewResolver , htmlView , jsonViewMock ) ;
}
@Test
@Test
public void resolveViewNameAcceptHeaderDefaultView ( ) throws Exception {
public void resolveViewNameAcceptHeaderDefaultView ( ) throws Exception {
request . addHeader ( "Accept" , "application/json" ) ;
request . addHeader ( "Accept" , "application/json" ) ;
@ -426,7 +469,7 @@ public class ContentNegotiatingViewResolverTests {
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext ( ) ;
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext ( ) ;
webAppContext . setServletContext ( new MockServletContext ( ) ) ;
webAppContext . setServletContext ( new MockServletContext ( ) ) ;
webAppContext . refresh ( ) ;
webAppContext . refresh ( ) ;
UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver ( ) ;
UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver ( ) ;
urlViewResolver . setApplicationContext ( webAppContext ) ;
urlViewResolver . setApplicationContext ( webAppContext ) ;
ViewResolver xmlViewResolver = createMock ( ViewResolver . class ) ;
ViewResolver xmlViewResolver = createMock ( ViewResolver . class ) ;
@ -435,7 +478,7 @@ public class ContentNegotiatingViewResolverTests {
View xmlView = createMock ( "application_xml" , View . class ) ;
View xmlView = createMock ( "application_xml" , View . class ) ;
View jsonView = createMock ( "application_json" , View . class ) ;
View jsonView = createMock ( "application_json" , View . class ) ;
viewResolver . setDefaultViews ( Arrays . asList ( jsonView ) ) ;
viewResolver . setDefaultViews ( Arrays . asList ( jsonView ) ) ;
String viewName = "redirect:anotherTest" ;
String viewName = "redirect:anotherTest" ;
Locale locale = Locale . ENGLISH ;
Locale locale = Locale . ENGLISH ;
@ -449,7 +492,7 @@ public class ContentNegotiatingViewResolverTests {
verify ( xmlViewResolver , xmlView , jsonView ) ;
verify ( xmlViewResolver , xmlView , jsonView ) ;
}
}
@Test
@Test
public void resolveViewNoMatch ( ) throws Exception {
public void resolveViewNoMatch ( ) throws Exception {
request . addHeader ( "Accept" , "text/html,application/xhtml+xml,application/xml;q=0.9" ) ;
request . addHeader ( "Accept" , "text/html,application/xhtml+xml,application/xml;q=0.9" ) ;
@ -505,7 +548,7 @@ public class ContentNegotiatingViewResolverTests {
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext ( ) ;
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext ( ) ;
webAppContext . setServletContext ( new MockServletContext ( ) ) ;
webAppContext . setServletContext ( new MockServletContext ( ) ) ;
webAppContext . refresh ( ) ;
webAppContext . refresh ( ) ;
InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver ( ) ;
InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver ( ) ;
nestedResolver . setApplicationContext ( webAppContext ) ;
nestedResolver . setApplicationContext ( webAppContext ) ;
nestedResolver . setViewClass ( InternalResourceView . class ) ;
nestedResolver . setViewClass ( InternalResourceView . class ) ;