GH-1337: Add support for using default label when serving binary resources (#1340)

resolves #1337
closes #1338
This commit is contained in:
Anshul Mehra
2019-04-09 10:17:59 -04:00
committed by Ryan Baxter
parent a694760135
commit 92307647e9
3 changed files with 35 additions and 2 deletions

View File

@@ -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.

View File

@@ -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)

View File

@@ -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");
}
}