@Test public void testOnResponseCloseNetworkStreamWithNormalResponse() throws Exception { int statusCode = 200; JSONObject bodyJson = new JSONObject(); bodyJson.put("key", "value"); String bodyStr = bodyJson.toString(); ByteArrayInputStream bodyStream = new ByteArrayInputStream(bodyStr.getBytes()); InputStream mockResponseStream = spy(bodyStream); doNothing() .when(mockResponseStream) .close(); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(statusCode) .setTotalSize((long) bodyStr.length()) .setContent(mockResponseStream) .build(); ParseRESTCommand command = new ParseRESTCommand.Builder().build(); JSONObject json = ParseTaskUtils.wait(command.onResponseAsync(mockResponse, null)); verify(mockResponseStream, times(1)).close(); assertEquals(bodyJson, json, JSONCompareMode.NON_EXTENSIBLE); }
@Override protected Task<JSONObject> onResponseAsync(ParseHttpResponse response, ProgressCallback downloadProgressCallback) { String content; InputStream responseStream = null; try { responseStream = response.getContent(); content = new String(ParseIOUtils.toByteArray(responseStream)); } catch (IOException e) { return Task.forError(e); } finally { ParseIOUtils.closeQuietly(responseStream); } int statusCode = response.getStatusCode(); if (statusCode >= 200 && statusCode < 600) { JSONObject json; try { json = new JSONObject(content); if (statusCode >= 400 && statusCode < 500) { return Task.forError(newPermanentException(json.optInt("code"), json.optString("error"))); } else if (statusCode >= 500) { return Task.forError(newTemporaryException(json.optInt("code"), json.optString("error"))); } return Task.forResult(json); } catch (JSONException e) { return Task.forError(newTemporaryException("bad json response", e)); } } return Task.forError(newPermanentException(ParseException.OTHER_CAUSE, content)); }
ParseRESTCommand extends ParseRequest<JSONObject> { @Override protected Task<JSONObject> onResponseAsync(ParseHttpResponse response, ProgressCallback downloadProgressCallback) { String content; InputStream responseStream = null; try { responseStream = response.getContent(); content = new String(ParseIOUtils.toByteArray(responseStream)); } catch (IOException e) { return Task.forError(e); } finally { ParseIOUtils.closeQuietly(responseStream); } int statusCode = response.getStatusCode(); if (statusCode >= 200 && statusCode < 600) { JSONObject json; try { json = new JSONObject(content); if (statusCode >= 400 && statusCode < 500) { return Task.forError(newPermanentException(json.optInt("code"), json.optString("error"))); } else if (statusCode >= 500) { return Task.forError(newTemporaryException(json.optInt("code"), json.optString("error"))); } return Task.forResult(json); } catch (JSONException e) { return Task.forError(newTemporaryException("bad json response", e)); } } return Task.forError(newPermanentException(ParseException.OTHER_CAUSE, content)); } }
ParseRESTCommand extends ParseRequest<JSONObject> { @Override protected Task<JSONObject> onResponseAsync(ParseHttpResponse response, ProgressCallback downloadProgressCallback) { String content; InputStream responseStream = null; try { responseStream = response.getContent(); content = new String(ParseIOUtils.toByteArray(responseStream)); } catch (IOException e) { return Task.forError(e); } finally { ParseIOUtils.closeQuietly(responseStream); } int statusCode = response.getStatusCode(); if (statusCode >= 200 && statusCode < 600) { JSONObject json; try { json = new JSONObject(content); if (statusCode >= 400 && statusCode < 500) { return Task.forError(newPermanentException(json.optInt("code"), json.optString("error"))); } else if (statusCode >= 500) { return Task.forError(newTemporaryException(json.optInt("code"), json.optString("error"))); } return Task.forResult(json); } catch (JSONException e) { return Task.forError(newTemporaryException("bad json response", e)); } } return Task.forError(newPermanentException(ParseException.OTHER_CAUSE, content)); } ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            Map<String, ?> parameters,
            String sessionToken);  ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            JSONObject jsonParameters,
            String sessionToken); private  ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            JSONObject jsonParameters,
            String localId, String sessionToken);   ParseRESTCommand(Init<?> builder); }
