Skip to content

Commit 2dbf3a2

Browse files
committed
WebClient.exchange->exchangeToMono
Closes gh-17057
1 parent 5704582 commit 2dbf3a2

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/introspection/NimbusReactiveOpaqueTokenIntrospector.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ public NimbusReactiveOpaqueTokenIntrospector(String introspectionUri, WebClient
9898
@Override
9999
public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
100100
// @formatter:off
101-
return Mono.just(token)
102-
.flatMap(this::makeRequest)
103-
.flatMap(this::adaptToNimbusResponse)
101+
return this.makeRequest(token)
102+
.exchangeToMono(this::adaptToNimbusResponse)
104103
.map(this::parseNimbusResponse)
105104
.map(this::castToNimbusSuccess)
106105
.doOnNext((response) -> validate(token, response))
@@ -109,13 +108,12 @@ public Mono<OAuth2AuthenticatedPrincipal> introspect(String token) {
109108
// @formatter:on
110109
}
111110

112-
private Mono<ClientResponse> makeRequest(String token) {
111+
private WebClient.RequestHeadersSpec<?> makeRequest(String token) {
113112
// @formatter:off
114113
return this.webClient.post()
115114
.uri(this.introspectionUri)
116115
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
117-
.body(BodyInserters.fromFormData("token", token))
118-
.exchange();
116+
.body(BodyInserters.fromFormData("token", token));
119117
// @formatter:on
120118
}
121119

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/introspection/NimbusReactiveOpaqueTokenIntrospectorTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525
import java.util.Optional;
26+
import java.util.function.Function;
2627

2728
import net.minidev.json.JSONObject;
2829
import okhttp3.mockwebserver.Dispatcher;
@@ -45,6 +46,7 @@
4546
import static org.assertj.core.api.Assertions.assertThat;
4647
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4748
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
49+
import static org.mockito.ArgumentMatchers.any;
4850
import static org.mockito.BDDMockito.given;
4951
import static org.mockito.Mockito.mock;
5052
import static org.mockito.Mockito.spy;
@@ -265,6 +267,7 @@ private WebClient mockResponse(String response) {
265267
}
266268

267269
private WebClient mockResponse(String response, String mediaType) {
270+
WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class);
268271
WebClient real = WebClient.builder().build();
269272
WebClient.RequestBodyUriSpec spec = spy(real.post());
270273
WebClient webClient = spy(WebClient.class);
@@ -275,7 +278,13 @@ private WebClient mockResponse(String response, String mediaType) {
275278
ClientResponse.Headers headers = mock(ClientResponse.Headers.class);
276279
given(headers.contentType()).willReturn(Optional.ofNullable(mediaType).map(MediaType::parseMediaType));
277280
given(clientResponse.headers()).willReturn(headers);
278-
given(spec.exchange()).willReturn(Mono.just(clientResponse));
281+
given(responseSpec.bodyToMono(ClientResponse.class)).willReturn(Mono.just(clientResponse));
282+
given(spec.exchangeToMono(any())).willAnswer((invocation) -> {
283+
Object[] args = invocation.getArguments();
284+
Function<ClientResponse, Mono<ClientResponse>> fn = (Function<ClientResponse, Mono<ClientResponse>>) args[0];
285+
return fn.apply(clientResponse);
286+
});
287+
given(spec.retrieve()).willReturn(responseSpec);
279288
return webClient;
280289
}
281290

@@ -284,7 +293,7 @@ private WebClient mockResponse(Throwable ex) {
284293
WebClient.RequestBodyUriSpec spec = spy(real.post());
285294
WebClient webClient = spy(WebClient.class);
286295
given(webClient.post()).willReturn(spec);
287-
given(spec.exchange()).willThrow(ex);
296+
given(spec.exchangeToMono(any())).willReturn(Mono.error(ex));
288297
return webClient;
289298
}
290299

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/introspection/SpringReactiveOpaqueTokenIntrospectorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private WebClient mockResponse(Throwable ex) {
345345
WebClient.RequestBodyUriSpec spec = spy(real.post());
346346
WebClient webClient = spy(WebClient.class);
347347
given(webClient.post()).willReturn(spec);
348-
given(spec.exchange()).willThrow(ex);
348+
given(spec.exchangeToMono(any())).willThrow(ex);
349349
return webClient;
350350
}
351351

0 commit comments

Comments
 (0)