use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseRESTCommandTest method test401Unauthorized.
/**
* Test to verify that handle 401 unauthorized
*/
@Test
public void test401Unauthorized() throws Exception {
JSONObject json = new JSONObject();
json.put("error", "unauthorized");
ParseHttpResponse response = newMockParseHttpResponse(401, json);
ParseHttpClient client = mock(ParseHttpClient.class);
when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
ParseRESTCommand command = new ParseRESTCommand.Builder().method(ParseHttpRequest.Method.GET).installationId("fake_installation_id").build();
Task<JSONObject> task = command.executeAsync(client);
task.waitForCompletion();
verify(client, times(1)).execute(any(ParseHttpRequest.class));
assertTrue(task.isFaulted());
assertEquals(0, ((ParseException) task.getError()).getCode());
assertEquals("unauthorized", task.getError().getMessage());
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseRESTCommandTest method testOnResponseCloseNetworkStreamWithNormalResponse.
@Test
public void testOnResponseCloseNetworkStreamWithNormalResponse() throws Exception {
// Mock response stream
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();
// Mock response
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);
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseRESTCommandTest method testOnResposneCloseNetworkStreamWithIOException.
@Test
public void testOnResposneCloseNetworkStreamWithIOException() throws Exception {
// Mock response stream
int statusCode = 200;
InputStream mockResponseStream = mock(InputStream.class);
doNothing().when(mockResponseStream).close();
IOException readException = new IOException("Error");
doThrow(readException).when(mockResponseStream).read();
doThrow(readException).when(mockResponseStream).read(any(byte[].class));
// Mock response
ParseHttpResponse mockResponse = new ParseHttpResponse.Builder().setStatusCode(statusCode).setContent(mockResponseStream).build();
ParseRESTCommand command = new ParseRESTCommand.Builder().build();
// We can not use ParseTaskUtils here since it will replace the original exception with runtime
// exception
Task<JSONObject> responseTask = command.onResponseAsync(mockResponse, null);
responseTask.waitForCompletion();
assertTrue(responseTask.isFaulted());
assertTrue(responseTask.getError() instanceof IOException);
assertEquals("Error", responseTask.getError().getMessage());
verify(mockResponseStream, times(1)).close();
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseRESTUserCommandTest method testOnResponseAsync.
//endregion
//region testOnResponseAsync
@Test
public void testOnResponseAsync() throws Exception {
ParseRESTUserCommand command = ParseRESTUserCommand.getCurrentUserCommand("sessionToken");
String content = "content";
String contentType = "application/json";
int statusCode = 200;
ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(content.getBytes())).setContentType(contentType).setStatusCode(statusCode).build();
command.onResponseAsync(response, null);
assertEquals(200, command.getStatusCode());
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseDecompressInterceptor method intercept.
@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
ParseHttpRequest request = chain.getRequest();
ParseHttpResponse response = chain.proceed(request);
// If the response is gziped, we need to decompress the stream and remove the gzip header.
if (GZIP_ENCODING.equalsIgnoreCase(response.getHeader(CONTENT_ENCODING_HEADER))) {
Map<String, String> newHeaders = new HashMap<>(response.getAllHeaders());
newHeaders.remove(CONTENT_ENCODING_HEADER);
// Since before we decompress the stream, we can not know the actual length of the stream.
// In this situation, we follow the OkHttp library, set the content-length of the response
// to -1
newHeaders.put(CONTENT_LENGTH_HEADER, "-1");
// TODO(mengyan): Add builder constructor based on an existing ParseHttpResponse
response = new ParseHttpResponse.Builder(response).setTotalSize(-1).setHeaders(newHeaders).setContent(new GZIPInputStream(response.getContent())).build();
}
return response;
}
Aggregations