mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-13 23:52:10 +00:00
DATAES-607 - Polishing.
Ensure copying HttpHeaders before using these as default to prevent modifications of the original object. Adapt tests. Reuse header constants. Original pull request: #293.
This commit is contained in:
parent
c8c3cb7909
commit
cf7fc61f7b
@ -112,7 +112,9 @@ class ClientConfigurationBuilder
|
|||||||
|
|
||||||
Assert.notNull(defaultHeaders, "Default HTTP headers must not be null");
|
Assert.notNull(defaultHeaders, "Default HTTP headers must not be null");
|
||||||
|
|
||||||
this.headers = defaultHeaders;
|
this.headers = new HttpHeaders();
|
||||||
|
this.headers.addAll(defaultHeaders);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,6 +169,7 @@ class ClientConfigurationBuilder
|
|||||||
}
|
}
|
||||||
headers.setBasicAuth(username, password);
|
headers.setBasicAuth(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DefaultClientConfiguration(this.hosts, this.headers, this.useSsl, this.sslContext, this.soTimeout,
|
return new DefaultClientConfiguration(this.hosts, this.headers, this.useSsl, this.sslContext, this.soTimeout,
|
||||||
this.connectTimeout);
|
this.connectTimeout);
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,6 @@ import org.springframework.http.HttpHeaders;
|
|||||||
*/
|
*/
|
||||||
public class ClientConfigurationUnitTests {
|
public class ClientConfigurationUnitTests {
|
||||||
|
|
||||||
private static final String AUTHORIZATION_HEADER = "Authorization";
|
|
||||||
|
|
||||||
@Test // DATAES-488
|
@Test // DATAES-488
|
||||||
public void shouldCreateSimpleConfiguration() {
|
public void shouldCreateSimpleConfiguration() {
|
||||||
|
|
||||||
@ -85,23 +83,23 @@ public class ClientConfigurationUnitTests {
|
|||||||
@Test // DATAES-607
|
@Test // DATAES-607
|
||||||
public void shouldAddBasicAuthenticationHeaderWhenNoHeadersAreSet() {
|
public void shouldAddBasicAuthenticationHeaderWhenNoHeadersAreSet() {
|
||||||
|
|
||||||
final String username = "secretUser";
|
String username = "secretUser";
|
||||||
final String password = "secretPassword";
|
String password = "secretPassword";
|
||||||
|
|
||||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder() //
|
ClientConfiguration clientConfiguration = ClientConfiguration.builder() //
|
||||||
.connectedTo("foo", "bar") //
|
.connectedTo("foo", "bar") //
|
||||||
.withBasicAuth(username, password) //
|
.withBasicAuth(username, password) //
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assertThat(clientConfiguration.getDefaultHeaders().get(AUTHORIZATION_HEADER))
|
assertThat(clientConfiguration.getDefaultHeaders().get(HttpHeaders.AUTHORIZATION))
|
||||||
.containsOnly(buildBasicAuth(username, password));
|
.containsOnly(buildBasicAuth(username, password));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-607
|
@Test // DATAES-607
|
||||||
public void shouldAddBasicAuthenticationHeaderAndKeepHeaders() {
|
public void shouldAddBasicAuthenticationHeaderAndKeepHeaders() {
|
||||||
|
|
||||||
final String username = "secretUser";
|
String username = "secretUser";
|
||||||
final String password = "secretPassword";
|
String password = "secretPassword";
|
||||||
|
|
||||||
HttpHeaders defaultHeaders = new HttpHeaders();
|
HttpHeaders defaultHeaders = new HttpHeaders();
|
||||||
defaultHeaders.set("foo", "bar");
|
defaultHeaders.set("foo", "bar");
|
||||||
@ -109,17 +107,20 @@ public class ClientConfigurationUnitTests {
|
|||||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder() //
|
ClientConfiguration clientConfiguration = ClientConfiguration.builder() //
|
||||||
.connectedTo("foo", "bar") //
|
.connectedTo("foo", "bar") //
|
||||||
.withBasicAuth(username, password) //
|
.withBasicAuth(username, password) //
|
||||||
.withDefaultHeaders(defaultHeaders).build();
|
.withDefaultHeaders(defaultHeaders) //
|
||||||
final HttpHeaders httpHeaders = clientConfiguration.getDefaultHeaders();
|
.build();
|
||||||
|
|
||||||
assertThat(httpHeaders.get(AUTHORIZATION_HEADER)).containsOnly(buildBasicAuth(username, password));
|
HttpHeaders httpHeaders = clientConfiguration.getDefaultHeaders();
|
||||||
assertThat(httpHeaders.get("foo")).containsOnly("bar");
|
|
||||||
|
assertThat(httpHeaders.get(HttpHeaders.AUTHORIZATION)).containsOnly(buildBasicAuth(username, password));
|
||||||
|
assertThat(httpHeaders.getFirst("foo")).isEqualTo("bar");
|
||||||
|
assertThat(defaultHeaders.get(HttpHeaders.AUTHORIZATION)).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildBasicAuth(String username, String password) {
|
private static String buildBasicAuth(String username, String password) {
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setBasicAuth(username, password);
|
headers.setBasicAuth(username, password);
|
||||||
return headers.get(AUTHORIZATION_HEADER).get(0);
|
return headers.getFirst(HttpHeaders.AUTHORIZATION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user