diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 48a074b0..37e55e8d 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -18,6 +18,7 @@ package org.springframework.cloud.config.client; import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -214,6 +215,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator if (StringUtils.hasText(state) && properties.isSendState()) { headers.add(STATE_HEADER, state); } + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); final HttpEntity entity = new HttpEntity<>((Void) null, headers); response = restTemplate.exchange(uri + path, HttpMethod.GET, entity, diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java index 746a17fd..71843919 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java @@ -26,6 +26,7 @@ import org.hamcrest.core.IsInstanceOf; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; import org.mockito.Matchers; import org.mockito.Mockito; @@ -50,6 +51,8 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.springframework.cloud.config.client.ConfigClientProperties.AUTHORIZATION; public class ConfigServicePropertySourceLocatorTests { @@ -69,7 +72,18 @@ public class ConfigServicePropertySourceLocatorTests { Environment body = new Environment("app", "master"); mockRequestResponseWithoutLabel(new ResponseEntity<>(body, HttpStatus.OK)); this.locator.setRestTemplate(this.restTemplate); + + ArgumentCaptor argumentCaptor = ArgumentCaptor + .forClass(HttpEntity.class); + assertThat(this.locator.locate(this.environment)).isNotNull(); + + Mockito.verify(this.restTemplate).exchange(anyString(), any(HttpMethod.class), + argumentCaptor.capture(), any(Class.class), anyString(), anyString()); + + HttpEntity httpEntity = argumentCaptor.getValue(); + assertThat(httpEntity.getHeaders().getAccept()) + .containsExactly(MediaType.APPLICATION_JSON); } @Test @@ -269,16 +283,15 @@ public class ConfigServicePropertySourceLocatorTests { private void mockRequestResponseWithLabel(ResponseEntity response, String label) { Mockito.when(this.restTemplate.exchange(Mockito.any(String.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), - Mockito.any(Class.class), Matchers.anyString(), Matchers.anyString(), - Matchers.eq(label))).thenReturn(response); + Mockito.any(Class.class), anyString(), anyString(), Matchers.eq(label))) + .thenReturn(response); } @SuppressWarnings("unchecked") private void mockRequestResponseWithoutLabel(ResponseEntity response) { Mockito.when(this.restTemplate.exchange(Mockito.any(String.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), - Mockito.any(Class.class), Matchers.anyString(), Matchers.anyString())) - .thenReturn(response); + Mockito.any(Class.class), anyString(), anyString())).thenReturn(response); } @SuppressWarnings("unchecked") @@ -286,8 +299,8 @@ public class ConfigServicePropertySourceLocatorTests { ResponseEntity response, String expectedName) { Mockito.when(this.restTemplate.exchange(Mockito.any(String.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), - Mockito.any(Class.class), Matchers.eq(expectedName), - Matchers.anyString())).thenReturn(response); + Mockito.any(Class.class), Matchers.eq(expectedName), anyString())) + .thenReturn(response); } }