@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()); }
@Test public void testNewBody() throws Exception { // Make test command String httpPath = "www.parse.com"; JSONObject jsonParameters = new JSONObject() .put("count", 1) .put("limit", 1); String sessionToken = "sessionToken"; String localId = "localId"; ParseRESTCommand command = new ParseRESTCommand.Builder() .httpPath(httpPath) .jsonParameters(jsonParameters) .method(ParseHttpRequest.Method.GET) .sessionToken(sessionToken) .localId(localId) .build(); ParseByteArrayHttpBody body = (ParseByteArrayHttpBody) command.newBody(null); // Verify body content is correct JSONObject json = new JSONObject(new String(ParseIOUtils.toByteArray(body.getContent()))); assertEquals(1, json.getInt("count")); assertEquals(1, json.getInt("limit")); assertEquals(ParseHttpRequest.Method.GET.toString(), json.getString("_method")); // Verify body content-type is correct assertEquals("application/json", body.getContentType()); }
@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 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 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())); }
ParseHttpBody bodyAgain = newRequest.getBody(); assertEquals(contentType, bodyAgain.getContentType()); assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));