From 7525e556113a5c04411fd481be54966c95185c23 Mon Sep 17 00:00:00 2001 From: Alex Muthmann Date: Tue, 23 Oct 2018 09:26:13 +0200 Subject: [PATCH] Adds Accept header to ensure config client requests json Fixes gh-1169 --- .../ConfigServicePropertySourceLocator.java | 2 ++ ...nfigServicePropertySourceLocatorTests.java | 34 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) 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 da848a6b..dcc836c7 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; @@ -213,6 +214,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 c5e6021f..1acb651b 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 @@ -1,23 +1,20 @@ package org.springframework.cloud.config.client; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - import java.io.ByteArrayInputStream; import java.net.URI; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import org.assertj.core.api.Assertions; import org.hamcrest.core.IsInstanceOf; -import org.hamcrest.core.IsNull; 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; + import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.GenericRequestHeaderInterceptor; import org.springframework.cloud.config.environment.Environment; @@ -34,12 +31,16 @@ import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; -import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.mock.http.client.MockClientHttpRequest; import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.springframework.cloud.config.client.ConfigClientProperties.AUTHORIZATION; public class ConfigServicePropertySourceLocatorTests { @@ -59,7 +60,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); + assertNotNull(this.locator.locate(this.environment)); + + Mockito.verify(this.restTemplate).exchange(anyString(), any(HttpMethod.class), + argumentCaptor.capture(), any(Class.class), anyString(), anyString()); + + HttpEntity httpEntity = argumentCaptor.getValue(); + Assertions.assertThat(httpEntity.getHeaders().getAccept()) + .containsExactly(MediaType.APPLICATION_JSON); } @Test @@ -259,7 +271,7 @@ 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(), + Mockito.any(Class.class), anyString(), anyString(), Matchers.eq(label))).thenReturn(response); } @@ -267,7 +279,7 @@ public class ConfigServicePropertySourceLocatorTests { 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())) + Mockito.any(Class.class), anyString(), anyString())) .thenReturn(response); } @@ -277,7 +289,7 @@ public class ConfigServicePropertySourceLocatorTests { 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); + anyString())).thenReturn(response); } }