|
33 | 33 | import org.mockito.Mock;
|
34 | 34 | import org.mockito.junit.jupiter.MockitoExtension;
|
35 | 35 |
|
| 36 | +import org.springframework.aot.hint.RuntimeHints; |
| 37 | +import org.springframework.aot.hint.predicate.ReflectionHintsPredicates; |
| 38 | +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; |
| 39 | +import org.springframework.boot.web.client.RestTemplateBuilder.RestTemplateBuilderRuntimeHints; |
36 | 40 | import org.springframework.http.HttpHeaders;
|
37 | 41 | import org.springframework.http.HttpMethod;
|
38 | 42 | import org.springframework.http.MediaType;
|
| 43 | +import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper; |
39 | 44 | import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
40 | 45 | import org.springframework.http.client.ClientHttpRequest;
|
41 | 46 | import org.springframework.http.client.ClientHttpRequestFactory;
|
|
50 | 55 | import org.springframework.http.converter.StringHttpMessageConverter;
|
51 | 56 | import org.springframework.test.util.ReflectionTestUtils;
|
52 | 57 | import org.springframework.test.web.client.MockRestServiceServer;
|
| 58 | +import org.springframework.util.ReflectionUtils; |
53 | 59 | import org.springframework.web.client.ResponseErrorHandler;
|
54 | 60 | import org.springframework.web.client.RestTemplate;
|
55 | 61 | import org.springframework.web.util.UriTemplateHandler;
|
@@ -585,6 +591,58 @@ void unwrappingDoesNotAffectRequestFactoryThatIsSetOnTheBuiltTemplate() {
|
585 | 591 | assertThat(template.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class);
|
586 | 592 | }
|
587 | 593 |
|
| 594 | + @Test |
| 595 | + void shouldRegisterHints() { |
| 596 | + RuntimeHints hints = new RuntimeHints(); |
| 597 | + new RestTemplateBuilderRuntimeHints().registerHints(hints, getClass().getClassLoader()); |
| 598 | + ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection(); |
| 599 | + assertThat(reflection |
| 600 | + .onField(ReflectionUtils.findField(AbstractClientHttpRequestFactoryWrapper.class, "requestFactory"))) |
| 601 | + .accepts(hints); |
| 602 | + } |
| 603 | + |
| 604 | + @Test |
| 605 | + void shouldRegisterHttpComponentHints() { |
| 606 | + RuntimeHints hints = new RuntimeHints(); |
| 607 | + new RestTemplateBuilderRuntimeHints().registerHints(hints, getClass().getClassLoader()); |
| 608 | + ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection(); |
| 609 | + assertThat(reflection.onMethod(ReflectionUtils.findMethod(HttpComponentsClientHttpRequestFactory.class, |
| 610 | + "setConnectTimeout", int.class))).accepts(hints); |
| 611 | + assertThat(reflection.onMethod( |
| 612 | + ReflectionUtils.findMethod(HttpComponentsClientHttpRequestFactory.class, "setReadTimeout", int.class))) |
| 613 | + .accepts(hints); |
| 614 | + assertThat(reflection.onMethod(ReflectionUtils.findMethod(HttpComponentsClientHttpRequestFactory.class, |
| 615 | + "setBufferRequestBody", boolean.class))).accepts(hints); |
| 616 | + } |
| 617 | + |
| 618 | + @Test |
| 619 | + void shouldRegisterOkHttpHints() { |
| 620 | + RuntimeHints hints = new RuntimeHints(); |
| 621 | + new RestTemplateBuilderRuntimeHints().registerHints(hints, getClass().getClassLoader()); |
| 622 | + ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection(); |
| 623 | + assertThat(reflection.onMethod( |
| 624 | + ReflectionUtils.findMethod(OkHttp3ClientHttpRequestFactory.class, "setConnectTimeout", int.class))) |
| 625 | + .accepts(hints); |
| 626 | + assertThat(reflection.onMethod( |
| 627 | + ReflectionUtils.findMethod(OkHttp3ClientHttpRequestFactory.class, "setReadTimeout", int.class))) |
| 628 | + .accepts(hints); |
| 629 | + } |
| 630 | + |
| 631 | + @Test |
| 632 | + void shouldRegisterSimpleHttpHints() { |
| 633 | + RuntimeHints hints = new RuntimeHints(); |
| 634 | + new RestTemplateBuilderRuntimeHints().registerHints(hints, getClass().getClassLoader()); |
| 635 | + ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection(); |
| 636 | + assertThat(reflection.onMethod( |
| 637 | + ReflectionUtils.findMethod(SimpleClientHttpRequestFactory.class, "setConnectTimeout", int.class))) |
| 638 | + .accepts(hints); |
| 639 | + assertThat(reflection.onMethod( |
| 640 | + ReflectionUtils.findMethod(SimpleClientHttpRequestFactory.class, "setReadTimeout", int.class))) |
| 641 | + .accepts(hints); |
| 642 | + assertThat(reflection.onMethod(ReflectionUtils.findMethod(SimpleClientHttpRequestFactory.class, |
| 643 | + "setBufferRequestBody", boolean.class))).accepts(hints); |
| 644 | + } |
| 645 | + |
588 | 646 | private ClientHttpRequest createRequest(RestTemplate template) {
|
589 | 647 | return ReflectionTestUtils.invokeMethod(template, "createRequest", URI.create("http://localhost"),
|
590 | 648 | HttpMethod.GET);
|
|
0 commit comments