diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java index 7fd80a8f..ab6761d8 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java @@ -137,7 +137,12 @@ public class ResourceController { return binary(request, name, profile, label, path); } - + @RequestMapping(value = "/{name}/{profile}/**", params = "useDefaultLabel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) + public byte[] binary(@PathVariable String name, @PathVariable String profile, + ServletWebRequest request) throws IOException { + String path = getFilePath(request, name, profile, null); + return binary(request, name, profile, null, path); + } /* * Used only for unit tests. diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java index f25d1adf..b790e044 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java @@ -31,12 +31,14 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.ByteArrayResource; +import org.springframework.http.HttpHeaders; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.MimeTypeUtils; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @@ -60,7 +62,7 @@ public class ResourceControllerIntegrationTests { @Before public void init() { - Mockito.reset(this.repository); + Mockito.reset(this.repository, this.resources); this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); } @@ -89,6 +91,20 @@ public class ResourceControllerIntegrationTests { Mockito.verify(this.resources).findOne("foo", "default", null, "foo.txt"); } + @Test + public void binaryResourceNoLabel() throws Exception { + Mockito.when(this.repository.findOne("foo", "default", null)) + .thenReturn(new Environment("foo", "default", "master")); + Mockito.when(this.resources.findOne("foo", "default", null, "foo.txt")) + .thenReturn(new ByteArrayResource("hello".getBytes())); + this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/foo.txt") + .param("useDefaultLabel", "") + .header(HttpHeaders.ACCEPT, MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE)) + .andExpect(MockMvcResultMatchers.status().isOk()); + Mockito.verify(this.repository).findOne("foo", "default", null); + Mockito.verify(this.resources).findOne("foo", "default", null, "foo.txt"); + } + @Configuration @EnableWebMvc @Import(PropertyPlaceholderAutoConfiguration.class) diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java index 95bc3ef4..2ec4495a 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java @@ -30,6 +30,7 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.web.context.request.ServletWebRequest; +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.assertTrue; @@ -268,4 +269,15 @@ public class ResourceControllerTests { assertEquals("foo: dev_bar/spam", new String(resource)); } + @Test + public void defaultLabelForBinary() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}"); + MockHttpServletRequest request = new MockHttpServletRequest(); + ServletWebRequest webRequest = new ServletWebRequest(request, + new MockHttpServletResponse()); + request.setRequestURI("/dev/spam/bar/" + "foo.txt"); + byte[] resource = this.controller.binary("dev/spam", "bar", webRequest); + assertThat(new String(resource)).isEqualToIgnoringNewLines("foo: dev_bar/spam"); + } + }