Fix regression in ProducesRequestCondition
Closes gh-22853
This commit is contained in:
@@ -185,10 +185,12 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
|
||||
@Override
|
||||
@Nullable
|
||||
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request) {
|
||||
if (isEmpty() || CorsUtils.isPreFlightRequest(request)) {
|
||||
if (CorsUtils.isPreFlightRequest(request)) {
|
||||
return EMPTY_CONDITION;
|
||||
}
|
||||
|
||||
if (isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
List<MediaType> acceptedMediaTypes;
|
||||
try {
|
||||
acceptedMediaTypes = getAcceptedMediaTypes(request);
|
||||
@@ -196,7 +198,6 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
|
||||
catch (HttpMediaTypeException ex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
|
||||
result.removeIf(expression -> !expression.match(acceptedMediaTypes));
|
||||
if (!result.isEmpty()) {
|
||||
|
||||
@@ -21,7 +21,11 @@ import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.accept.FixedContentNegotiationStrategy;
|
||||
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
|
||||
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.ProduceMediaTypeExpression;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -139,6 +143,24 @@ public class ProducesRequestConditionTests {
|
||||
assertNotNull(condition.getMatchingCondition(request));
|
||||
}
|
||||
|
||||
@Test // gh-22853
|
||||
public void matchAndCompare() {
|
||||
ContentNegotiationManager manager = new ContentNegotiationManager(
|
||||
new HeaderContentNegotiationStrategy(),
|
||||
new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
|
||||
|
||||
ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, manager);
|
||||
ProducesRequestCondition html = new ProducesRequestCondition(new String[] {"text/html"}, null, manager);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "*/*");
|
||||
|
||||
ProducesRequestCondition noneMatch = none.getMatchingCondition(request);
|
||||
ProducesRequestCondition htmlMatch = html.getMatchingCondition(request);
|
||||
|
||||
assertEquals(1, noneMatch.compareTo(htmlMatch, request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTo() {
|
||||
ProducesRequestCondition html = new ProducesRequestCondition("text/html");
|
||||
|
||||
Reference in New Issue
Block a user