use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-boot by spring-projects.
the class WebClientAutoConfigurationTests method shouldGetPrototypeScopedBean.
@Test
void shouldGetPrototypeScopedBean() {
this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class).run((context) -> {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getBody()).willReturn(Flux.empty());
given(response.getHeaders()).willReturn(new HttpHeaders());
ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
given(firstConnector.connect(any(), any(), any())).willReturn(Mono.just(response));
WebClient.Builder firstBuilder = context.getBean(WebClient.Builder.class);
firstBuilder.clientConnector(firstConnector).baseUrl("https://first.example.org");
ClientHttpConnector secondConnector = mock(ClientHttpConnector.class);
given(secondConnector.connect(any(), any(), any())).willReturn(Mono.just(response));
WebClient.Builder secondBuilder = context.getBean(WebClient.Builder.class);
secondBuilder.clientConnector(secondConnector).baseUrl("https://second.example.org");
assertThat(firstBuilder).isNotEqualTo(secondBuilder);
firstBuilder.build().get().uri("/foo").retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
secondBuilder.build().get().uri("/foo").retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
then(firstConnector).should().connect(eq(HttpMethod.GET), eq(URI.create("https://first.example.org/foo")), any());
then(secondConnector).should().connect(eq(HttpMethod.GET), eq(URI.create("https://second.example.org/foo")), any());
WebClientCustomizer customizer = context.getBean("webClientCustomizer", WebClientCustomizer.class);
then(customizer).should(times(2)).customize(any(WebClient.Builder.class));
});
}
use of org.springframework.http.client.reactive.ClientHttpConnector in project JavaForFun by gumartinm.
the class ServicesConfig method webClientBuilder.
@Bean
public WebClient.Builder webClientBuilder() {
ClientHttpConnector connector = new ReactorClientHttpConnector(options -> {
options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeOut).onChannelInit(channel -> {
channel.pipeline().addLast(new ReadTimeoutHandler(readTimeOut, TimeUnit.MILLISECONDS));
channel.pipeline().addLast(new WriteTimeoutHandler(writeTimeout, TimeUnit.MILLISECONDS));
return true;
});
});
WebClient.Builder webClientBuilder = WebClient.builder();
return webClientBuilder.clientConnector(connector);
}
use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-framework by spring-projects.
the class AbstractMockMvcServerSpec method configureClient.
@Override
public WebTestClient.Builder configureClient() {
MockMvc mockMvc = getMockMvcBuilder().build();
ClientHttpConnector connector = new MockMvcHttpConnector(mockMvc);
return WebTestClient.bindToServer(connector);
}
use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-framework by spring-projects.
the class DefaultWebTestClientBuilder method build.
@Override
public WebTestClient build() {
ClientHttpConnector connectorToUse = this.connector;
if (connectorToUse == null) {
if (this.httpHandlerBuilder != null) {
connectorToUse = new HttpHandlerConnector(this.httpHandlerBuilder.build());
}
}
if (connectorToUse == null) {
connectorToUse = initConnector();
}
Function<ClientHttpConnector, ExchangeFunction> exchangeFactory = connector -> {
ExchangeFunction exchange = ExchangeFunctions.create(connector, initExchangeStrategies());
if (CollectionUtils.isEmpty(this.filters)) {
return exchange;
}
return this.filters.stream().reduce(ExchangeFilterFunction::andThen).map(filter -> filter.apply(exchange)).orElse(exchange);
};
return new DefaultWebTestClient(connectorToUse, exchangeFactory, initUriBuilderFactory(), this.defaultHeaders != null ? HttpHeaders.readOnlyHttpHeaders(this.defaultHeaders) : null, this.defaultCookies != null ? CollectionUtils.unmodifiableMultiValueMap(this.defaultCookies) : null, this.entityResultConsumer, this.responseTimeout, new DefaultWebTestClientBuilder(this));
}
use of org.springframework.http.client.reactive.ClientHttpConnector in project spring-framework by spring-projects.
the class WiretapConnectorTests method captureAndClaim.
@Test
public void captureAndClaim() {
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.create(HttpMethod.GET, URI.create("/test")).header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
WiretapConnector wiretapConnector = new WiretapConnector(connector);
ExchangeFunction function = ExchangeFunctions.create(wiretapConnector);
function.exchange(clientRequest).block(ofMillis(0));
ExchangeResult result = wiretapConnector.getExchangeResult("1", null, Duration.ZERO);
assertThat(result.getMethod()).isEqualTo(HttpMethod.GET);
assertThat(result.getUrl().toString()).isEqualTo("/test");
}
Aggregations