Support content negotiation for RFC 7807
Closes gh-28189
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.reactive.result.method.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -25,14 +26,19 @@ import io.reactivex.rxjava3.core.Single;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.core.codec.ByteBufferEncoder;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.ResourceHttpMessageWriter;
|
||||
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
||||
import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -40,8 +46,11 @@ import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.get;
|
||||
import static org.springframework.web.testfixture.method.ResolvableMethod.on;
|
||||
|
||||
/**
|
||||
@@ -82,7 +91,7 @@ public class ResponseBodyResultHandlerTests {
|
||||
testSupports(controller, method);
|
||||
|
||||
method = on(TestController.class).annotNotPresent(ResponseBody.class).resolveMethod("doWork");
|
||||
HandlerResult handlerResult = getHandlerResult(controller, method);
|
||||
HandlerResult handlerResult = getHandlerResult(controller, null, method);
|
||||
assertThat(this.resultHandler.supports(handlerResult)).isFalse();
|
||||
}
|
||||
|
||||
@@ -105,13 +114,42 @@ public class ResponseBodyResultHandlerTests {
|
||||
}
|
||||
|
||||
private void testSupports(Object controller, Method method) {
|
||||
HandlerResult handlerResult = getHandlerResult(controller, method);
|
||||
HandlerResult handlerResult = getHandlerResult(controller, null, method);
|
||||
assertThat(this.resultHandler.supports(handlerResult)).isTrue();
|
||||
}
|
||||
|
||||
private HandlerResult getHandlerResult(Object controller, Method method) {
|
||||
HandlerMethod handlerMethod = new HandlerMethod(controller, method);
|
||||
return new HandlerResult(handlerMethod, null, handlerMethod.getReturnType());
|
||||
@Test
|
||||
void problemDetailContentNegotiation() {
|
||||
|
||||
// Default
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
|
||||
testProblemDetailMediaType(exchange, MediaType.APPLICATION_PROBLEM_JSON);
|
||||
|
||||
// JSON requested
|
||||
exchange = MockServerWebExchange.from(get("/path").accept(MediaType.APPLICATION_JSON));
|
||||
testProblemDetailMediaType(exchange, MediaType.APPLICATION_JSON);
|
||||
|
||||
// No match fallback
|
||||
exchange = MockServerWebExchange.from(get("/path").accept(MediaType.APPLICATION_PDF));
|
||||
testProblemDetailMediaType(exchange, MediaType.APPLICATION_PROBLEM_JSON);
|
||||
}
|
||||
|
||||
private void testProblemDetailMediaType(MockServerWebExchange exchange, MediaType expectedMediaType) {
|
||||
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
|
||||
|
||||
Method method = on(TestRestController.class).returning(ProblemDetail.class).resolveMethod();
|
||||
HandlerResult result = getHandlerResult(new TestRestController(), problemDetail, method);
|
||||
|
||||
this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
|
||||
|
||||
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(expectedMediaType);
|
||||
assertResponseBody(exchange,
|
||||
"{\"type\":\"about:blank\"," +
|
||||
"\"title\":\"Bad Request\"," +
|
||||
"\"status\":400," +
|
||||
"\"detail\":null," +
|
||||
"\"instance\":\"/path\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -119,6 +157,17 @@ public class ResponseBodyResultHandlerTests {
|
||||
assertThat(this.resultHandler.getOrder()).isEqualTo(100);
|
||||
}
|
||||
|
||||
private HandlerResult getHandlerResult(Object controller, @Nullable Object returnValue, Method method) {
|
||||
HandlerMethod handlerMethod = new HandlerMethod(controller, method);
|
||||
return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType());
|
||||
}
|
||||
|
||||
private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) {
|
||||
StepVerifier.create(exchange.getResponse().getBody())
|
||||
.consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody))
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
@@ -142,6 +191,11 @@ public class ResponseBodyResultHandlerTests {
|
||||
public Completable handleToCompletable() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProblemDetail handleToProblemDetail() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user