|
16 | 16 |
|
17 | 17 | package org.springframework.web.service.registry; |
18 | 18 |
|
| 19 | +import java.net.URI; |
19 | 20 | import java.util.List; |
20 | 21 | import java.util.function.Predicate; |
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.ArgumentCaptor; |
23 | 25 | import org.mockito.Mockito; |
24 | 26 |
|
25 | 27 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
26 | 28 | import org.springframework.http.client.ClientHttpRequestFactory; |
27 | 29 | import org.springframework.util.LinkedMultiValueMap; |
28 | 30 | import org.springframework.util.MultiValueMap; |
| 31 | +import org.springframework.util.StringValueResolver; |
29 | 32 | import org.springframework.web.client.RestClient; |
30 | 33 | import org.springframework.web.client.support.RestClientHttpServiceGroupConfigurer; |
| 34 | +import org.springframework.web.service.annotation.GetExchange; |
| 35 | +import org.springframework.web.service.annotation.HttpExchange; |
31 | 36 | import org.springframework.web.service.invoker.HttpServiceProxyFactory; |
32 | 37 | import org.springframework.web.service.registry.echo.EchoA; |
33 | 38 | import org.springframework.web.service.registry.echo.EchoB; |
|
37 | 42 | import org.springframework.web.testfixture.http.client.MockClientHttpResponse; |
38 | 43 |
|
39 | 44 | import static org.assertj.core.api.Assertions.assertThat; |
| 45 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
40 | 46 | import static org.mockito.ArgumentMatchers.any; |
41 | 47 | import static org.mockito.BDDMockito.given; |
42 | 48 | import static org.mockito.Mockito.atLeastOnce; |
@@ -95,6 +101,87 @@ void initializeClientBuilder() throws Exception { |
95 | 101 | verify(requestFactory, atLeastOnce()).createRequest(any(), any()); |
96 | 102 | } |
97 | 103 |
|
| 104 | + @Test |
| 105 | + void propertyPlaceholderInHttpExchangeUrlIsResolved() throws Exception { |
| 106 | + GroupsMetadata groupsMetadata = new GroupsMetadata(); |
| 107 | + groupsMetadata.getOrCreateGroup("testGroup", REST_CLIENT) |
| 108 | + .httpServiceTypeNames() |
| 109 | + .add(PlaceholderService.class.getName()); |
| 110 | + |
| 111 | + ClientHttpRequestFactory requestFactory = Mockito.mock(ClientHttpRequestFactory.class); |
| 112 | + MockClientHttpRequest mockRequest = new MockClientHttpRequest(); |
| 113 | + mockRequest.setResponse(new MockClientHttpResponse()); |
| 114 | + |
| 115 | + ArgumentCaptor<URI> uriCaptor = ArgumentCaptor.forClass(URI.class); |
| 116 | + given(requestFactory.createRequest(uriCaptor.capture(), any())).willReturn(mockRequest); |
| 117 | + |
| 118 | + StringValueResolver resolver = value -> { |
| 119 | + if (value.contains("${test.base.url}")) { |
| 120 | + return value.replace("${test.base.url}", "https://api.example.com"); |
| 121 | + } |
| 122 | + return value; |
| 123 | + }; |
| 124 | + |
| 125 | + RestClient.Builder clientBuilder = RestClient.builder().requestFactory(requestFactory); |
| 126 | + |
| 127 | + RestClientHttpServiceGroupConfigurer configurer = groups -> groups.forEachClient(group -> clientBuilder); |
| 128 | + |
| 129 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 130 | + context.registerBean(RestClientHttpServiceGroupConfigurer.class, () -> configurer); |
| 131 | + context.refresh(); |
| 132 | + |
| 133 | + HttpServiceProxyRegistryFactoryBean factoryBean = new HttpServiceProxyRegistryFactoryBean(groupsMetadata); |
| 134 | + |
| 135 | + factoryBean.setApplicationContext(context); |
| 136 | + factoryBean.setBeanClassLoader(getClass().getClassLoader()); |
| 137 | + factoryBean.setEmbeddedValueResolver(resolver); |
| 138 | + factoryBean.afterPropertiesSet(); |
| 139 | + |
| 140 | + HttpServiceProxyRegistry registry = factoryBean.getObject(); |
| 141 | + PlaceholderService service = registry.getClient(PlaceholderService.class); |
| 142 | + service.callEndpoint(); |
| 143 | + |
| 144 | + URI requestedUri = uriCaptor.getValue(); |
| 145 | + |
| 146 | + assertThat(requestedUri.toString()) |
| 147 | + .startsWith("https://api.example.com") |
| 148 | + .doesNotContain("${") |
| 149 | + .contains("/endpoint"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void withoutResolverPlaceholderRemainsUnresolved() throws Exception { |
| 154 | + GroupsMetadata groupsMetadata = new GroupsMetadata(); |
| 155 | + groupsMetadata.getOrCreateGroup("testGroup", REST_CLIENT) |
| 156 | + .httpServiceTypeNames() |
| 157 | + .add(PlaceholderService.class.getName()); |
| 158 | + |
| 159 | + ClientHttpRequestFactory requestFactory = Mockito.mock(ClientHttpRequestFactory.class); |
| 160 | + MockClientHttpRequest capturedRequest = new MockClientHttpRequest(); |
| 161 | + capturedRequest.setResponse(new MockClientHttpResponse()); |
| 162 | + given(requestFactory.createRequest(any(), any())).willReturn(capturedRequest); |
| 163 | + |
| 164 | + RestClient.Builder clientBuilder = RestClient.builder().requestFactory(requestFactory); |
| 165 | + RestClientHttpServiceGroupConfigurer configurer = groups -> |
| 166 | + groups.forEachClient(group -> clientBuilder); |
| 167 | + |
| 168 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 169 | + context.registerBean(RestClientHttpServiceGroupConfigurer.class, () -> configurer); |
| 170 | + context.refresh(); |
| 171 | + |
| 172 | + HttpServiceProxyRegistryFactoryBean factoryBean = new HttpServiceProxyRegistryFactoryBean(groupsMetadata); |
| 173 | + factoryBean.setApplicationContext(context); |
| 174 | + factoryBean.setBeanClassLoader(getClass().getClassLoader()); |
| 175 | + factoryBean.afterPropertiesSet(); |
| 176 | + |
| 177 | + HttpServiceProxyRegistry registry = factoryBean.getObject(); |
| 178 | + PlaceholderService service = registry.getClient(PlaceholderService.class); |
| 179 | + |
| 180 | + assertThatThrownBy(service::callEndpoint) |
| 181 | + .isInstanceOf(IllegalArgumentException.class) |
| 182 | + .hasMessageContaining("test.base.url"); |
| 183 | + } |
| 184 | + |
98 | 185 | private HttpServiceProxyRegistry initProxyRegistry( |
99 | 186 | RestClientHttpServiceGroupConfigurer groupConfigurer, GroupsMetadata groupsMetadata) { |
100 | 187 |
|
@@ -136,4 +223,10 @@ public void withGroup(HttpServiceGroup group, RestClient clientBuilder, HttpServ |
136 | 223 | } |
137 | 224 | } |
138 | 225 |
|
| 226 | + @HttpExchange(url = "${test.base.url}") |
| 227 | + interface PlaceholderService { |
| 228 | + |
| 229 | + @GetExchange("/endpoint") |
| 230 | + String callEndpoint(); |
| 231 | + } |
139 | 232 | } |
0 commit comments