Search in sources :

Example 1 with ClientHttpRequest

use of org.springframework.http.client.reactive.ClientHttpRequest in project spring-framework by spring-projects.

the class WebTestClientConnectorTests method captureAndClaim.

@Test
@SuppressWarnings("deprecation")
public void captureAndClaim() throws Exception {
    ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
    ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));
    ClientRequest clientRequest = ClientRequest.method(HttpMethod.GET, URI.create("/test")).header(WiretapConnector.REQUEST_ID_HEADER_NAME, "1").build();
    WiretapConnector wiretapConnector = new WiretapConnector(connector);
    ExchangeFunction function = ExchangeFunctions.create(wiretapConnector);
    function.exchange(clientRequest).blockMillis(0);
    ExchangeResult actual = wiretapConnector.claimRequest("1");
    assertNotNull(actual);
    assertEquals(HttpMethod.GET, actual.getMethod());
    assertEquals("/test", actual.getUrl().toString());
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Assert.assertNotNull(org.junit.Assert.assertNotNull) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpStatus(org.springframework.http.HttpStatus) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) ExchangeFunctions(org.springframework.web.reactive.function.client.ExchangeFunctions) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Assert.assertEquals(org.junit.Assert.assertEquals) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Test(org.junit.Test)

Example 2 with ClientHttpRequest

use of org.springframework.http.client.reactive.ClientHttpRequest in project spring-framework by spring-projects.

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() throws Exception {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
        DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com")).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    when(strategies.messageWriters()).thenReturn(messageWriters::stream);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertNotNull(request.getBody());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Assert(org.junit.Assert) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) ByteBuffer(java.nio.ByteBuffer) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 3 with ClientHttpRequest

use of org.springframework.http.client.reactive.ClientHttpRequest in project spring-framework by spring-projects.

the class BodyInsertersTests method ofFormData.

@Test
public void ofFormData() throws Exception {
    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("http://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);
        assertArrayEquals("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8), resultBytes);
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 ClientHttpRequest (org.springframework.http.client.reactive.ClientHttpRequest)3 URI (java.net.URI)2 HttpMethod (org.springframework.http.HttpMethod)2 MockClientHttpRequest (org.springframework.mock.http.client.reactive.test.MockClientHttpRequest)2 Mono (reactor.core.publisher.Mono)2 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Mockito (org.mockito.Mockito)1 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpConnector (org.springframework.http.client.reactive.ClientHttpConnector)1 ClientHttpResponse (org.springframework.http.client.reactive.ClientHttpResponse)1