use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseFileControllerTest method testSaveAsyncSuccessWithFile.
@Test
public void testSaveAsyncSuccessWithFile() throws Exception {
JSONObject json = new JSONObject();
json.put("name", "new_file_name");
json.put("url", "http://example.com");
String content = json.toString();
ParseHttpResponse mockResponse = new ParseHttpResponse.Builder().setStatusCode(200).setTotalSize((long) content.length()).setContent(new ByteArrayInputStream(content.getBytes())).build();
ParseHttpClient restClient = mock(ParseHttpClient.class);
when(restClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse);
File root = temporaryFolder.getRoot();
ParseFileController controller = new ParseFileController(restClient, root);
File file = new File(root, "test");
ParseFileUtils.writeStringToFile(file, "content", "UTF-8");
ParseFile.State state = new ParseFile.State.Builder().name("file_name").mimeType("mime_type").build();
Task<ParseFile.State> task = controller.saveAsync(state, file, null, null, null);
ParseFile.State result = ParseTaskUtils.wait(task);
verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
assertEquals("new_file_name", result.name());
assertEquals("http://example.com", result.url());
File cachedFile = new File(root, "new_file_name");
assertTrue(cachedFile.exists());
assertTrue(file.exists());
assertEquals("content", ParseFileUtils.readFileToString(cachedFile, "UTF-8"));
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseCloudCodeControllerTest method testCallFunctionInBackgroundSuccessWithResult.
@Test
public void testCallFunctionInBackgroundSuccessWithResult() throws Exception {
JSONObject json = new JSONObject();
json.put("result", "test");
String content = json.toString();
ParseHttpResponse mockResponse = new ParseHttpResponse.Builder().setStatusCode(200).setTotalSize((long) content.length()).setContent(new ByteArrayInputStream(content.getBytes())).build();
ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse);
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
Task<String> cloudCodeTask = controller.callFunctionInBackground("test", new HashMap<String, Object>(), "sessionToken");
ParseTaskUtils.wait(cloudCodeTask);
verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
assertEquals("test", cloudCodeTask.getResult());
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseHttpResponseTest method testParseHttpResponseDefaults.
@Test
public void testParseHttpResponseDefaults() throws IOException {
ParseHttpResponse response = new ParseHttpResponse.Builder().build();
assertNull(response.getContent());
assertNull(response.getContentType());
assertNull(response.getReasonPhrase());
assertEquals(0, response.getStatusCode());
assertEquals(-1, response.getTotalSize());
assertEquals(0, response.getAllHeaders().size());
assertNull(response.getHeader("test"));
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseHttpResponseTest method testParseHttpResponseGetMethod.
@Test
public void testParseHttpResponseGetMethod() throws IOException {
Map<String, String> headers = new HashMap<>();
String name = "name";
String value = "value";
headers.put(name, value);
String content = "content";
String contentType = "application/json";
String reasonPhrase = "OK";
int statusCode = 200;
int totalSize = content.length();
ParseHttpResponse response = new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(content.getBytes())).setContentType(contentType).setHeaders(headers).setReasonPhrase(reasonPhrase).setStatusCode(statusCode).setTotalSize(totalSize).build();
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(response.getContent()));
assertEquals(contentType, response.getContentType());
assertEquals(reasonPhrase, response.getReasonPhrase());
assertEquals(statusCode, response.getStatusCode());
assertEquals(totalSize, response.getTotalSize());
assertEquals(value, response.getHeader(name));
assertEquals(1, response.getAllHeaders().size());
}
use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseRequestTest method testDownloadProgress.
// TODO(grantland): Move to ParseAWSRequestTest or ParseCountingByteArrayHttpBodyTest
@Test
public void testDownloadProgress() throws Exception {
ParseHttpResponse mockResponse = new ParseHttpResponse.Builder().setStatusCode(200).setTotalSize((long) data.length).setContent(new ByteArrayInputStream(data)).build();
ParseHttpClient mockHttpClient = mock(ParseHttpClient.class);
when(mockHttpClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse);
File tempFile = temporaryFolder.newFile("test");
ParseAWSRequest request = new ParseAWSRequest(ParseHttpRequest.Method.GET, "localhost", tempFile);
TestProgressCallback downloadProgressCallback = new TestProgressCallback();
Task<Void> task = request.executeAsync(mockHttpClient, null, downloadProgressCallback);
task.waitForCompletion();
assertFalse("Download failed: " + task.getError(), task.isFaulted());
assertEquals(data.length, ParseFileUtils.readFileToByteArray(tempFile).length);
assertProgressCompletedSuccessfully(downloadProgressCallback);
}
Aggregations