Search in sources :

Example 1 with ClientHttpRequest

use of cn.taketoday.http.client.reactive.ClientHttpRequest in project today-infrastructure by TAKETODAY.

the class BodyInsertersTests method fromFormDataWith.

@Test
public void fromFormDataWith() {
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData("name 1", "value 1").with("name 2", "value 2+1").with("name 2", "value 2+2").with("name 3", null);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8));
    }).expectComplete().verify();
}
Also used : MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 2 with ClientHttpRequest

use of cn.taketoday.http.client.reactive.ClientHttpRequest in project today-framework by TAKETODAY.

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 : 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 3 with ClientHttpRequest

use of cn.taketoday.http.client.reactive.ClientHttpRequest in project today-framework by TAKETODAY.

the class BodyInsertersTests method fromFormDataMap.

@Test
public void fromFormDataMap() {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData(body);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8));
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 4 with ClientHttpRequest

use of cn.taketoday.http.client.reactive.ClientHttpRequest in project today-framework by TAKETODAY.

the class BodyInsertersTests method fromFormDataWith.

@Test
public void fromFormDataWith() {
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData("name 1", "value 1").with("name 2", "value 2+1").with("name 2", "value 2+2").with("name 3", null);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8));
    }).expectComplete().verify();
}
Also used : MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 5 with ClientHttpRequest

use of cn.taketoday.http.client.reactive.ClientHttpRequest 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)

Aggregations

ClientHttpRequest (cn.taketoday.http.client.reactive.ClientHttpRequest)8 Test (org.junit.jupiter.api.Test)8 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)4 MultiValueMap (cn.taketoday.core.MultiValueMap)4 TypeReference (cn.taketoday.core.TypeReference)4 CharSequenceEncoder (cn.taketoday.core.codec.CharSequenceEncoder)4 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)4 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)4 DELETE (cn.taketoday.http.HttpMethod.DELETE)4 GET (cn.taketoday.http.HttpMethod.GET)4 OPTIONS (cn.taketoday.http.HttpMethod.OPTIONS)4 POST (cn.taketoday.http.HttpMethod.POST)4 MockClientHttpRequest (cn.taketoday.http.client.reactive.MockClientHttpRequest)4 EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)4 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)4 BodyInserter (cn.taketoday.web.reactive.function.BodyInserter)4 MockClientHttpRequest (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)4