Skip to content

Commit

Permalink
Return null result properly for cloud function (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo authored Jul 26, 2018
1 parent 0cfc67e commit 5795828
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions parse/src/main/java/com/parse/ParseCloudCodeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public T then(Task<JSONObject> task) {
/* package for test */ Object convertCloudResponse(Object result) {
if (result instanceof JSONObject) {
JSONObject jsonResult = (JSONObject) result;
// We want to make sure we pass back a null result as null, and not a JSONObject
if (jsonResult.isNull("result")) {
return null;
}
result = jsonResult.opt("result");
}

Expand Down
22 changes: 22 additions & 0 deletions parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ public void testCallFunctionInBackgroundFailure() throws Exception {

//endregion

@Test
public void testCallFunctionWithNullResult() throws Exception {
String content = "{ result: null }";

ParseHttpResponse mockResponse = new ParseHttpResponse.Builder()
.setStatusCode(200)
.setTotalSize((long) content.length())
.setContent(new ByteArrayInputStream(content.getBytes()))
.build();

ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse);
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);

Task<String> cloudCodeTask = controller.callFunctionInBackground(
"test", new HashMap<String, Object>(), "sessionToken");
ParseTaskUtils.wait(cloudCodeTask);

verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
String result = cloudCodeTask.getResult();
assertEquals(null, result);
}

private ParseHttpClient mockParseHttpClientWithReponse(ParseHttpResponse response)
throws IOException {
ParseHttpClient client = mock(ParseHttpClient.class);
Expand Down

0 comments on commit 5795828

Please sign in to comment.