mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
Handle exception in stream processing
This commit is contained in:
@@ -18,6 +18,7 @@ import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
|
||||
@@ -64,8 +65,7 @@ public class ContentResourceTest {
|
||||
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "file");
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
((StreamingOutput) response.getEntity()).write(baos);
|
||||
ByteArrayOutputStream baos = readOutputStream(response);
|
||||
|
||||
assertEquals("Hello", baos.toString());
|
||||
}
|
||||
@@ -115,6 +115,15 @@ public class ContentResourceTest {
|
||||
assertEquals("application/octet-stream", response.getHeaderString("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleExceptionsInStreamProcessing() throws Exception {
|
||||
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "MissingFile");
|
||||
|
||||
ByteArrayOutputStream baos = readOutputStream(response);
|
||||
|
||||
assertEquals(404, response.getStatus());
|
||||
}
|
||||
|
||||
private void mockContentFromResource(String fileName) throws Exception {
|
||||
URL url = Resources.getResource(fileName);
|
||||
mockContent(fileName, Resources.toByteArray(url));
|
||||
@@ -128,4 +137,10 @@ public class ContentResourceTest {
|
||||
return null;
|
||||
}).when(catCommand).retriveContent(any(), eq(path));
|
||||
}
|
||||
|
||||
private ByteArrayOutputStream readOutputStream(Response response) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
((StreamingOutput) response.getEntity()).write(baos);
|
||||
return baos;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user