StSch
StSch

Reputation: 397

How to log a library's WebClient request and response?

I am aware of how to configure the logging if I am building the Spring Boot Webclient on my own in my code. But what if I am calling some API that encapsulates the http calls using the WebClient? Can I intercept the communication to log the request + response?

Upvotes: 0

Views: 47

Answers (1)

Wuj
Wuj

Reputation: 13

What you usually need to consider, given an external integration, is how you expect the external endpoint to behave. Once defined its basic behaviour, that could be more or less equivalent to what the actual web server does, you can mock it, using for example Mockito.

About logging, that really depends on the implementation of your integration. Usually, it is part of a standalone component that is to be injected/wired or part of a layer in the application.

I don't expect you to be testing logging string but you probably want to test strings contained in the body of a response. That happens by means of reactor.test.StepVerifier.

It would look like something that could resemble

Mono<List<LicenseRecord>> matchLicenses = WebClientWrapper.request();
StepVerifier.create(tournamentLicenses).expectNextMatches(list -> {
            return list.size() == 1
                && list.get(0).getType().equals(LicenseType.TOURNAMENT)
                && list.get(0).getCustomerId().equals(customerId);
        }).verifyComplete()

From the official doc, there is an example here https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java

Upvotes: 0

Related Questions