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