Search in sources :

Example 6 with ParseNetworkInterceptor

use of com.parse.http.ParseNetworkInterceptor 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)

Aggregations

ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)6 ParseHttpRequest (com.parse.http.ParseHttpRequest)3 ParseHttpResponse (com.parse.http.ParseHttpResponse)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 Capture (bolts.Capture)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Buffer (okio.Buffer)1