Search in sources :

Example 1 with HttpHandlerConnector

use of org.springframework.test.web.reactive.server.HttpHandlerConnector 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 2 with HttpHandlerConnector

use of org.springframework.test.web.reactive.server.HttpHandlerConnector 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 3 with HttpHandlerConnector

use of org.springframework.test.web.reactive.server.HttpHandlerConnector 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 4 with HttpHandlerConnector

use of org.springframework.test.web.reactive.server.HttpHandlerConnector 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)

Example 5 with HttpHandlerConnector

use of org.springframework.test.web.reactive.server.HttpHandlerConnector in project spring-integration by spring-projects.

the class WebFluxDslTests method testHttpReactiveProxyFlow.

@Test
public void testHttpReactiveProxyFlow() throws Exception {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        return response.writeWith(Mono.just(response.bufferFactory().wrap("FOO".getBytes()))).then(Mono.defer(response::setComplete));
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    new DirectFieldAccessor(this.httpReactiveProxyFlow).setPropertyValue("webClient", webClient);
    this.mockMvc.perform(get("/service2").with(httpBasic("guest", "guest")).param("name", "foo")).andExpect(content().string("FOO"));
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) WebClient(org.springframework.web.reactive.function.client.WebClient) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 ClientHttpConnector (org.springframework.http.client.reactive.ClientHttpConnector)8 HttpHandlerConnector (org.springframework.test.web.reactive.server.HttpHandlerConnector)8 WebClient (org.springframework.web.reactive.function.client.WebClient)8 QueueChannel (org.springframework.integration.channel.QueueChannel)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)4 ErrorMessage (org.springframework.messaging.support.ErrorMessage)4 Flux (reactor.core.publisher.Flux)4 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 MessageHandlingException (org.springframework.messaging.MessageHandlingException)3 WebClientResponseException (org.springframework.web.reactive.function.client.WebClientResponseException)3 List (java.util.List)2 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertNull (org.junit.Assert.assertNull)2 Assert.assertThat (org.junit.Assert.assertThat)2 Subscriber (org.reactivestreams.Subscriber)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2