Search in sources :

Example 1 with PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE

use of org.springframework.web.reactive.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleWithProducibleContentTypeShouldFailWithServerError.

// gh-23287
@Test
public void handleWithProducibleContentTypeShouldFailWithServerError() {
    ResponseEntity<String> value = ResponseEntity.ok().body("<foo/>");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    Set<MediaType> mediaTypes = Collections.singleton(MediaType.APPLICATION_XML);
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
    ResponseEntityResultHandler resultHandler = new ResponseEntityResultHandler(Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())), new RequestedContentTypeResolverBuilder().build());
    StepVerifier.create(resultHandler.handleResult(exchange, result)).consumeErrorWith(ex -> assertThat(ex).isInstanceOf(HttpMessageNotWritableException.class).hasMessageContaining("with preset Content-Type")).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) Duration(java.time.Duration) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE(org.springframework.web.reactive.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Instant(java.time.Instant) HandlerResult(org.springframework.web.reactive.HandlerResult) Test(org.junit.jupiter.api.Test) List(java.util.List) Single(io.reactivex.rxjava3.core.Single) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) CompletableFuture(java.util.concurrent.CompletableFuture) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ArrayList(java.util.ArrayList) MockServerHttpRequest.get(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.get) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ResolvableMethod.on(org.springframework.web.testfixture.method.ResolvableMethod.on) LinkedHashSet(java.util.LinkedHashSet) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ObjectUtils(org.springframework.util.ObjectUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) Completable(io.reactivex.rxjava3.core.Completable) Mono(reactor.core.publisher.Mono) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) ResponseEntity.notFound(org.springframework.http.ResponseEntity.notFound) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ResolvableType.forClassWithGenerics(org.springframework.core.ResolvableType.forClassWithGenerics) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MediaType(org.springframework.http.MediaType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 Completable (io.reactivex.rxjava3.core.Completable)1 Single (io.reactivex.rxjava3.core.Single)1 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 MethodParameter (org.springframework.core.MethodParameter)1