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());
}
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"));
}
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();
}
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();
}
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();
}
Aggregations