Search in sources :

Example 16 with HttpMessageWriter

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

the class DefaultClientRequestBuilderTests method bodyClass.

@Test
public void bodyClass() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, String.class).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) Test(org.junit.jupiter.api.Test)

Example 17 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-framework 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 18 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-framework 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)

Example 19 with HttpMessageWriter

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

the class BaseDefaultCodecs method getBaseTypedWriters.

/**
 * Return "base" typed writers only, i.e. common to client and server.
 */
final List<HttpMessageWriter<?>> getBaseTypedWriters() {
    if (!this.registerDefaults) {
        return Collections.emptyList();
    }
    List<HttpMessageWriter<?>> writers = new ArrayList<>();
    addCodec(writers, new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    addCodec(writers, new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    addCodec(writers, new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
    if (nettyByteBufPresent) {
        addCodec(writers, new EncoderHttpMessageWriter<>(new NettyByteBufEncoder()));
    }
    addCodec(writers, new ResourceHttpMessageWriter());
    addCodec(writers, new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    if (protobufPresent) {
        addCodec(writers, new ProtobufHttpMessageWriter(this.protobufEncoder != null ? (ProtobufEncoder) this.protobufEncoder : new ProtobufEncoder()));
    }
    return writers;
}
Also used : ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) 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) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ArrayList(java.util.ArrayList) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)

Example 20 with HttpMessageWriter

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

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(cn.taketoday.core.codec.ByteBufferEncoder) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(cn.taketoday.core.MultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

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