Search in sources :

Example 11 with ClientHttpConnector

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));
    });
}
Also used : WebClientCustomizer(org.springframework.boot.web.reactive.function.client.WebClientCustomizer) HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 12 with ClientHttpConnector

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);
}
Also used : ReactorClientHttpConnector(org.springframework.http.client.reactive.ReactorClientHttpConnector) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) WriteTimeoutHandler(io.netty.handler.timeout.WriteTimeoutHandler) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) WebClient(org.springframework.web.reactive.function.client.WebClient) ReactorClientHttpConnector(org.springframework.http.client.reactive.ReactorClientHttpConnector) Bean(org.springframework.context.annotation.Bean)

Example 13 with ClientHttpConnector

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);
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockMvc(org.springframework.test.web.servlet.MockMvc)

Example 14 with ClientHttpConnector

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));
}
Also used : Arrays(java.util.Arrays) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Duration(java.time.Duration) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) Nullable(org.springframework.lang.Nullable) HttpComponentsClientHttpConnector(org.springframework.http.client.reactive.HttpComponentsClientHttpConnector) ReactorClientHttpConnector(org.springframework.http.client.reactive.ReactorClientHttpConnector) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) ClassUtils(org.springframework.util.ClassUtils) JettyClientHttpConnector(org.springframework.http.client.reactive.JettyClientHttpConnector) HttpHeaders(org.springframework.http.HttpHeaders) MultiValueMap(org.springframework.util.MultiValueMap) Consumer(java.util.function.Consumer) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) List(java.util.List) ExchangeFilterFunction(org.springframework.web.reactive.function.client.ExchangeFilterFunction) CollectionUtils(org.springframework.util.CollectionUtils) ExchangeFunctions(org.springframework.web.reactive.function.client.ExchangeFunctions) ExchangeStrategies(org.springframework.web.reactive.function.client.ExchangeStrategies) UriBuilderFactory(org.springframework.web.util.UriBuilderFactory) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) Assert(org.springframework.util.Assert) HttpComponentsClientHttpConnector(org.springframework.http.client.reactive.HttpComponentsClientHttpConnector) ReactorClientHttpConnector(org.springframework.http.client.reactive.ReactorClientHttpConnector) JettyClientHttpConnector(org.springframework.http.client.reactive.JettyClientHttpConnector) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction)

Example 15 with ClientHttpConnector

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");
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Duration(java.time.Duration) ExchangeFunctions(org.springframework.web.reactive.function.client.ExchangeFunctions) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Duration.ofMillis(java.time.Duration.ofMillis) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

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