@Override protected ParseHttpBody newBody(ProgressCallback uploadProgressCallback) { if (jsonParameters == null) { String message = String.format("Trying to execute a %s command without body parameters.", method.toString()); throw new IllegalArgumentException(message); } try { JSONObject parameters = jsonParameters; if (method == ParseHttpRequest.Method.GET || method == ParseHttpRequest.Method.DELETE) { // The request URI may be too long to include parameters in the URI. // To avoid this problem we send the parameters in a POST request json-encoded body // and add a http method override parameter. parameters = new JSONObject(jsonParameters.toString()); parameters.put(PARAMETER_METHOD_OVERRIDE, method.toString()); } return new ParseByteArrayHttpBody(parameters.toString(), "application/json"); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
@Override protected ParseHttpBody newBody(final ProgressCallback progressCallback) { // TODO(mengyan): Delete ParseByteArrayHttpBody when we change input byte array to staged file // in ParseFileController if (progressCallback == null) { return data != null ? new ParseByteArrayHttpBody(data, contentType) : new ParseFileHttpBody(file, contentType); } return data != null ? new ParseCountingByteArrayHttpBody(data, contentType, progressCallback) : new ParseCountingFileHttpBody(file, contentType, progressCallback); }
@Test public void testWriteTo() throws IOException { String content = "content"; String contentType = "application/json"; ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType); // Check content ByteArrayOutputStream output = new ByteArrayOutputStream(); body.writeTo(output); String contentAgain = output.toString(); assertEquals(content, contentAgain); // No need to check whether content input stream is closed since it is a ByteArrayInputStream } }
@Test public void testInitializeWithString() throws IOException { String content = "content"; String contentType = "application/json"; ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType); assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent())); assertEquals(contentType, body.getContentType()); assertEquals(7, body.getContentLength()); }
.setUrl(url) .setMethod(ParseHttpRequest.Method.POST) .setBody(new ParseByteArrayHttpBody(content, contentType)) .setHeaders(headers) .build();
@Test public void testInitializeWithByteArray() throws IOException { byte[] content = {1, 1, 1, 1, 1}; String contentType = "application/json"; ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType); assertArrayEquals(content, ParseIOUtils.toByteArray(body.getContent())); assertEquals(contentType, body.getContentType()); assertEquals(5, body.getContentLength()); }
@Test public void testGetOkHttpRequestWithEmptyContentType() throws Exception { String url = "http://www.parse.com/"; String content = "test"; ParseHttpRequest parseRequest = new ParseHttpRequest.Builder() .setUrl(url) .setMethod(ParseHttpRequest.Method.POST) .setBody(new ParseByteArrayHttpBody(content, null)) .build(); ParseHttpClient parseClient = ParseHttpClient.createClient(new OkHttpClient.Builder()); Request okHttpRequest = parseClient.getRequest(parseRequest); // Verify Content-Type assertNull(okHttpRequest.body().contentType()); }
.setUrl(requestUrl) .setMethod(ParseHttpRequest.Method.POST) .setBody(new ParseByteArrayHttpBody(requestContent, requestContentType)) .setHeaders(requestHeaders) .build();
.setBody(new ParseByteArrayHttpBody("test", "application/json")) .build(); okHttpRequest = parseClient.getRequest(parseRequest); .setBody(new ParseByteArrayHttpBody("test", "application/json")) .build(); okHttpRequest = parseClient.getRequest(parseRequest);
@Test public void testParseHttpRequestGetMethod() throws IOException { String url = "www.parse.com"; ParseHttpRequest.Method method = ParseHttpRequest.Method.POST; Map<String, String> headers = new HashMap<>(); String name = "name"; String value = "value"; headers.put(name, value); String content = "content"; String contentType = "application/json"; ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType); ParseHttpRequest request = new ParseHttpRequest.Builder() .setUrl(url) .addHeader(name, value) .setMethod(method) .setBody(body) .build(); assertEquals(url, request.getUrl()); assertEquals(method.toString(), request.getMethod().toString()); assertEquals(1, request.getAllHeaders().size()); assertEquals(value, request.getHeader(name)); ParseHttpBody bodyAgain = request.getBody(); assertEquals(contentType, bodyAgain.getContentType()); assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent())); }
@Test public void testParseHttpRequestBuilderInitialization() throws IOException { String url = "www.parse.com"; ParseHttpRequest.Method method = ParseHttpRequest.Method.POST; Map<String, String> headers = new HashMap<>(); String name = "name"; String value = "value"; headers.put(name, value); String content = "content"; String contentType = "application/json"; ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType); ParseHttpRequest request = new ParseHttpRequest.Builder() .setUrl(url) .addHeader(name, value) .setMethod(method) .setBody(body) .build(); ParseHttpRequest requestAgain = new ParseHttpRequest.Builder(request).build(); assertEquals(url, requestAgain.getUrl()); assertEquals(method.toString(), requestAgain.getMethod().toString()); assertEquals(1, requestAgain.getAllHeaders().size()); assertEquals(value, requestAgain.getHeader(name)); ParseHttpBody bodyAgain = requestAgain.getBody(); assertEquals(contentType, bodyAgain.getContentType()); assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent())); }
ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);