use of com.parse.http.ParseNetworkInterceptor in project Parse-SDK-Android by ParsePlatform.
the class ParseClientConfigurationTest method testSetNetworkInterceptors.
@Test
public void testSetNetworkInterceptors() {
final ParseNetworkInterceptor interceptorA = mock(ParseNetworkInterceptor.class);
final ParseNetworkInterceptor interceptorB = mock(ParseNetworkInterceptor.class);
Collection<ParseNetworkInterceptor> collectionA = new ArrayList<ParseNetworkInterceptor>() {
{
add(interceptorA);
add(interceptorB);
}
};
Collection<ParseNetworkInterceptor> collectionB = new ArrayList<ParseNetworkInterceptor>() {
{
add(interceptorB);
add(interceptorA);
}
};
Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
builder.setNetworkInterceptors(collectionA);
Parse.Configuration configurationA = builder.build();
builder.setNetworkInterceptors(collectionB);
Parse.Configuration configurationB = builder.build();
assertTrue(collectionsEqual(configurationA.interceptors, collectionA));
assertTrue(collectionsEqual(configurationB.interceptors, collectionB));
}
use of com.parse.http.ParseNetworkInterceptor 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();
}
use of com.parse.http.ParseNetworkInterceptor in project Parse-SDK-Android by ParsePlatform.
the class Parse method initializeParseHttpClientsWithParseNetworkInterceptors.
// Initialize all necessary http clients and add interceptors to these http clients
private static void initializeParseHttpClientsWithParseNetworkInterceptors(List<ParseNetworkInterceptor> interceptors) {
// This means developers have not called addInterceptor method so we should do nothing.
if (interceptors == null) {
return;
}
List<ParseHttpClient> clients = new ArrayList<>();
// Rest http client
clients.add(ParsePlugins.get().restClient());
// AWS http client
clients.add(ParseCorePlugins.getInstance().getFileController().awsClient());
// Add interceptors to http clients
for (ParseHttpClient parseHttpClient : clients) {
// We need to add the decompress interceptor before the external interceptors to return
// a decompressed response to Parse.
parseHttpClient.addInternalInterceptor(new ParseDecompressInterceptor());
for (ParseNetworkInterceptor interceptor : interceptors) {
parseHttpClient.addExternalInterceptor(interceptor);
}
}
}
use of com.parse.http.ParseNetworkInterceptor in project Parse-SDK-Android by ParsePlatform.
the class ParseOkHttpClient method addExternalInterceptor.
/**
* For OKHttpClient, since it does not expose any interface for us to check the raw response
* stream, we have to use OKHttp networkInterceptors. Instead of using our own interceptor list,
* we use OKHttp inner interceptor list.
* @param parseNetworkInterceptor
*/
@Override
/* package */
void addExternalInterceptor(final ParseNetworkInterceptor parseNetworkInterceptor) {
OkHttpClient.Builder builder = okHttpClient.newBuilder();
builder.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(final Chain okHttpChain) throws IOException {
Request okHttpRequest = okHttpChain.request();
// Transfer OkHttpRequest to ParseHttpRequest
final ParseHttpRequest parseRequest = getParseHttpRequest(okHttpRequest);
// Capture OkHttpResponse
final Capture<Response> okHttpResponseCapture = new Capture<>();
final ParseHttpResponse parseResponse = parseNetworkInterceptor.intercept(new ParseNetworkInterceptor.Chain() {
@Override
public ParseHttpRequest getRequest() {
return parseRequest;
}
@Override
public ParseHttpResponse proceed(ParseHttpRequest parseRequest) throws IOException {
// Use OKHttpClient to send request
Request okHttpRequest = ParseOkHttpClient.this.getRequest(parseRequest);
Response okHttpResponse = okHttpChain.proceed(okHttpRequest);
okHttpResponseCapture.set(okHttpResponse);
return getResponse(okHttpResponse);
}
});
final Response okHttpResponse = okHttpResponseCapture.get();
// Ideally we should build newOkHttpResponse only based on parseResponse, however
// ParseHttpResponse does not have all the info we need to build the newOkHttpResponse, so
// we rely on the okHttpResponse to generate the builder and change the necessary info
// inside
Response.Builder newOkHttpResponseBuilder = okHttpResponse.newBuilder();
// Set status
newOkHttpResponseBuilder.code(parseResponse.getStatusCode()).message(parseResponse.getReasonPhrase());
// Set headers
if (parseResponse.getAllHeaders() != null) {
for (Map.Entry<String, String> entry : parseResponse.getAllHeaders().entrySet()) {
newOkHttpResponseBuilder.header(entry.getKey(), entry.getValue());
}
}
// Set body
newOkHttpResponseBuilder.body(new ResponseBody() {
@Override
public MediaType contentType() {
if (parseResponse.getContentType() == null) {
return null;
}
return MediaType.parse(parseResponse.getContentType());
}
@Override
public long contentLength() {
return parseResponse.getTotalSize();
}
@Override
public BufferedSource source() {
// interceptor.
if (parseResponse.getContent() == null) {
return null;
}
return Okio.buffer(Okio.source(parseResponse.getContent()));
}
});
return newOkHttpResponseBuilder.build();
}
});
okHttpClient = builder.build();
}
use of com.parse.http.ParseNetworkInterceptor in project Parse-SDK-Android by ParsePlatform.
the class ParseClientConfigurationTest method testNetworkInterceptors.
@Test
public void testNetworkInterceptors() {
ParseNetworkInterceptor interceptorA = mock(ParseNetworkInterceptor.class);
ParseNetworkInterceptor interceptorB = mock(ParseNetworkInterceptor.class);
Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
builder.addNetworkInterceptor(interceptorA);
Parse.Configuration configurationA = builder.build();
builder.addNetworkInterceptor(interceptorB);
Parse.Configuration configurationB = builder.build();
assertFalse(configurationA.interceptors.contains(interceptorB));
assertTrue(configurationB.interceptors.contains(interceptorB));
try {
configurationA.interceptors.add(interceptorB);
fail("Interceptors shouldn't be mutable.");
} catch (UnsupportedOperationException ex) {
// Expected
}
}
Aggregations