|
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets;
|
|
|
import java.time.Instant;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Set;
|
|
|
+import java.util.function.Consumer;
|
|
|
|
|
|
import okhttp3.mockwebserver.MockResponse;
|
|
|
import okhttp3.mockwebserver.MockWebServer;
|
|
@@ -34,6 +35,7 @@ import org.springframework.core.convert.converter.Converter;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.MediaType;
|
|
|
+import org.springframework.security.oauth2.client.MockResponses;
|
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
|
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
|
@@ -54,6 +56,7 @@ import org.springframework.web.client.RestClient;
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.BDDMockito.given;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.verify;
|
|
@@ -85,14 +88,12 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
this.server = new MockWebServer();
|
|
|
this.server.start();
|
|
|
String tokenUri = this.server.url("/oauth2/token").toString();
|
|
|
- // @formatter:off
|
|
|
this.clientRegistration = TestClientRegistrations.clientCredentials()
|
|
|
- .clientId("client-1")
|
|
|
- .clientSecret("secret")
|
|
|
- .authorizationGrantType(AuthorizationGrantType.TOKEN_EXCHANGE)
|
|
|
- .tokenUri(tokenUri)
|
|
|
- .scope("read", "write");
|
|
|
- // @formatter:on
|
|
|
+ .clientId("client-1")
|
|
|
+ .clientSecret("secret")
|
|
|
+ .authorizationGrantType(AuthorizationGrantType.TOKEN_EXCHANGE)
|
|
|
+ .tokenUri(tokenUri)
|
|
|
+ .scope("read", "write");
|
|
|
this.subjectToken = TestOAuth2AccessTokens.scopes("read", "write");
|
|
|
this.actorToken = null;
|
|
|
}
|
|
@@ -158,15 +159,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenSuccessResponseThenReturnAccessTokenResponse() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\",\n"
|
|
|
- + " \"scope\": \"read write\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response-read-write.json"));
|
|
|
Instant expiresAtBefore = Instant.now().plusSeconds(3600);
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
Set<String> scopes = clientRegistration.getScopes();
|
|
@@ -199,15 +192,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenSubjectTokenIsJwtThenSubjectTokenTypeIsJwt() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\",\n"
|
|
|
- + " \"scope\": \"read write\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response-read-write.json"));
|
|
|
Instant expiresAtBefore = Instant.now().plusSeconds(3600);
|
|
|
this.subjectToken = TestJwts.jwt().build();
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
@@ -241,15 +226,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenActorTokenIsNotNullThenActorParametersAreSent() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\",\n"
|
|
|
- + " \"scope\": \"read write\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response-read-write.json"));
|
|
|
Instant expiresAtBefore = Instant.now().plusSeconds(3600);
|
|
|
this.actorToken = TestOAuth2AccessTokens.noScopes();
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
@@ -285,15 +262,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenActorTokenIsJwtThenActorTokenTypeIsJwt() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\",\n"
|
|
|
- + " \"scope\": \"read write\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response-read-write.json"));
|
|
|
Instant expiresAtBefore = Instant.now().plusSeconds(3600);
|
|
|
this.actorToken = TestJwts.jwt().build();
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
@@ -329,14 +298,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenAuthenticationClientSecretBasicThenAuthorizationHeaderIsSent() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
@@ -347,14 +309,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenAuthenticationClientSecretPostThenFormParametersAreSent() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration
|
|
|
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
|
|
|
.build();
|
|
@@ -363,19 +318,17 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
this.tokenResponseClient.getTokenResponse(grantRequest);
|
|
|
RecordedRequest recordedRequest = this.server.takeRequest();
|
|
|
String formParameters = recordedRequest.getBody().readUtf8();
|
|
|
- assertThat(formParameters).contains("client_id=client-1", "client_secret=secret");
|
|
|
+ // @formatter:off
|
|
|
+ assertThat(formParameters).contains(
|
|
|
+ param(OAuth2ParameterNames.CLIENT_ID, "client-1"),
|
|
|
+ param(OAuth2ParameterNames.CLIENT_SECRET, "secret")
|
|
|
+ );
|
|
|
+ // @formatter:on
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenSuccessResponseAndNotBearerTokenTypeThenThrowOAuth2AuthorizationException() {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"not-bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("invalid-token-type-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
@@ -390,15 +343,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenSuccessResponseIncludesScopeThenAccessTokenHasResponseScope() {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\",\n"
|
|
|
- + " \"scope\": \"read\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response-read.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
@@ -409,14 +354,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenSuccessResponseDoesNotIncludeScopeThenAccessTokenHasNoScope() {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
@@ -440,8 +378,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenServerErrorResponseThenThrowOAuth2AuthorizationException() {
|
|
|
- String accessTokenErrorResponse = "{\"error\": \"server_error\", \"error_description\": \"A server error occurred\"}";
|
|
|
- this.server.enqueue(jsonResponse(accessTokenErrorResponse).setResponseCode(500));
|
|
|
+ this.server.enqueue(MockResponses.json("server-error-response.json").setResponseCode(500));
|
|
|
TokenExchangeGrantRequest request = new TokenExchangeGrantRequest(this.clientRegistration.build(),
|
|
|
this.subjectToken, this.actorToken);
|
|
|
// @formatter:off
|
|
@@ -454,8 +391,7 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenErrorResponseThenThrowOAuth2AuthorizationException() {
|
|
|
- String accessTokenErrorResponse = "{\"error\": \"invalid_grant\", \"error_description\": \"Invalid grant\"}";
|
|
|
- this.server.enqueue(jsonResponse(accessTokenErrorResponse).setResponseCode(400));
|
|
|
+ this.server.enqueue(MockResponses.json("invalid-grant-response.json").setResponseCode(400));
|
|
|
TokenExchangeGrantRequest request = new TokenExchangeGrantRequest(this.clientRegistration.build(),
|
|
|
this.subjectToken, this.actorToken);
|
|
|
// @formatter:off
|
|
@@ -496,18 +432,11 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenHeadersConverterAddedThenCalled() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
|
- Converter<TokenExchangeGrantRequest, HttpHeaders> headersConverter = mock(Converter.class);
|
|
|
+ Converter<TokenExchangeGrantRequest, HttpHeaders> headersConverter = mock();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.put("custom-header-name", Collections.singletonList("custom-header-value"));
|
|
|
given(headersConverter.convert(grantRequest)).willReturn(headers);
|
|
@@ -521,18 +450,11 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenHeadersConverterSetThenCalled() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
|
- Converter<TokenExchangeGrantRequest, HttpHeaders> headersConverter = mock(Converter.class);
|
|
|
+ Converter<TokenExchangeGrantRequest, HttpHeaders> headersConverter = mock();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.put("custom-header-name", Collections.singletonList("custom-header-value"));
|
|
|
given(headersConverter.convert(grantRequest)).willReturn(headers);
|
|
@@ -546,18 +468,11 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenParametersConverterSetThenCalled() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
|
- Converter<TokenExchangeGrantRequest, MultiValueMap<String, String>> parametersConverter = mock(Converter.class);
|
|
|
+ Converter<TokenExchangeGrantRequest, MultiValueMap<String, String>> parametersConverter = mock();
|
|
|
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
|
|
parameters.add("custom-parameter-name", "custom-parameter-value");
|
|
|
given(parametersConverter.convert(grantRequest)).willReturn(parameters);
|
|
@@ -566,20 +481,13 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
verify(parametersConverter).convert(grantRequest);
|
|
|
RecordedRequest recordedRequest = this.server.takeRequest();
|
|
|
String formParameters = recordedRequest.getBody().readUtf8();
|
|
|
- assertThat(formParameters).contains("custom-parameter-name=custom-parameter-value");
|
|
|
+ assertThat(formParameters).contains(param("custom-parameter-name", "custom-parameter-value"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenParametersConverterSetThenAbleToOverrideDefaultParameters() throws Exception {
|
|
|
this.clientRegistration.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST);
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
@@ -587,7 +495,6 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
parameters.set(OAuth2ParameterNames.GRANT_TYPE, "custom");
|
|
|
parameters.set(OAuth2ParameterNames.SCOPE, "one two");
|
|
|
parameters.set(OAuth2ParameterNames.SUBJECT_TOKEN, "custom-token");
|
|
|
- // The client_id parameter is omitted for testing purposes
|
|
|
this.tokenResponseClient.setParametersConverter((authorizationGrantRequest) -> parameters);
|
|
|
this.tokenResponseClient.getTokenResponse(grantRequest);
|
|
|
RecordedRequest recordedRequest = this.server.takeRequest();
|
|
@@ -595,27 +502,22 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
// @formatter:off
|
|
|
assertThat(formParameters).contains(
|
|
|
param(OAuth2ParameterNames.GRANT_TYPE, "custom"),
|
|
|
- param(OAuth2ParameterNames.SCOPE, "one two"),
|
|
|
- param(OAuth2ParameterNames.SUBJECT_TOKEN, "custom-token"));
|
|
|
+ param(OAuth2ParameterNames.CLIENT_ID, "client-1"),
|
|
|
+ param(OAuth2ParameterNames.SUBJECT_TOKEN, "custom-token"),
|
|
|
+ param(OAuth2ParameterNames.SUBJECT_TOKEN_TYPE, ACCESS_TOKEN_TYPE_VALUE),
|
|
|
+ param(OAuth2ParameterNames.SCOPE, "one two")
|
|
|
+ );
|
|
|
// @formatter:on
|
|
|
- assertThat(formParameters).doesNotContain(OAuth2ParameterNames.CLIENT_ID);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getTokenResponseWhenParametersConverterAddedThenCalled() throws Exception {
|
|
|
- // @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
- // @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
Set<String> scopes = clientRegistration.getScopes();
|
|
|
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
this.actorToken);
|
|
|
- Converter<TokenExchangeGrantRequest, MultiValueMap<String, String>> parametersConverter = mock(Converter.class);
|
|
|
+ Converter<TokenExchangeGrantRequest, MultiValueMap<String, String>> parametersConverter = mock();
|
|
|
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
|
|
parameters.add("custom-parameter-name", "custom-parameter-value");
|
|
|
given(parametersConverter.convert(grantRequest)).willReturn(parameters);
|
|
@@ -637,16 +539,26 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void getTokenResponseWhenRestClientSetThenCalled() {
|
|
|
+ public void getTokenResponseWhenParametersCustomizerSetThenCalled() throws Exception {
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
+ ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
|
+ TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, this.subjectToken,
|
|
|
+ this.actorToken);
|
|
|
+ Consumer<MultiValueMap<String, String>> parametersCustomizer = mock();
|
|
|
// @formatter:off
|
|
|
- String accessTokenSuccessResponse = "{\n"
|
|
|
- + " \"access_token\": \"access-token-1234\",\n"
|
|
|
- + " \"token_type\": \"bearer\",\n"
|
|
|
- + " \"expires_in\": \"3600\"\n"
|
|
|
- + "}\n";
|
|
|
+ DefaultOAuth2TokenRequestParametersConverter<TokenExchangeGrantRequest> parametersConverter =
|
|
|
+ new DefaultOAuth2TokenRequestParametersConverter<>();
|
|
|
// @formatter:on
|
|
|
- this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
|
|
|
- RestClient customClient = mock(RestClient.class);
|
|
|
+ parametersConverter.setParametersCustomizer(parametersCustomizer);
|
|
|
+ this.tokenResponseClient.setParametersConverter(parametersConverter);
|
|
|
+ this.tokenResponseClient.getTokenResponse(grantRequest);
|
|
|
+ verify(parametersCustomizer).accept(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getTokenResponseWhenRestClientSetThenCalled() {
|
|
|
+ this.server.enqueue(MockResponses.json("access-token-response.json"));
|
|
|
+ RestClient customClient = mock();
|
|
|
given(customClient.post()).willReturn(RestClient.builder().build().post());
|
|
|
this.tokenResponseClient.setRestClient(customClient);
|
|
|
ClientRegistration clientRegistration = this.clientRegistration.build();
|
|
@@ -656,10 +568,6 @@ public class RestClientTokenExchangeTokenResponseClientTests {
|
|
|
verify(customClient).post();
|
|
|
}
|
|
|
|
|
|
- private static MockResponse jsonResponse(String json) {
|
|
|
- return new MockResponse().setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).setBody(json);
|
|
|
- }
|
|
|
-
|
|
|
private static String param(String parameterName, String parameterValue) {
|
|
|
return "%s=%s".formatted(parameterName, URLEncoder.encode(parameterValue, StandardCharsets.UTF_8));
|
|
|
}
|