Fix regression in ProducesRequestCondition
Closes gh-22853
This commit is contained in:
parent
a8f845c944
commit
b5327ef60f
|
@ -185,9 +185,12 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public ProducesRequestCondition getMatchingCondition(ServerWebExchange exchange) {
|
public ProducesRequestCondition getMatchingCondition(ServerWebExchange exchange) {
|
||||||
if (isEmpty() || CorsUtils.isPreFlightRequest(exchange.getRequest())) {
|
if (CorsUtils.isPreFlightRequest(exchange.getRequest())) {
|
||||||
return EMPTY_CONDITION;
|
return EMPTY_CONDITION;
|
||||||
}
|
}
|
||||||
|
if (isEmpty()) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
|
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
|
||||||
result.removeIf(expression -> !expression.match(exchange));
|
result.removeIf(expression -> !expression.match(exchange));
|
||||||
if (!result.isEmpty()) {
|
if (!result.isEmpty()) {
|
||||||
|
|
|
@ -21,15 +21,14 @@ import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.mock.web.test.server.MockServerWebExchange;
|
import org.springframework.mock.web.test.server.MockServerWebExchange;
|
||||||
|
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||||
|
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.*;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link ProducesRequestCondition}.
|
* Unit tests for {@link ProducesRequestCondition}.
|
||||||
|
@ -110,6 +109,23 @@ public class ProducesRequestConditionTests {
|
||||||
assertNotNull(condition.getMatchingCondition(exchange));
|
assertNotNull(condition.getMatchingCondition(exchange));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // gh-22853
|
||||||
|
public void matchAndCompare() {
|
||||||
|
RequestedContentTypeResolverBuilder builder = new RequestedContentTypeResolverBuilder();
|
||||||
|
builder.headerResolver();
|
||||||
|
builder.fixedResolver(MediaType.TEXT_HTML);
|
||||||
|
RequestedContentTypeResolver resolver = builder.build();
|
||||||
|
|
||||||
|
ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, resolver);
|
||||||
|
ProducesRequestCondition html = new ProducesRequestCondition(new String[] {"text/html"}, null, resolver);
|
||||||
|
|
||||||
|
MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "*/*"));
|
||||||
|
|
||||||
|
ProducesRequestCondition noneMatch = none.getMatchingCondition(exchange);
|
||||||
|
ProducesRequestCondition htmlMatch = html.getMatchingCondition(exchange);
|
||||||
|
|
||||||
|
assertEquals(1, noneMatch.compareTo(htmlMatch, exchange));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compareTo() {
|
public void compareTo() {
|
||||||
|
|
|
@ -185,10 +185,12 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request) {
|
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request) {
|
||||||
if (isEmpty() || CorsUtils.isPreFlightRequest(request)) {
|
if (CorsUtils.isPreFlightRequest(request)) {
|
||||||
return EMPTY_CONDITION;
|
return EMPTY_CONDITION;
|
||||||
}
|
}
|
||||||
|
if (isEmpty()) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
List<MediaType> acceptedMediaTypes;
|
List<MediaType> acceptedMediaTypes;
|
||||||
try {
|
try {
|
||||||
acceptedMediaTypes = getAcceptedMediaTypes(request);
|
acceptedMediaTypes = getAcceptedMediaTypes(request);
|
||||||
|
@ -196,7 +198,6 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
|
||||||
catch (HttpMediaTypeException ex) {
|
catch (HttpMediaTypeException ex) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
|
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
|
||||||
result.removeIf(expression -> !expression.match(acceptedMediaTypes));
|
result.removeIf(expression -> !expression.match(acceptedMediaTypes));
|
||||||
if (!result.isEmpty()) {
|
if (!result.isEmpty()) {
|
||||||
|
|
|
@ -21,7 +21,11 @@ import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
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 org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.ProduceMediaTypeExpression;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
@ -139,6 +143,24 @@ public class ProducesRequestConditionTests {
|
||||||
assertNotNull(condition.getMatchingCondition(request));
|
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
|
@Test
|
||||||
public void compareTo() {
|
public void compareTo() {
|
||||||
ProducesRequestCondition html = new ProducesRequestCondition("text/html");
|
ProducesRequestCondition html = new ProducesRequestCondition("text/html");
|
||||||
|
|
Loading…
Reference in New Issue