Search in sources :

Example 1 with ClientHttpConnector

use of org.springframework.http.client.reactive.ClientHttpConnector 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 ClientHttpConnector

use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-integration by spring-projects.

the class WebFluxRequestExecutingMessageHandlerTests method testReactiveConnectErrorOneWay.

@Test
public void testReactiveConnectErrorOneWay() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        throw new RuntimeException("Intentional connection error");
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    String destinationUri = "http://www.springsource.org/spring-integration";
    WebFluxRequestExecutingMessageHandler reactiveHandler = new WebFluxRequestExecutingMessageHandler(destinationUri, webClient);
    reactiveHandler.setExpectReply(false);
    QueueChannel errorChannel = new QueueChannel();
    reactiveHandler.handleMessage(MessageBuilder.withPayload("hello, world").setErrorChannel(errorChannel).build());
    Message<?> errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    assertThat(errorMessage, instanceOf(ErrorMessage.class));
    Throwable throwable = (Throwable) errorMessage.getPayload();
    assertThat(throwable.getMessage(), containsString("Intentional connection error"));
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) QueueChannel(org.springframework.integration.channel.QueueChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) ErrorMessage(org.springframework.messaging.support.ErrorMessage) WebClient(org.springframework.web.reactive.function.client.WebClient) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Test(org.junit.Test)

Example 3 with ClientHttpConnector

use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-integration by spring-projects.

the class WebFluxRequestExecutingMessageHandlerTests method testFluxReply.

@Test
@SuppressWarnings("unchecked")
public void testFluxReply() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        DataBufferFactory bufferFactory = response.bufferFactory();
        Mono<DataBuffer> data = Mono.just(bufferFactory.wrap("foo\nbar\nbaz".getBytes()));
        return response.writeWith(data).then(Mono.defer(response::setComplete));
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    String destinationUri = "http://www.springsource.org/spring-integration";
    WebFluxRequestExecutingMessageHandler reactiveHandler = new WebFluxRequestExecutingMessageHandler(destinationUri, webClient);
    QueueChannel replyChannel = new QueueChannel();
    reactiveHandler.setOutputChannel(replyChannel);
    reactiveHandler.setExpectedResponseType(String.class);
    reactiveHandler.setReplyPayloadToFlux(true);
    reactiveHandler.handleMessage(MessageBuilder.withPayload("hello, world").build());
    Message<?> receive = replyChannel.receive(10_000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(Flux.class));
    Flux<String> flux = (Flux<String>) receive.getPayload();
    StepVerifier.create(flux).expectNext("foo", "bar", "baz").verifyComplete();
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) QueueChannel(org.springframework.integration.channel.QueueChannel) Flux(reactor.core.publisher.Flux) Matchers.containsString(org.hamcrest.Matchers.containsString) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) WebClient(org.springframework.web.reactive.function.client.WebClient) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 4 with ClientHttpConnector

use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-integration by spring-projects.

the class WebFluxRequestExecutingMessageHandlerTests method testReactiveReturn.

@Test
public void testReactiveReturn() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.OK);
        return Mono.defer(response::setComplete);
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    String destinationUri = "http://www.springsource.org/spring-integration";
    WebFluxRequestExecutingMessageHandler reactiveHandler = new WebFluxRequestExecutingMessageHandler(destinationUri, webClient);
    FluxMessageChannel ackChannel = new FluxMessageChannel();
    reactiveHandler.setOutputChannel(ackChannel);
    reactiveHandler.handleMessage(MessageBuilder.withPayload("hello, world").build());
    reactiveHandler.handleMessage(MessageBuilder.withPayload("hello, world").build());
    StepVerifier.create(ackChannel, 2).assertNext(m -> assertThat(m, hasHeader(HttpHeaders.STATUS_CODE, HttpStatus.OK))).assertNext(m -> assertThat(m, hasHeader(HttpHeaders.STATUS_CODE, HttpStatus.OK))).then(() -> ((Subscriber<?>) TestUtils.getPropertyValue(ackChannel, "subscribers", List.class).get(0)).onComplete()).verifyComplete();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) StepVerifier(reactor.test.StepVerifier) WebClient(org.springframework.web.reactive.function.client.WebClient) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestUtils(org.springframework.integration.test.util.TestUtils) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) ClientHttpResponseBodyExtractor(org.springframework.integration.webflux.support.ClientHttpResponseBodyExtractor) MessageHandlingException(org.springframework.messaging.MessageHandlingException) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Message(org.springframework.messaging.Message) Subscriber(org.reactivestreams.Subscriber) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) HttpHeaders(org.springframework.integration.http.HttpHeaders) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) HeaderMatcher.hasHeader(org.springframework.integration.test.matcher.HeaderMatcher.hasHeader) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) Subscriber(org.reactivestreams.Subscriber) Matchers.containsString(org.hamcrest.Matchers.containsString) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) WebClient(org.springframework.web.reactive.function.client.WebClient) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Test(org.junit.Test)

