Search in sources :

Example 11 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.http.HttpMethod.GET) DELETE(org.springframework.http.HttpMethod.DELETE) BodyInserter(org.springframework.web.reactive.function.BodyInserter) ArrayList(java.util.ArrayList) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) BDDMockito.given(org.mockito.BDDMockito.given) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) POST(org.springframework.http.HttpMethod.POST) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 12 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.

the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.

@Test
public void bodyParameterizedTypeReference() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<>() {
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, typeReference).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 13 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.

the class BodyInsertersTests method createContext.

@BeforeEach
public void createContext() {
    final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    messageWriters.add(new ResourceHttpMessageWriter());
    messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
    messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
    messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
    messageWriters.add(new FormHttpMessageWriter());
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    messageWriters.add(new MultipartHttpMessageWriter(messageWriters));
    this.context = new BodyInserter.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return messageWriters;
        }

        @Override
        public Optional<ServerHttpRequest> serverRequest() {
            return Optional.empty();
        }

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    };
    this.hints = new HashMap<>();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.

the class DefaultClientRequestBuilderTests method fromCopiesBody.

@Test
public void fromCopiesBody() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest other = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    ClientRequest result = ClientRequest.from(other).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(POST, "/");
    result.writeTo(request, strategies).block();
    String copiedBody = request.getBodyAsString().block();
    assertThat(copiedBody).isEqualTo("foo");
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.http.HttpMethod.GET) DELETE(org.springframework.http.HttpMethod.DELETE) BodyInserter(org.springframework.web.reactive.function.BodyInserter) ArrayList(java.util.ArrayList) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) BDDMockito.given(org.mockito.BDDMockito.given) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) POST(org.springframework.http.HttpMethod.POST) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 15 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleWithPresetContentTypeShouldFailWithServerError.

// gh-23205
@Test
public void handleWithPresetContentTypeShouldFailWithServerError() {
    ResponseEntity<String> value = ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body("<foo/>");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    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) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)18 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)15 ArrayList (java.util.ArrayList)11 Test (org.junit.jupiter.api.Test)11 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)11 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)11 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)10 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)7 List (java.util.List)6 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)5 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)5 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)5 Mono (reactor.core.publisher.Mono)5 URI (java.net.URI)4 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 MediaType (org.springframework.http.MediaType)4 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)4