Search in sources :

Example 1 with ClientHttpResponseBodyExtractor

use of org.springframework.integration.webflux.support.ClientHttpResponseBodyExtractor 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

List (java.util.List)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNull (org.junit.Assert.assertNull)1 Assert.assertThat (org.junit.Assert.assertThat)1 Test (org.junit.Test)1 Subscriber (org.reactivestreams.Subscriber)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 ClientHttpConnector (org.springframework.http.client.reactive.ClientHttpConnector)1 ClientHttpResponse (org.springframework.http.client.reactive.ClientHttpResponse)1 FluxMessageChannel (org.springframework.integration.channel.FluxMessageChannel)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 HttpHeaders (org.springframework.integration.http.HttpHeaders)1 MessageBuilder (org.springframework.integration.support.MessageBuilder)1 HeaderMatcher.hasHeader (org.springframework.integration.test.matcher.HeaderMatcher.hasHeader)1