Search in sources :

Example 26 with ParseHttpResponse

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"));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 27 with ParseHttpResponse

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());
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONObject(org.json.JSONObject) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 28 with ParseHttpResponse

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"));
}
Also used : ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 29 with ParseHttpResponse

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());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 30 with ParseHttpResponse

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);
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Aggregations

ParseHttpResponse (com.parse.http.ParseHttpResponse)33 ParseHttpRequest (com.parse.http.ParseHttpRequest)26 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)20 JSONObject (org.json.JSONObject)12 ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 File (java.io.File)4 InputStream (java.io.InputStream)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 ResponseBody (okhttp3.ResponseBody)3 Buffer (okio.Buffer)3 Task (bolts.Task)2 ArrayList (java.util.ArrayList)2