diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java index d5f1ef77a0..95c63f267f 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java @@ -3,7 +3,6 @@ package com.baeldung.reactive.logging; import ch.qos.logback.classic.spi.LoggingEvent; import ch.qos.logback.core.Appender; import com.baeldung.reactive.logging.filters.LogFilters; -import com.baeldung.reactive.logging.jetty.RequestLogEnhancer; import com.baeldung.reactive.logging.netty.CustomLogger; import com.fasterxml.jackson.databind.ObjectMapper; import java.net.URI; @@ -21,6 +20,7 @@ import org.springframework.web.reactive.function.client.WebClient; import reactor.netty.channel.BootstrapHandlers; import reactor.netty.http.client.HttpClient; +import static com.baeldung.reactive.logging.jetty.RequestLogEnhancer.enhance; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -69,13 +69,13 @@ public class WebClientLoggingIntegrationTest { } @Test - public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() { + public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() { SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) { @Override public Request newRequest(URI uri) { Request request = super.newRequest(uri); - return new RequestLogEnhancer().enhance(request); + return enhance(request); } }; @@ -94,7 +94,7 @@ public class WebClientLoggingIntegrationTest { } @Test - public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() { + public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() { reactor.netty.http.client.HttpClient httpClient = HttpClient .create() @@ -108,11 +108,12 @@ public class WebClientLoggingIntegrationTest { .body(BodyInserters.fromObject(post)) .exchange() .block(); + verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains("00000300"))); } @Test - public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() { + public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() { reactor.netty.http.client.HttpClient httpClient = HttpClient .create() @@ -128,11 +129,12 @@ public class WebClientLoggingIntegrationTest { .body(BodyInserters.fromObject(post)) .exchange() .block(); + verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody))); } @Test - public void givenDefaultHttpClientWithString_whenEndpointIsConsumed_thenRequestAndResponseLogged() { + public void givenDefaultHttpClientWithFilter_whenEndpointIsConsumed_thenRequestAndResponseLogged() { WebClient .builder() .filters(exchangeFilterFunctions -> { diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java index ac333feb6c..43e3660743 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java @@ -13,7 +13,7 @@ import org.eclipse.jetty.http.HttpHeader; @Slf4j public class RequestLogEnhancer { - public Request enhance(Request request) { + public static Request enhance(Request request) { StringBuilder group = new StringBuilder(); request.onRequestBegin(theRequest -> group .append("Request ") @@ -63,7 +63,7 @@ public class RequestLogEnhancer { return request; } - private String toString(ByteBuffer buffer, Charset charset) { + private static String toString(ByteBuffer buffer, Charset charset) { byte[] bytes; if (buffer.hasArray()) { bytes = new byte[buffer.capacity()]; @@ -75,7 +75,7 @@ public class RequestLogEnhancer { return new String(bytes, charset); } - private Charset getCharset(HttpFields headers) { + private static Charset getCharset(HttpFields headers) { String contentType = headers.get(HttpHeader.CONTENT_TYPE); if (contentType != null) { String[] tokens = contentType @@ -88,5 +88,6 @@ public class RequestLogEnhancer { } return StandardCharsets.UTF_8; } + } diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java index 529549f99b..9f2a4d127f 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java @@ -20,7 +20,7 @@ public class CustomLogger extends LoggingHandler { ByteBuf msg = (ByteBuf) arg; return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset()); } - return ""; + return super.format(ctx, event, arg); } private String decode(ByteBuf src, int readerIndex, int len, Charset charset) {