Skip to content

Commit 497cfce

Browse files
author
Nakul Sabharwal
committed
Null check while creating GraphError when body is empty.
1 parent eda89fb commit 497cfce

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/main/java/com/microsoft/graph/http/GraphServiceException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
311311
}
312312

313313
final String responseMessage = connection.getResponseMessage();
314-
final String rawOutput = DefaultHttpProvider.streamToString(connection.getInputStream());
314+
String rawOutput = "{}";
315+
if(connection.getInputStream() != null) {
316+
rawOutput = DefaultHttpProvider.streamToString(connection.getInputStream());
317+
}
315318
GraphErrorResponse error;
316319
try {
317320
error = serializer.deserializeObject(rawOutput, GraphErrorResponse.class, connection.getResponseHeaders());

src/test/java/com/microsoft/graph/http/GraphServiceExceptionTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertTrue;
66

7+
import java.io.ByteArrayInputStream;
78
import java.io.IOException;
9+
import java.io.InputStream;
810
import java.util.ArrayList;
911
import java.util.HashMap;
1012
import java.util.Map;
@@ -80,5 +82,47 @@ public Map<String, String> getHeaders() {
8082
assertTrue(message.indexOf("Error code: Unable to parse error response message") == 0);
8183
assertTrue(message.indexOf("http://localhost") > 0);
8284
}
85+
86+
@Test
87+
public void testNullConnection() {
88+
DefaultLogger logger = new DefaultLogger();
89+
GraphServiceException exception = null;
90+
Boolean success = false;
91+
Boolean failure = false;
92+
final ITestConnectionData data = new ITestConnectionData() {
93+
@Override
94+
public int getRequestCode() {
95+
return 401;
96+
}
97+
98+
@Override
99+
public String getJsonResponse() {
100+
return "{}";
101+
}
102+
103+
@Override
104+
public Map<String, String> getHeaders() {
105+
return new HashMap<>();
106+
}
107+
};
108+
try{
109+
exception = GraphServiceException.createFromConnection(new MockHttpRequest(),null,null,new MockConnection(data) {
110+
@Override
111+
public InputStream getInputStream() throws IOException {
112+
return null;
113+
}
114+
},logger);
115+
success = true;
116+
}catch (IOException ex){
117+
failure = true;
118+
}
119+
120+
assertTrue(success);
121+
assertFalse(failure);
122+
123+
String message = exception.getMessage();
124+
assertTrue(message.indexOf("Error code: Unable to parse error response message") == 0);
125+
assertTrue(message.indexOf("http://localhost") > 0);
126+
}
83127

84128
}

0 commit comments

Comments
 (0)