Search in sources :

Example 6 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseHttpClientTest method doSingleParseHttpClientExecuteWithGzipResponse.

private void doSingleParseHttpClientExecuteWithGzipResponse(int responseCode, String responseStatus, final String responseContent, ParseHttpClient client) throws Exception {
    MockWebServer server = new MockWebServer();
    // Make mock response
    Buffer buffer = new Buffer();
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write(responseContent.getBytes());
    gzipOut.close();
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + responseCode + " " + responseStatus).setBody(buffer).setHeader("Content-Encoding", "gzip");
    // Start mock server
    server.enqueue(mockResponse);
    server.start();
    // 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);
    RecordedRequest recordedRequest = server.takeRequest();
    // Verify request method
    assertEquals(ParseHttpRequest.Method.GET.toString(), recordedRequest.getMethod());
    // Verify request headers
    Headers recordedHeaders = recordedRequest.getHeaders();
    assertEquals("gzip", recordedHeaders.get("Accept-Encoding"));
    // Verify we do not have Content-Encoding header
    assertNull(parseResponse.getHeader("Content-Encoding"));
    // Verify response body
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals(responseContent.getBytes(), content);
    // Shutdown mock server
    server.shutdown();
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) GZIPOutputStream(java.util.zip.GZIPOutputStream) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse)

Example 7 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseHttpResponseTest method testParseHttpResponseBuildWithParseHttpResponse.

@Test
public void testParseHttpResponseBuildWithParseHttpResponse() 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();
    String newReasonPhrase = "Failed";
    ParseHttpResponse newResponse = new ParseHttpResponse.Builder(response).setReasonPhrase(newReasonPhrase).build();
    assertEquals(contentType, newResponse.getContentType());
    assertEquals(newReasonPhrase, newResponse.getReasonPhrase());
    assertEquals(statusCode, newResponse.getStatusCode());
    assertEquals(totalSize, newResponse.getTotalSize());
    assertEquals(value, newResponse.getHeader(name));
    assertEquals(1, newResponse.getAllHeaders().size());
    assertSame(response.getContent(), newResponse.getContent());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 8 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseCloudCodeControllerTest method testCallFunctionInBackgroundSuccessWithoutResult.

@Test
public void testCallFunctionInBackgroundSuccessWithoutResult() throws Exception {
    JSONObject json = new JSONObject();
    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));
    assertNull(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 9 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseFileControllerTest method testSaveAsyncSuccessWithByteArray.

@Test
public void testSaveAsyncSuccessWithByteArray() 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);
    byte[] data = "hello".getBytes();
    ParseFile.State state = new ParseFile.State.Builder().name("file_name").mimeType("mime_type").build();
    Task<ParseFile.State> task = controller.saveAsync(state, data, 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 file = new File(root, "new_file_name");
    assertTrue(file.exists());
    assertEquals("hello", ParseFileUtils.readFileToString(file, "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 10 with ParseHttpResponse

use of com.parse.http.ParseHttpResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseAWSRequestTest method test4XXThrowsException.

@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void test4XXThrowsException() throws Exception {
    ParseRequest.setDefaultInitialRetryDelay(1L);
    InputStream mockInputStream = new ByteArrayInputStream("An Error occurred while saving".getBytes());
    ParseHttpResponse mockResponse = new ParseHttpResponse.Builder().setStatusCode(400).setTotalSize(0L).setReasonPhrase("Bad Request").setContent(mockInputStream).build();
    ParseHttpClient mockHttpClient = mock(ParseHttpClient.class);
    when(mockHttpClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse);
    ParseAWSRequest request = new ParseAWSRequest(ParseHttpRequest.Method.GET, "http://parse.com", null);
    Task<Void> task = request.executeAsync(mockHttpClient);
    task.waitForCompletion();
    assertTrue(task.isFaulted());
    assertTrue(task.getError() instanceof ParseException);
    ParseException error = (ParseException) task.getError();
    assertEquals(error.getCode(), ParseException.CONNECTION_FAILED);
    assertTrue(error.getMessage().contains("Download from S3"));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse)

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