@ -16,8 +16,10 @@
@@ -16,8 +16,10 @@
package org.springframework.web.servlet.mvc.condition ;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collections ;
import javax.servlet.http.HttpServletRequest ;
import org.junit.Test ;
@ -37,9 +39,7 @@ public class ProducesRequestConditionTests {
@@ -37,9 +39,7 @@ public class ProducesRequestConditionTests {
@Test
public void match ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "text/plain" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
assertNotNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -47,9 +47,7 @@ public class ProducesRequestConditionTests {
@@ -47,9 +47,7 @@ public class ProducesRequestConditionTests {
@Test
public void matchNegated ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "!text/plain" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
assertNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -57,9 +55,8 @@ public class ProducesRequestConditionTests {
@@ -57,9 +55,8 @@ public class ProducesRequestConditionTests {
@Test
public void matchNegatedWithoutAcceptHeader ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "!text/plain" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
assertNotNull ( condition . getMatchingCondition ( request ) ) ;
assertNotNull ( condition . getMatchingCondition ( new MockHttpSe rvletR equest( ) ) ) ;
assertEquals ( Collections . emptySet ( ) , condition . getProducibleMediaTypes ( ) ) ;
}
@ -72,9 +69,7 @@ public class ProducesRequestConditionTests {
@@ -72,9 +69,7 @@ public class ProducesRequestConditionTests {
@Test
public void matchWildcard ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "text/*" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
assertNotNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -82,9 +77,7 @@ public class ProducesRequestConditionTests {
@@ -82,9 +77,7 @@ public class ProducesRequestConditionTests {
@Test
public void matchMultiple ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "text/plain" , "application/xml" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
assertNotNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -92,9 +85,7 @@ public class ProducesRequestConditionTests {
@@ -92,9 +85,7 @@ public class ProducesRequestConditionTests {
@Test
public void matchSingle ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "text/plain" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "application/xml" ) ;
HttpServletRequest request = createRequest ( "application/xml" ) ;
assertNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -102,9 +93,7 @@ public class ProducesRequestConditionTests {
@@ -102,9 +93,7 @@ public class ProducesRequestConditionTests {
@Test
public void matchParseError ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "text/plain" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "bogus" ) ;
HttpServletRequest request = createRequest ( "bogus" ) ;
assertNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -112,18 +101,17 @@ public class ProducesRequestConditionTests {
@@ -112,18 +101,17 @@ public class ProducesRequestConditionTests {
@Test
public void matchParseErrorWithNegation ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "!text/plain" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "bogus" ) ;
HttpServletRequest request = createRequest ( "bogus" ) ;
assertNull ( condition . getMatchingCondition ( request ) ) ;
}
@Test
public void matchByRequestParameter ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( new String [ ] { "text/plain" } , new String [ ] { } ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( "GET" , "/foo.txt" ) ;
String [ ] produces = { "text/plain" } ;
String [ ] headers = { } ;
ProducesRequestCondition condition = new ProducesRequestCondition ( produces , headers ) ;
HttpServletRequest request = new MockHttpServletRequest ( "GET" , "/foo.txt" ) ;
assertNotNull ( condition . getMatchingCondition ( request ) ) ;
}
@ -131,9 +119,7 @@ public class ProducesRequestConditionTests {
@@ -131,9 +119,7 @@ public class ProducesRequestConditionTests {
@Test // SPR-17550
public void matchWithNegationAndMediaTypeAllWithQualityParameter ( ) {
ProducesRequestCondition condition = new ProducesRequestCondition ( "!application/json" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" ,
HttpServletRequest request = createRequest (
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" ) ;
assertNotNull ( condition . getMatchingCondition ( request ) ) ;
@ -145,8 +131,7 @@ public class ProducesRequestConditionTests {
@@ -145,8 +131,7 @@ public class ProducesRequestConditionTests {
ProducesRequestCondition xml = new ProducesRequestCondition ( "application/xml" ) ;
ProducesRequestCondition none = new ProducesRequestCondition ( ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "application/xml, text/html" ) ;
HttpServletRequest request = createRequest ( "application/xml, text/html" ) ;
assertTrue ( html . compareTo ( xml , request ) > 0 ) ;
assertTrue ( xml . compareTo ( html , request ) < 0 ) ;
@ -155,21 +140,18 @@ public class ProducesRequestConditionTests {
@@ -155,21 +140,18 @@ public class ProducesRequestConditionTests {
assertTrue ( html . compareTo ( none , request ) < 0 ) ;
assertTrue ( none . compareTo ( html , request ) > 0 ) ;
request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "application/xml, text/*" ) ;
request = createRequest ( "application/xml, text/*" ) ;
assertTrue ( html . compareTo ( xml , request ) > 0 ) ;
assertTrue ( xml . compareTo ( html , request ) < 0 ) ;
request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "application/pdf" ) ;
request = createRequest ( "application/pdf" ) ;
assertTrue ( html . compareTo ( xml , request ) = = 0 ) ;
assertTrue ( xml . compareTo ( html , request ) = = 0 ) ;
assertEquals ( 0 , html . compareTo ( xml , request ) ) ;
assertEquals ( 0 , xml . compareTo ( html , request ) ) ;
// See SPR-7000
request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/html;q=0.9,application/xml" ) ;
request = createRequest ( "text/html;q=0.9,application/xml" ) ;
assertTrue ( html . compareTo ( xml , request ) > 0 ) ;
assertTrue ( xml . compareTo ( html , request ) < 0 ) ;
@ -177,8 +159,7 @@ public class ProducesRequestConditionTests {
@@ -177,8 +159,7 @@ public class ProducesRequestConditionTests {
@Test
public void compareToWithSingleExpression ( ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
ProducesRequestCondition condition1 = new ProducesRequestCondition ( "text/plain" ) ;
ProducesRequestCondition condition2 = new ProducesRequestCondition ( "text/*" ) ;
@ -195,8 +176,7 @@ public class ProducesRequestConditionTests {
@@ -195,8 +176,7 @@ public class ProducesRequestConditionTests {
ProducesRequestCondition condition1 = new ProducesRequestCondition ( "*/*" , "text/plain" ) ;
ProducesRequestCondition condition2 = new ProducesRequestCondition ( "text/*" , "text/plain;q=0.7" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
int result = condition1 . compareTo ( condition2 , request ) ;
assertTrue ( "Invalid comparison result: " + result , result < 0 ) ;
@ -210,9 +190,7 @@ public class ProducesRequestConditionTests {
@@ -210,9 +190,7 @@ public class ProducesRequestConditionTests {
ProducesRequestCondition condition1 = new ProducesRequestCondition ( "text/*" , "text/plain" ) ;
ProducesRequestCondition condition2 = new ProducesRequestCondition ( "application/*" , "application/xml" ) ;
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
request . addHeader ( "Accept" , "application/xml" ) ;
HttpServletRequest request = createRequest ( "text/plain" , "application/xml" ) ;
int result = condition1 . compareTo ( condition2 , request ) ;
assertTrue ( "Invalid comparison result: " + result , result < 0 ) ;
@ -220,9 +198,7 @@ public class ProducesRequestConditionTests {
@@ -220,9 +198,7 @@ public class ProducesRequestConditionTests {
result = condition2 . compareTo ( condition1 , request ) ;
assertTrue ( "Invalid comparison result: " + result , result > 0 ) ;
request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "application/xml" ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
request = createRequest ( "application/xml" , "text/plain" ) ;
result = condition1 . compareTo ( condition2 , request ) ;
assertTrue ( "Invalid comparison result: " + result , result > 0 ) ;
@ -270,8 +246,7 @@ public class ProducesRequestConditionTests {
@@ -270,8 +246,7 @@ public class ProducesRequestConditionTests {
@Test
public void compareToMediaTypeAllWithParameter ( ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "*/*;q=0.9" ) ;
HttpServletRequest request = createRequest ( "*/*;q=0.9" ) ;
ProducesRequestCondition condition1 = new ProducesRequestCondition ( ) ;
ProducesRequestCondition condition2 = new ProducesRequestCondition ( "application/json" ) ;
@ -282,8 +257,7 @@ public class ProducesRequestConditionTests {
@@ -282,8 +257,7 @@ public class ProducesRequestConditionTests {
@Test
public void compareToEqualMatch ( ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/*" ) ;
HttpServletRequest request = createRequest ( "text/*" ) ;
ProducesRequestCondition condition1 = new ProducesRequestCondition ( "text/plain" ) ;
ProducesRequestCondition condition2 = new ProducesRequestCondition ( "text/xhtml" ) ;
@ -324,8 +298,7 @@ public class ProducesRequestConditionTests {
@@ -324,8 +298,7 @@ public class ProducesRequestConditionTests {
@Test
public void getMatchingCondition ( ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . addHeader ( "Accept" , "text/plain" ) ;
HttpServletRequest request = createRequest ( "text/plain" ) ;
ProducesRequestCondition condition = new ProducesRequestCondition ( "text/plain" , "application/xml" ) ;
@ -339,6 +312,12 @@ public class ProducesRequestConditionTests {
@@ -339,6 +312,12 @@ public class ProducesRequestConditionTests {
}
private MockHttpServletRequest createRequest ( String . . . headerValue ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
Arrays . stream ( headerValue ) . forEach ( value - > request . addHeader ( "Accept" , headerValue ) ) ;
return request ;
}
private void assertConditions ( ProducesRequestCondition condition , String . . . expected ) {
Collection < ProduceMediaTypeExpression > expressions = condition . getContent ( ) ;
assertEquals ( "Invalid number of conditions" , expressions . size ( ) , expected . length ) ;