mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
Handle empty files in content
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user