Example 5 with ClientHttpConnector

use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-integration by spring-projects.

the class WebFluxRequestExecutingMessageHandlerTests method testClientHttpResponseAsReply.

@Test
public void testClientHttpResponseAsReply() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        DataBufferFactory bufferFactory = response.bufferFactory();
        Flux<DataBuffer> data = Flux.just(bufferFactory.wrap("foo".getBytes()), bufferFactory.wrap("bar".getBytes()), bufferFactory.wrap("baz".getBytes()));
        return response.writeWith(data).then(Mono.defer(response::setComplete));
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    String destinationUri = "http://www.springsource.org/spring-integration";
    WebFluxRequestExecutingMessageHandler reactiveHandler = new WebFluxRequestExecutingMessageHandler(destinationUri, webClient);
    QueueChannel replyChannel = new QueueChannel();
    reactiveHandler.setOutputChannel(replyChannel);
    reactiveHandler.setBodyExtractor(new ClientHttpResponseBodyExtractor());
    reactiveHandler.handleMessage(MessageBuilder.withPayload("hello, world").build());
    Message<?> receive = replyChannel.receive(10_000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(ClientHttpResponse.class));
    ClientHttpResponse response = (ClientHttpResponse) receive.getPayload();
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(MediaType.TEXT_PLAIN, response.getHeaders().getContentType());
    StepVerifier.create(response.getBody().map(dataBuffer -> new String(dataBuffer.asByteBuffer().array()))).expectNext("foo", "bar", "baz").verifyComplete();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) StepVerifier(reactor.test.StepVerifier) WebClient(org.springframework.web.reactive.function.client.WebClient) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestUtils(org.springframework.integration.test.util.TestUtils) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) ClientHttpResponseBodyExtractor(org.springframework.integration.webflux.support.ClientHttpResponseBodyExtractor) MessageHandlingException(org.springframework.messaging.MessageHandlingException) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Message(org.springframework.messaging.Message) Subscriber(org.reactivestreams.Subscriber) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) HttpHeaders(org.springframework.integration.http.HttpHeaders) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) HeaderMatcher.hasHeader(org.springframework.integration.test.matcher.HeaderMatcher.hasHeader) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) QueueChannel(org.springframework.integration.channel.QueueChannel) ClientHttpResponseBodyExtractor(org.springframework.integration.webflux.support.ClientHttpResponseBodyExtractor) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.springframework.web.reactive.function.client.WebClient) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

ClientHttpConnector (org.springframework.http.client.reactive.ClientHttpConnector)16 WebClient (org.springframework.web.reactive.function.client.WebClient)10 Test (org.junit.Test)9 HttpHandlerConnector (org.springframework.test.web.reactive.server.HttpHandlerConnector)8 QueueChannel (org.springframework.integration.channel.QueueChannel)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ClientHttpResponse (org.springframework.http.client.reactive.ClientHttpResponse)5 List (java.util.List)4 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertNotNull (org.junit.Assert.assertNotNull)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)3 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpStatus (org.springframework.http.HttpStatus)3 ReactorClientHttpConnector (org.springframework.http.client.reactive.ReactorClientHttpConnector)3 ErrorMessage (org.springframework.messaging.support.ErrorMessage)3 MockClientHttpResponse (org.springframework.mock.http.client.reactive.MockClientHttpResponse)3 ExchangeFunction (org.springframework.web.reactive.function.client.ExchangeFunction)3 ExchangeFunctions (org.springframework.web.reactive.function.client.ExchangeFunctions)3 Flux (reactor.core.publisher.Flux)3