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);
}
Aggregations