Search in sources :

Example 6 with ClientHttpConnector

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

Example 7 with ClientHttpConnector

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

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

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

Example 10 with ClientHttpConnector

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

the class WebClientMetricsConfigurationTests method mockWebClient.

private WebClient mockWebClient(WebClient.Builder builder) {
    ClientHttpConnector connector = mock(ClientHttpConnector.class);
    given(connector.connect(any(), any(), any())).willReturn(Mono.just(new MockClientHttpResponse(HttpStatus.OK)));
    return builder.clientConnector(connector).build();
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse)

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