Search in sources :

Example 16 with ParseHttpResponse

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());
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) JSONObject(org.json.JSONObject) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 17 with ParseHttpResponse

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);
}
Also used : JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 18 with ParseHttpResponse

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();
}
Also used : JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 19 with ParseHttpResponse

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 20 with ParseHttpResponse

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Aggregations

ParseHttpResponse (com.parse.http.ParseHttpResponse)33 ParseHttpRequest (com.parse.http.ParseHttpRequest)26 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)20 JSONObject (org.json.JSONObject)12 ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 File (java.io.File)4 InputStream (java.io.InputStream)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 ResponseBody (okhttp3.ResponseBody)3 Buffer (okio.Buffer)3 Task (bolts.Task)2 ArrayList (java.util.ArrayList)2