|
19 | 19 | import java.time.Duration;
|
20 | 20 | import java.util.ArrayList;
|
21 | 21 | import java.util.List;
|
22 |
| -import java.util.Optional; |
23 | 22 |
|
24 | 23 | import co.elastic.clients.transport.rest5_client.low_level.Node;
|
25 | 24 | import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
|
|
49 | 48 | import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
|
50 | 49 | import org.springframework.context.annotation.Bean;
|
51 | 50 | import org.springframework.context.annotation.Configuration;
|
52 |
| -import org.springframework.web.client.RestClient; |
53 | 51 |
|
54 | 52 | import static org.assertj.core.api.Assertions.assertThat;
|
55 | 53 | import static org.mockito.BDDMockito.then;
|
@@ -138,25 +136,6 @@ void configureUriWithNoScheme() {
|
138 | 136 | });
|
139 | 137 | }
|
140 | 138 |
|
141 |
| - @Test |
142 |
| - void configureUriWithAPiKey() { |
143 |
| - this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200","spring.elasticsearch.apikey=some-apiKey").run((context) -> { |
144 |
| - Rest5Client client = context.getBean(Rest5Client.class); |
145 |
| - assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) |
146 |
| - .containsExactly("http://localhost:9200"); |
147 |
| - assertThat(client) |
148 |
| - .extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class)) |
149 |
| - .satisfies(( defaultHeaders) -> { |
150 |
| - Optional<? extends Header> authHeader = defaultHeaders.stream() |
151 |
| - .filter(x -> x.getName().equals("Authorization")) |
152 |
| - .findFirst(); |
153 |
| - assertThat(authHeader).isPresent(); |
154 |
| - assertThat(authHeader.get().getValue()).isEqualTo("ApiKey some-apiKey"); |
155 |
| - }); |
156 |
| - }); |
157 |
| - } |
158 |
| - |
159 |
| - |
160 | 139 | @Test
|
161 | 140 | void configureUriWithUsernameOnly() {
|
162 | 141 | this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200").run((context) -> {
|
@@ -216,6 +195,41 @@ void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() {
|
216 | 195 | });
|
217 | 196 | }
|
218 | 197 |
|
| 198 | + @Test |
| 199 | + void whenApiKeyIsConfiguredThenAuthorizationHeaderIsPresent() { |
| 200 | + this.contextRunner.withPropertyValues("spring.elasticsearch.api-key=some-api-key").run((context) -> { |
| 201 | + Rest5Client client = context.getBean(Rest5Client.class); |
| 202 | + assertThat(client).extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class)) |
| 203 | + .satisfiesOnlyOnce((header) -> { |
| 204 | + assertThat(header.getName().equals("Authorization")); |
| 205 | + assertThat(header.getValue().equals("ApiKey some-api-key")); |
| 206 | + }); |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + void whenApiKeyAndUsernameAndPasswordAreConfiguredThenBothFormsOfCredentialsArePresent() { |
| 212 | + this.contextRunner |
| 213 | + .withPropertyValues("spring.elasticsearch.api-key=some-api-key", "spring.elasticsearch.username=alice", |
| 214 | + "spring.elasticsearch.password=secret") |
| 215 | + .run((context) -> { |
| 216 | + Rest5Client client = context.getBean(Rest5Client.class); |
| 217 | + assertThat(client).extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class)) |
| 218 | + .satisfiesOnlyOnce((header) -> { |
| 219 | + assertThat(header.getName().equals("Authorization")); |
| 220 | + assertThat(header.getValue().equals("ApiKey some-api-key")); |
| 221 | + }); |
| 222 | + assertThat(client) |
| 223 | + .extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) |
| 224 | + .satisfies((credentialsProvider) -> { |
| 225 | + UsernamePasswordCredentials defaultCredentials = (UsernamePasswordCredentials) credentialsProvider |
| 226 | + .getCredentials(new AuthScope(null, -1), null); |
| 227 | + assertThat(defaultCredentials.getUserPrincipal().getName()).isEqualTo("alice"); |
| 228 | + assertThat(defaultCredentials.getUserPassword()).containsExactly("secret".toCharArray()); |
| 229 | + }); |
| 230 | + }); |
| 231 | + } |
| 232 | + |
219 | 233 | @Test
|
220 | 234 | void configureWithCustomPathPrefix() {
|
221 | 235 | this.contextRunner.withPropertyValues("spring.elasticsearch.path-prefix=/some/prefix").run((context) -> {
|
|
0 commit comments