Search in sources :

Example 6 with HttpHandlerConnector

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

the class WebFluxRequestExecutingMessageHandlerTests method testServiceUnavailableWithoutBody.

@Test
public void testServiceUnavailableWithoutBody() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.SERVICE_UNAVAILABLE);
        return Mono.defer(response::setComplete);
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    String destinationUri = "http://www.springsource.org/spring-integration";
    QueueChannel replyChannel = new QueueChannel();
    QueueChannel errorChannel = new QueueChannel();
    WebFluxRequestExecutingMessageHandler messageHandler = new WebFluxRequestExecutingMessageHandler(destinationUri, webClient);
    messageHandler.setOutputChannel(replyChannel);
    Message<String> requestMessage = MessageBuilder.withPayload("test").setErrorChannel(errorChannel).build();
    messageHandler.handleMessage(requestMessage);
    Message<?> errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertThat(payload, instanceOf(MessageHandlingException.class));
    Exception exception = (Exception) payload;
    assertThat(exception.getCause(), instanceOf(WebClientResponseException.class));
    assertThat(exception.getMessage(), containsString("503 Service Unavailable"));
    Message<?> replyMessage = errorChannel.receive(10);
    assertNull(replyMessage);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.springframework.web.reactive.function.client.WebClient) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Test(org.junit.Test)

Example 7 with HttpHandlerConnector

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

the class WebFluxRequestExecutingMessageHandlerTests method testReactiveErrorOneWay.

@Test
public void testReactiveErrorOneWay() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        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);
    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("401 Unauthorized"));
}
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 8 with HttpHandlerConnector

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

the class WebFluxDslTests method testWebFluxFlowWithReplyPayloadToFlux.

@Test
public void testWebFluxFlowWithReplyPayloadToFlux() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        DataBufferFactory bufferFactory = response.bufferFactory();
        return response.writeWith(Mono.just(bufferFactory.wrap("FOO\nBAR\n".getBytes()))).then(Mono.defer(response::setComplete));
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    new DirectFieldAccessor(this.webFluxWithReplyPayloadToFlux).setPropertyValue("webClient", webClient);
    QueueChannel replyChannel = new QueueChannel();
    Message<String> testMessage = MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build();
    this.webFluxFlowWithReplyPayloadToFluxInput.send(testMessage);
    Message<?> receive = replyChannel.receive(10_000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(Flux.class));
    @SuppressWarnings("unchecked") Flux<String> response = (Flux<String>) receive.getPayload();
    StepVerifier.create(response).expectNext("FOO", "BAR").verifyComplete();
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) EnableWebFlux(org.springframework.web.reactive.config.EnableWebFlux) Flux(reactor.core.publisher.Flux) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) 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