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