Search in sources :

Example 21 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ClientCodecConfigurerTests method clonedConfigurer.

@Test
public void clonedConfigurer() {
    ClientCodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jackson2Decoder = new Jackson2JsonDecoder();
    clone.defaultCodecs().serverSentEventDecoder(jackson2Decoder);
    clone.defaultCodecs().multipartCodecs().encoder(new Jackson2SmileEncoder());
    clone.defaultCodecs().multipartCodecs().writer(new ResourceHttpMessageWriter());
    // Clone has the customizations
    Decoder<?> sseDecoder = findCodec(clone.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(sseDecoder).isSameAs(jackson2Decoder);
    assertThat(writers).hasSize(2);
    // Original does not have the customizations
    sseDecoder = findCodec(this.configurer.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
    writers = findCodec(this.configurer.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(sseDecoder).isNotSameAs(jackson2Decoder);
    assertThat(writers).hasSize(10);
}
Also used : ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) ClientCodecConfigurer(cn.taketoday.http.codec.ClientCodecConfigurer) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) Test(org.junit.jupiter.api.Test)

Example 22 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ClientCodecConfigurerTests method cloneShouldNotDropMultipartCodecs.

// gh-24194
@Test
public void cloneShouldNotDropMultipartCodecs() {
    ClientCodecConfigurer clone = this.configurer.clone();
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(writers).hasSize(10);
}
Also used : MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) ClientCodecConfigurer(cn.taketoday.http.codec.ClientCodecConfigurer) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) Test(org.junit.jupiter.api.Test)

Example 23 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ClientCodecConfigurerTests method cloneShouldNotBeImpactedByChangesToOriginal.

@Test
public void cloneShouldNotBeImpactedByChangesToOriginal() {
    ClientCodecConfigurer clone = this.configurer.clone();
    this.configurer.registerDefaults(false);
    this.configurer.customCodecs().register(new Jackson2JsonEncoder());
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(writers).hasSize(10);
}
Also used : MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) ClientCodecConfigurer(cn.taketoday.http.codec.ClientCodecConfigurer) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 24 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

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 : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) DELETE(cn.taketoday.http.HttpMethod.DELETE) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) BDDMockito.given(org.mockito.BDDMockito.given) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) URI(java.net.URI) TypeReference(cn.taketoday.core.TypeReference) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) BodyInserter(cn.taketoday.web.reactive.function.BodyInserter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 25 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.

@Test
public void bodyParameterizedTypeReference() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    TypeReference<String> typeReference = new TypeReference<>() {
    };
    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(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Aggregations

EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)28 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)28 Test (org.junit.jupiter.api.Test)22 ResourceHttpMessageWriter (cn.taketoday.http.codec.ResourceHttpMessageWriter)20 FormHttpMessageWriter (cn.taketoday.http.codec.FormHttpMessageWriter)18 MultipartHttpMessageWriter (cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter)16 ProtobufHttpMessageWriter (cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)16 ArrayList (java.util.ArrayList)14 PartEventHttpMessageWriter (cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter)12 Jackson2JsonEncoder (cn.taketoday.http.codec.json.Jackson2JsonEncoder)10 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)8 List (java.util.List)8 MockClientHttpRequest (cn.taketoday.http.client.reactive.MockClientHttpRequest)7 TypeReference (cn.taketoday.core.TypeReference)6 CharSequenceEncoder (cn.taketoday.core.codec.CharSequenceEncoder)6 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)6 ServerSentEventHttpMessageReader (cn.taketoday.http.codec.ServerSentEventHttpMessageReader)6 ServerSentEventHttpMessageWriter (cn.taketoday.http.codec.ServerSentEventHttpMessageWriter)6 Publisher (org.reactivestreams.Publisher)6 Mono (reactor.core.publisher.Mono)6