use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.
the class ParseDecompressInterceptorTest method testDecompressInterceptorWithGZIPResponse.
@Test
public void testDecompressInterceptorWithGZIPResponse() throws Exception {
ParseDecompressInterceptor interceptor = new ParseDecompressInterceptor();
final String responseContent = "content";
ParseHttpResponse interceptedResponse = interceptor.intercept(new ParseNetworkInterceptor.Chain() {
@Override
public ParseHttpRequest getRequest() {
// Generate test request
return new ParseHttpRequest.Builder().setUrl("www.parse.com").setMethod(ParseHttpRequest.Method.GET).build();
}
@Override
public ParseHttpResponse proceed(ParseHttpRequest request) throws IOException {
// Make gzip response content
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
gzipOut.write(responseContent.getBytes());
gzipOut.close();
// Make gzip encoding headers
Map<String, String> headers = new HashMap<>();
headers.put("Content-Encoding", "gzip");
// Generate test response
return new ParseHttpResponse.Builder().setStatusCode(200).setTotalSize(byteOut.toByteArray().length).setReasonPhrase("Success").setContentType("text/plain").setContent(new ByteArrayInputStream(byteOut.toByteArray())).setHeaders(headers).build();
}
});
// Verify response is correct
assertEquals(200, interceptedResponse.getStatusCode());
assertEquals(-1, interceptedResponse.getTotalSize());
assertEquals("Success", interceptedResponse.getReasonPhrase());
assertEquals("text/plain", interceptedResponse.getContentType());
assertNull(interceptedResponse.getHeader("Content-Encoding"));
byte[] content = ParseIOUtils.toByteArray(interceptedResponse.getContent());
assertArrayEquals(responseContent.getBytes(), content);
}
use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.
the class ParseHttpRequestTest method testParseHttpRequestGetMethod.
@Test
public void testParseHttpRequestGetMethod() throws IOException {
String url = "www.parse.com";
ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
Map<String, String> headers = new HashMap<>();
String name = "name";
String value = "value";
headers.put(name, value);
String content = "content";
String contentType = "application/json";
ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);
ParseHttpRequest request = new ParseHttpRequest.Builder().setUrl(url).addHeader(name, value).setMethod(method).setBody(body).build();
assertEquals(url, request.getUrl());
assertEquals(method.toString(), request.getMethod().toString());
assertEquals(1, request.getAllHeaders().size());
assertEquals(value, request.getHeader(name));
ParseHttpBody bodyAgain = request.getBody();
assertEquals(contentType, bodyAgain.getContentType());
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
use of com.parse.http.ParseHttpRequest in project Parse-SDK-Android by ParsePlatform.
the class ParseHttpRequestTest method testParseHttpRequestBuilderInitialization.
@Test
public void testParseHttpRequestBuilderInitialization() throws IOException {
String url = "www.parse.com";
ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
Map<String, String> headers = new HashMap<>();
String name = "name";
String value = "value";
headers.put(name, value);
String content = "content";
String contentType = "application/json";
ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);
ParseHttpRequest request = new ParseHttpRequest.Builder().setUrl(url).addHeader(name, value).setMethod(method).setBody(body).build();
ParseHttpRequest requestAgain = new ParseHttpRequest.Builder(request).build();
assertEquals(url, requestAgain.getUrl());
assertEquals(method.toString(), requestAgain.getMethod().toString());
assertEquals(1, requestAgain.getAllHeaders().size());
assertEquals(value, requestAgain.getHeader(name));
ParseHttpBody bodyAgain = requestAgain.getBody();
assertEquals(contentType, bodyAgain.getContentType());
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
use of com.parse.http.ParseHttpRequest 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