diff options
author | David Pursehouse <david.pursehouse@gmail.com> | 2016-07-22 14:35:18 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@gmail.com> | 2016-07-27 11:07:05 +0900 |
commit | e27eab26e2027baba7e96a972b90b17f18843467 (patch) | |
tree | a09e4ee74546c131d347255a831fa4f174acfd03 /org.eclipse.jgit.lfs.server.test/tst | |
parent | 2e652aebab329e805beb8198dea6a064bd64ec1f (diff) | |
download | jgit-e27eab26e2027baba7e96a972b90b17f18843467.tar.gz jgit-e27eab26e2027baba7e96a972b90b17f18843467.zip |
FileLfsServlet: Include error message in response body
According to the specification [1], the error response body must
include the error message in json format.
[1] https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md#response-errors
Change-Id: I79e7a841d230fdedefa53b9c6d2d477e81e1f9e6
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.lfs.server.test/tst')
-rw-r--r-- | org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/DownloadTest.java | 27 | ||||
-rw-r--r-- | org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/LfsServerTest.java | 18 |
2 files changed, 33 insertions, 12 deletions
diff --git a/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/DownloadTest.java b/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/DownloadTest.java index ac707eacf0..f15edca01a 100644 --- a/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/DownloadTest.java +++ b/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/DownloadTest.java @@ -72,14 +72,15 @@ public class DownloadTest extends LfsServerTest { public void testDownloadInvalidPathInfo() throws ClientProtocolException, IOException { String TEXT = "test"; - AnyLongObjectId id = putContent(TEXT); + String id = putContent(TEXT).name().substring(0, 60); Path f = Paths.get(getTempDirectory().toString(), "download"); try { - getContent(id.name().substring(0, 60), f); + getContent(id, f); fail("expected RuntimeException"); } catch (RuntimeException e) { - assertEquals("Status: 422 Unprocessable Entity", - e.getMessage()); + String error = String.format( + "Invalid pathInfo '/%s' does not match '/{SHA-256}'", id); + assertEquals(formatErrorMessage(422, error), e.getMessage()); } } @@ -87,14 +88,14 @@ public class DownloadTest extends LfsServerTest { public void testDownloadInvalidId() throws ClientProtocolException, IOException { String TEXT = "test"; - AnyLongObjectId id = putContent(TEXT); + String id = putContent(TEXT).name().replace('f', 'z'); Path f = Paths.get(getTempDirectory().toString(), "download"); try { - getContent(id.name().replace('f', 'z'), f); + getContent(id, f); fail("expected RuntimeException"); } catch (RuntimeException e) { - assertEquals("Status: 422 Unprocessable Entity", - e.getMessage()); + String error = String.format("Invalid id: : %s", id); + assertEquals(formatErrorMessage(422, error), e.getMessage()); } } @@ -108,8 +109,8 @@ public class DownloadTest extends LfsServerTest { getContent(id, f); fail("expected RuntimeException"); } catch (RuntimeException e) { - assertEquals("Status: 404 Not Found", - e.getMessage()); + String error = String.format("Object '%s' not found", id.getName()); + assertEquals(formatErrorMessage(404, error), e.getMessage()); } } @@ -129,4 +130,10 @@ public class DownloadTest extends LfsServerTest { FileUtils.delete(f.toFile(), FileUtils.RETRY); } + + @SuppressWarnings("boxing") + private String formatErrorMessage(int status, String message) { + return String.format("Status: %d {\n \"message\": \"%s\"\n}", status, + message); + } } diff --git a/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/LfsServerTest.java b/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/LfsServerTest.java index 8c266d4830..b4a4228901 100644 --- a/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/LfsServerTest.java +++ b/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/LfsServerTest.java @@ -45,6 +45,7 @@ package org.eclipse.jgit.lfs.server.fs; import static org.junit.Assert.assertEquals; import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -186,8 +187,21 @@ public abstract class LfsServerTest { StatusLine statusLine = response.getStatusLine(); int status = statusLine.getStatusCode(); if (statusLine.getStatusCode() >= 400) { - throw new RuntimeException("Status: " + status + " " - + statusLine.getReasonPhrase()); + String error; + try { + BufferedInputStream bis = new BufferedInputStream( + response.getEntity().getContent()); + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + int result = bis.read(); + while (result != -1) { + buf.write((byte) result); + result = bis.read(); + } + error = buf.toString(); + } catch (IOException e) { + error = statusLine.getReasonPhrase(); + } + throw new RuntimeException("Status: " + status + " " + error); } assertEquals(200, status); } |