ParseRESTCommand extends ParseRequest<JSONObject> { @Override protected Task<JSONObject> onResponseAsync(ParseHttpResponse response, ProgressCallback downloadProgressCallback) { String content; InputStream responseStream = null; try { responseStream = response.getContent(); content = new String(ParseIOUtils.toByteArray(responseStream)); } catch (IOException e) { return Task.forError(e); } finally { ParseIOUtils.closeQuietly(responseStream); } int statusCode = response.getStatusCode(); if (statusCode >= 200 && statusCode < 600) { JSONObject json; try { json = new JSONObject(content); if (statusCode >= 400 && statusCode < 500) { return Task.forError(newPermanentException(json.optInt("code"), json.optString("error"))); } else if (statusCode >= 500) { return Task.forError(newTemporaryException(json.optInt("code"), json.optString("error"))); } return Task.forResult(json); } catch (JSONException e) { return Task.forError(newTemporaryException("bad json response", e)); } } return Task.forError(newPermanentException(ParseException.OTHER_CAUSE, content)); } ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            Map<String, ?> parameters,
            String sessionToken);  ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            JSONObject jsonParameters,
            String sessionToken); private  ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            JSONObject jsonParameters,
            String localId, String sessionToken);   ParseRESTCommand(Init<?> builder); static ParseRESTCommand fromJSONObject(JSONObject jsonObject); @Override Task<JSONObject> executeAsync(
            final ParseHttpClient client,
            final ProgressCallback uploadProgressCallback,
            final ProgressCallback downloadProgressCallback,
            final Task<Void> cancellationToken); String getCacheKey(); JSONObject toJSONObject(); String getSessionToken(); String getOperationSetUUID(); String getLocalId(); void setLocalId(String localId); void resolveLocalIds(); void retainLocalIds(); void releaseLocalIds(); }
ParseRESTCommand extends ParseRequest<JSONObject> { @Override protected Task<JSONObject> onResponseAsync(ParseHttpResponse response, ProgressCallback downloadProgressCallback) { String content; InputStream responseStream = null; try { responseStream = response.getContent(); content = new String(ParseIOUtils.toByteArray(responseStream)); } catch (IOException e) { return Task.forError(e); } finally { ParseIOUtils.closeQuietly(responseStream); } int statusCode = response.getStatusCode(); if (statusCode >= 200 && statusCode < 600) { JSONObject json; try { json = new JSONObject(content); if (statusCode >= 400 && statusCode < 500) { return Task.forError(newPermanentException(json.optInt("code"), json.optString("error"))); } else if (statusCode >= 500) { return Task.forError(newTemporaryException(json.optInt("code"), json.optString("error"))); } return Task.forResult(json); } catch (JSONException e) { return Task.forError(newTemporaryException("bad json response", e)); } } return Task.forError(newPermanentException(ParseException.OTHER_CAUSE, content)); } ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            Map<String, ?> parameters,
            String sessionToken);  ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            JSONObject jsonParameters,
            String sessionToken); private  ParseRESTCommand(
            String httpPath,
            ParseHttpRequest.Method httpMethod,
            JSONObject jsonParameters,
            String localId, String sessionToken);   ParseRESTCommand(Init<?> builder); static ParseRESTCommand fromJSONObject(JSONObject jsonObject); @Override Task<JSONObject> executeAsync(
            final ParseHttpClient client,
            final ProgressCallback uploadProgressCallback,
            final ProgressCallback downloadProgressCallback,
            final Task<Void> cancellationToken); String getCacheKey(); JSONObject toJSONObject(); String getSessionToken(); String getOperationSetUUID(); String getLocalId(); void setLocalId(String localId); void resolveLocalIds(); void retainLocalIds(); void releaseLocalIds(); public String masterKey; }