Search in sources :

Example 11 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseConfigControllerTest method mockParseHttpClientWithResponse.

//TODO(mengyan) Create unit test helper and move all similar methods to the class
private ParseHttpClient mockParseHttpClientWithResponse(JSONObject content, int statusCode, String reasonPhrase) throws IOException {
    byte[] contentBytes = content.toString().getBytes();
    ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(contentBytes)).setStatusCode(statusCode).setTotalSize(contentBytes.length).setContentType("application/json").build();
    ParseHttpClient client = mock(ParseHttpClient.class);
    when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
    return client;
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Example 12 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseRESTCommandTest method testPermanentFailures.

@Test
public void testPermanentFailures() throws Exception {
    JSONObject json = new JSONObject();
    json.put("code", 1337);
    json.put("error", "mock error");
    ParseHttpResponse response = newMockParseHttpResponse(400, 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(1337, ((ParseException) task.getError()).getCode());
    assertEquals("mock error", task.getError().getMessage());
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) JSONObject(org.json.JSONObject) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 13 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.

@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
    // Make mock response
    Buffer buffer = new Buffer();
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write("content".getBytes());
    gzipOut.close();
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
    // Start mock server
    server.enqueue(mockResponse);
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    final Semaphore done = new Semaphore(0);
    // Add plain interceptor to disable decompress response stream
    client.addExternalInterceptor(new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
            // Make sure the response we get from the interceptor is the raw gzip stream
            byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
            assertArrayEquals(byteOut.toByteArray(), content);
            // We need to set a new stream since we have read it
            return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
        }
    });
    // We do not need to add Accept-Encoding header manually, httpClient library should do that.
    String requestUrl = server.url("/").toString();
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);
    // Make sure the response we get is ungziped by OkHttp library
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals("content".getBytes(), content);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
    server.shutdown();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 14 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParsePushControllerTest method mockParseHttpClientWithResponse.

//endregion
private ParseHttpClient mockParseHttpClientWithResponse(JSONObject content, int statusCode, String reasonPhrase) throws IOException {
    byte[] contentBytes = content.toString().getBytes();
    ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(contentBytes)).setStatusCode(statusCode).setTotalSize(contentBytes.length).setContentType("application/json").build();
    ParseHttpClient client = mock(ParseHttpClient.class);
    when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
    return client;
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Example 15 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseTestUtils method mockParseHttpClientWithResponse.

public static ParseHttpClient mockParseHttpClientWithResponse(JSONObject content, int statusCode, String reasonPhrase) throws IOException {
    byte[] contentBytes = content.toString().getBytes();
    ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(contentBytes)).setStatusCode(statusCode).setTotalSize(contentBytes.length).setContentType("application/json").build();
    ParseHttpClient client = mock(ParseHttpClient.class);
    when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
    return client;
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) 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