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