use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseConfigControllerTest method mockParseHttpClientWithResponse.
//TODO(mengyan) Create unit test helper and move all similar methods to the class
private ParseHttpClient mockParseHttpClientWithResponse(JSONObject content, int statusCode, String reasonPhrase) throws IOException {
byte[] contentBytes = content.toString().getBytes();
ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(contentBytes)).setStatusCode(statusCode).setTotalSize(contentBytes.length).setContentType("application/json").build();
ParseHttpClient client = mock(ParseHttpClient.class);
when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
return client;
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseRESTCommandTest method testPermanentFailures.
@Test
public void testPermanentFailures() throws Exception {
JSONObject json = new JSONObject();
json.put("code", 1337);
json.put("error", "mock error");
ParseHttpResponse response = newMockParseHttpResponse(400, json);
ParseHttpClient client = mock(ParseHttpClient.class);
when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
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(1)).execute(any(ParseHttpRequest.class));
assertTrue(task.isFaulted());
assertEquals(1337, ((ParseException) task.getError()).getCode());
assertEquals("mock error", task.getError().getMessage());
}
use of com.parse.http.ParseHttpResponse 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.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParsePushControllerTest method mockParseHttpClientWithResponse.
//endregion
private ParseHttpClient mockParseHttpClientWithResponse(JSONObject content, int statusCode, String reasonPhrase) throws IOException {
byte[] contentBytes = content.toString().getBytes();
ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(contentBytes)).setStatusCode(statusCode).setTotalSize(contentBytes.length).setContentType("application/json").build();
ParseHttpClient client = mock(ParseHttpClient.class);
when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
return client;
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseTestUtils method mockParseHttpClientWithResponse.
public static ParseHttpClient mockParseHttpClientWithResponse(JSONObject content, int statusCode, String reasonPhrase) throws IOException {
byte[] contentBytes = content.toString().getBytes();
ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(contentBytes)).setStatusCode(statusCode).setTotalSize(contentBytes.length).setContentType("application/json").build();
ParseHttpClient client = mock(ParseHttpClient.class);
when(client.execute(any(ParseHttpRequest.class))).thenReturn(response);
return client;
}
Aggregations