Search in sources :

Example 31 with ParseHttpResponse

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

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithInterceptor.

// This test is used to test okHttp interceptors. The difference between external and
// internal interceptor is the external interceptor is added to OkHttpClient level, an internal
// interceptor is added to ParseHttpClient level.
// In the interceptor, we change request and response to see whether our server and
// ParseHttpClient can receive the correct value.
private void testParseOkHttpClientExecuteWithInterceptor(boolean isInternalInterceptorTest) throws Exception {
    // Start mock server
    server.enqueue(generateServerResponse());
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    // Make ParseHttpRequest
    ParseHttpRequest parseRequest = generateClientRequest();
    final Semaphore done = new Semaphore(0);
    ParseNetworkInterceptor interceptor = new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpRequest request = chain.getRequest();
            // Verify original request
            verifyClientRequest(request);
            // Change request
            ParseHttpRequest requestAgain = generateInterceptorRequest();
            // Proceed
            ParseHttpResponse parseResponse = chain.proceed(requestAgain);
            // Verify original response
            verifyServerResponse(parseResponse);
            // Change response
            return generateInterceptorResponse();
        }
    };
    // Add interceptor
    if (isInternalInterceptorTest) {
        client.addInternalInterceptor(interceptor);
    } else {
        client.addExternalInterceptor(interceptor);
    }
    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(5, TimeUnit.SECONDS));
    RecordedRequest recordedRequest = server.takeRequest();
    // Verify request changed by interceptor
    verifyInterceptorRequest(recordedRequest);
    // Verify response changed by interceptor
    verifyInterceptorResponse(parseResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ParseHttpRequest(com.parse.http.ParseHttpRequest) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) Semaphore(java.util.concurrent.Semaphore) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Example 32 with ParseHttpResponse

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

the class ParseOkHttpClientTest method testGetParseResponse.

@Test
public void testGetParseResponse() throws IOException {
    int statusCode = 200;
    String reasonPhrase = "test reason";
    final String content = "test";
    final int contentLength = content.length();
    final String contentType = "application/json";
    String url = "http://www.parse.com/";
    Request request = new Request.Builder().url(url).build();
    Response okHttpResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(statusCode).message(reasonPhrase).body(new ResponseBody() {

        @Override
        public MediaType contentType() {
            return MediaType.parse(contentType);
        }

        @Override
        public long contentLength() {
            return contentLength;
        }

        @Override
        public BufferedSource source() {
            Buffer buffer = new Buffer();
            buffer.write(content.getBytes());
            return buffer;
        }
    }).build();
    ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
    ParseHttpResponse parseResponse = parseClient.getResponse(okHttpResponse);
    // Verify status code
    assertEquals(statusCode, parseResponse.getStatusCode());
    // Verify reason phrase
    assertEquals(reasonPhrase, parseResponse.getReasonPhrase());
    // Verify content length
    assertEquals(contentLength, parseResponse.getTotalSize());
    // Verify content
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(parseResponse.getContent()));
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpResponse(com.parse.http.ParseHttpResponse) Buffer(okio.Buffer) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ResponseBody(okhttp3.ResponseBody) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 33 with ParseHttpResponse

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

the class ParseRESTCommandTest method testTemporaryFailures.

@Test
public void testTemporaryFailures() throws Exception {
    JSONObject json = new JSONObject();
    json.put("code", 1337);
    json.put("error", "mock error");
    ParseHttpResponse response1 = newMockParseHttpResponse(500, json);
    ParseHttpResponse response2 = newMockParseHttpResponse(500, json);
    ParseHttpResponse response3 = newMockParseHttpResponse(500, json);
    ParseHttpResponse response4 = newMockParseHttpResponse(500, json);
    ParseHttpResponse response5 = newMockParseHttpResponse(500, json);
    ParseHttpClient client = mock(ParseHttpClient.class);
    when(client.execute(any(ParseHttpRequest.class))).thenReturn(response1, response2, response3, response4, response5);
    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(5)).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)

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