Skip to content

Commit

Permalink
fixed potential error using webClient without specifying url, and imp…
Browse files Browse the repository at this point in the history
…roved timeout test case
  • Loading branch information
rozagerardo committed Feb 12, 2021
1 parent 7d5be17 commit 230a7fa
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
Expand Down Expand Up @@ -41,6 +39,7 @@
import com.baeldung.web.reactive.client.WebClientApplication;

import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -265,17 +264,17 @@ public void givenWebClientWithTimeoutConfigurations_whenRequestUsingWronglyConfi
.addHandlerLast(new WriteTimeoutHandler(1000, TimeUnit.MILLISECONDS)));

WebClient timeoutClient = WebClient.builder()
.baseUrl("http://localhost:" + port)
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();

BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserterCompleteSuscriber = BodyInserters.fromPublisher(Subscriber::onComplete, String.class);
RequestHeadersSpec<?> headersSpecInserterCompleteSuscriber = timeoutClient.post()
StepVerifier.create(timeoutClient.post()
.uri("/resource")
.body(inserterCompleteSuscriber);

StepVerifier.create(headersSpecInserterCompleteSuscriber.retrieve()
.body(Mono.never(), String.class)
.retrieve()
.bodyToMono(String.class))
.expectTimeout(Duration.ofMillis(2000))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ReadTimeoutException.class.isAssignableFrom(ex.getCause()
.getClass()))
.verify();
}

Expand Down

0 comments on commit 230a7fa

Please sign in to comment.