Accept null for JSON problem type and detail

pull/17/merge
Richard Körber 2016-02-20 13:58:04 +01:00
parent 749abc8f99
commit 6f122e63f1
2 changed files with 33 additions and 0 deletions

View File

@ -332,6 +332,14 @@ public class DefaultConnection implements Connection {
String type = (String) map.get("type");
String detail = (String) map.get("detail");
if (detail == null) {
detail = "general problem";
}
if (type == null) {
throw new AcmeException(detail);
}
switch (type) {
case "urn:acme:error:unauthorized":
throw new AcmeUnauthorizedException(type, detail);

View File

@ -249,6 +249,31 @@ public class DefaultConnectionTest {
verifyNoMoreInteractions(mockUrlConnection);
}
/**
* Test if an {@link AcmeException} is thrown if there is no error type.
*/
@Test
public void testNoTypeThrowException() {
when(mockUrlConnection.getHeaderField("Content-Type"))
.thenReturn("application/problem+json");
try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) {
@Override
public Map<String,Object> readJsonResponse() throws AcmeException {
return new HashMap<String, Object>();
};
}) {
conn.conn = mockUrlConnection;
conn.throwAcmeException();
fail("Expected to fail");
} catch (AcmeException ex) {
assertThat(ex.getMessage(), not(isEmptyOrNullString()));
}
verify(mockUrlConnection).getHeaderField("Content-Type");
verifyNoMoreInteractions(mockUrlConnection);
}
/**
* Test GET requests.
*/