|
45 | 45 | import org.springframework.core.env.ConfigurableEnvironment;
|
46 | 46 | import org.springframework.core.env.StandardEnvironment;
|
47 | 47 | import org.springframework.http.HttpRequest;
|
| 48 | +import org.springframework.http.HttpStatus; |
| 49 | +import org.springframework.http.MediaType; |
48 | 50 | import org.springframework.http.client.ClientHttpRequestExecution;
|
49 | 51 | import org.springframework.http.client.ClientHttpRequestInterceptor;
|
50 | 52 | import org.springframework.http.client.ClientHttpResponse;
|
| 53 | +import org.springframework.mock.http.client.MockClientHttpResponse; |
51 | 54 | import org.springframework.security.core.GrantedAuthority;
|
52 | 55 | import org.springframework.security.core.authority.AuthorityUtils;
|
53 | 56 | import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
|
60 | 63 | import org.springframework.web.client.RestTemplate;
|
61 | 64 |
|
62 | 65 | import static org.assertj.core.api.Assertions.assertThat;
|
63 |
| -import static org.mockito.Matchers.any; |
64 | 66 | import static org.mockito.Mockito.mock;
|
65 |
| -import static org.mockito.Mockito.verify; |
66 | 67 |
|
67 | 68 | /**
|
68 | 69 | * Tests for {@link ResourceServerTokenServicesConfiguration}.
|
@@ -247,23 +248,12 @@ public void customUserInfoRestTemplateFactory() {
|
247 | 248 |
|
248 | 249 | @Test
|
249 | 250 | public void jwtAccessTokenConverterIsConfiguredWhenKeyUriIsProvided() {
|
250 |
| - EnvironmentTestUtils.addEnvironment(this.environment, |
251 |
| - "security.oauth2.resource.jwt.key-uri=http://localhost:12345/banana"); |
252 |
| - this.context = new SpringApplicationBuilder(ResourceConfiguration.class) |
253 |
| - .environment(this.environment).web(false).run(); |
254 |
| - assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1); |
255 |
| - } |
256 |
| - |
257 |
| - @Test |
258 |
| - public void jwtAccessTokenConverterRestTemplateCanBeCustomized() { |
259 | 251 | EnvironmentTestUtils.addEnvironment(this.environment,
|
260 | 252 | "security.oauth2.resource.jwt.key-uri=http://localhost:12345/banana");
|
261 | 253 | this.context = new SpringApplicationBuilder(ResourceConfiguration.class,
|
262 | 254 | JwtAccessTokenConverterRestTemplateCustomizerConfiguration.class)
|
263 | 255 | .environment(this.environment).web(false).run();
|
264 |
| - JwtAccessTokenConverterRestTemplateCustomizer customizer = this.context |
265 |
| - .getBean(JwtAccessTokenConverterRestTemplateCustomizer.class); |
266 |
| - verify(customizer).customize(any(RestTemplate.class)); |
| 256 | + assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1); |
267 | 257 | }
|
268 | 258 |
|
269 | 259 | @Configuration
|
@@ -385,7 +375,29 @@ static class JwtAccessTokenConverterRestTemplateCustomizerConfiguration {
|
385 | 375 |
|
386 | 376 | @Bean
|
387 | 377 | public JwtAccessTokenConverterRestTemplateCustomizer restTemplateCustomizer() {
|
388 |
| - return mock(JwtAccessTokenConverterRestTemplateCustomizer.class); |
| 378 | + return new MockRestCallCustomizer(); |
| 379 | + } |
| 380 | + |
| 381 | + } |
| 382 | + |
| 383 | + private static class MockRestCallCustomizer |
| 384 | + implements JwtAccessTokenConverterRestTemplateCustomizer { |
| 385 | + |
| 386 | + @Override |
| 387 | + public void customize(RestTemplate template) { |
| 388 | + template.getInterceptors().add(new ClientHttpRequestInterceptor() { |
| 389 | + |
| 390 | + @Override |
| 391 | + public ClientHttpResponse intercept(HttpRequest request, byte[] body, |
| 392 | + ClientHttpRequestExecution execution) throws IOException { |
| 393 | + String payload = "{\"value\":\"FOO\"}"; |
| 394 | + MockClientHttpResponse response = new MockClientHttpResponse( |
| 395 | + payload.getBytes(), HttpStatus.OK); |
| 396 | + response.getHeaders().setContentType(MediaType.APPLICATION_JSON); |
| 397 | + return response; |
| 398 | + } |
| 399 | + |
| 400 | + }); |
389 | 401 | }
|
390 | 402 |
|
391 | 403 | }
|
|
0 commit comments