fixed potential error using webClient without specifying url, and improved timeout test case

This commit is contained in:
Gerardo Roza 2021-02-12 20:08:12 -03:00
parent 89eb93a46c
commit dc6e305e1f
1 changed files with 7 additions and 8 deletions

View File

@ -11,8 +11,6 @@ import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test; 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;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
@ -41,6 +39,7 @@ import com.baeldung.web.reactive.client.Foo;
import com.baeldung.web.reactive.client.WebClientApplication; import com.baeldung.web.reactive.client.WebClientApplication;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -265,17 +264,17 @@ public class WebClientIntegrationTest {
.addHandlerLast(new WriteTimeoutHandler(1000, TimeUnit.MILLISECONDS))); .addHandlerLast(new WriteTimeoutHandler(1000, TimeUnit.MILLISECONDS)));
WebClient timeoutClient = WebClient.builder() WebClient timeoutClient = WebClient.builder()
.baseUrl("http://localhost:" + port)
.clientConnector(new ReactorClientHttpConnector(httpClient)) .clientConnector(new ReactorClientHttpConnector(httpClient))
.build(); .build();
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserterCompleteSuscriber = BodyInserters.fromPublisher(Subscriber::onComplete, String.class); StepVerifier.create(timeoutClient.post()
RequestHeadersSpec<?> headersSpecInserterCompleteSuscriber = timeoutClient.post()
.uri("/resource") .uri("/resource")
.body(inserterCompleteSuscriber); .body(Mono.never(), String.class)
.retrieve()
StepVerifier.create(headersSpecInserterCompleteSuscriber.retrieve()
.bodyToMono(String.class)) .bodyToMono(String.class))
.expectTimeout(Duration.ofMillis(2000)) .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ReadTimeoutException.class.isAssignableFrom(ex.getCause()
.getClass()))
.verify(); .verify();
} }