Handle empty files in content

This commit is contained in:
René Pfeuffer
2018-08-24 08:52:19 +02:00
parent 4699166f87
commit 9775817269
2 changed files with 14 additions and 12 deletions

View File

@@ -142,7 +142,9 @@ public class ContentResource {
try {
byte[] buffer = new byte[HEAD_BUFFER_SIZE];
int length = stream.read(buffer);
if (length < buffer.length) {
if (length < 0) { // empty file
return new byte[]{};
} else if (length < buffer.length) {
return Arrays.copyOf(buffer, length);
} else {
return buffer;

View File

@@ -108,17 +108,6 @@ public class ContentResourceTest {
assertEquals("text/plain", response.getHeaderString("Content-Type"));
}
@Test
public void shouldRecognizeShebangSourceCode() throws Exception {
mockContentFromResource("someScript.sh");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "someScript.sh");
assertEquals(200, response.getStatus());
assertEquals("PYTHON", response.getHeaderString("Language"));
assertEquals("application/x-sh", response.getHeaderString("Content-Type"));
}
@Test
public void shouldHandleRandomByteFile() throws Exception {
mockContentFromResource("JustBytes");
@@ -142,6 +131,17 @@ public class ContentResourceTest {
assertTrue("stream has to be closed after reading head", stream.isClosed());
}
@Test
public void shouldHandleEmptyFile() throws Exception {
mockContent("empty", new byte[]{});
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "empty");
assertEquals(200, response.getStatus());
assertFalse(response.getHeaders().containsKey("Language"));
assertEquals("application/octet-stream", response.getHeaderString("Content-Type"));
}
private void mockContentFromResource(String fileName) throws Exception {
URL url = Resources.getResource(fileName);
mockContent(fileName, Resources.toByteArray(url));