Search in sources :

Example 6 with ParseHttpRequest

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

the class ParseOkHttpClientTest method testGetOkHttpRequest.

@Test
public void testGetOkHttpRequest() throws IOException {
    Map<String, String> headers = new HashMap<>();
    String headerName = "name";
    String headerValue = "value";
    headers.put(headerName, headerValue);
    String url = "http://www.parse.com/";
    String content = "test";
    int contentLength = content.length();
    String contentType = "application/json";
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(url).setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(content, contentType)).setHeaders(headers).build();
    ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
    Request okHttpRequest = parseClient.getRequest(parseRequest);
    // Verify method
    assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
    // Verify URL
    assertEquals(url, okHttpRequest.url().toString());
    // Verify Headers
    assertEquals(1, okHttpRequest.headers(headerName).size());
    assertEquals(headerValue, okHttpRequest.headers(headerName).get(0));
    // Verify Body
    RequestBody okHttpBody = okHttpRequest.body();
    assertEquals(contentLength, okHttpBody.contentLength());
    assertEquals(contentType, okHttpBody.contentType().toString());
    // Can not read parseRequest body to compare since it has been read during
    // creating okHttpRequest
    Buffer buffer = new Buffer();
    okHttpBody.writeTo(buffer);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    buffer.copyTo(output);
    assertArrayEquals(content.getBytes(), output.toByteArray());
}
Also used : Buffer(okio.Buffer) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 7 with ParseHttpRequest

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

the class ParseOkHttpClientTest method testGetOkHttpRequestType.

//region testTransferRequest/Response
@Test
public void testGetOkHttpRequestType() throws IOException {
    ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
    ParseHttpRequest.Builder builder = new ParseHttpRequest.Builder();
    builder.setUrl("http://www.parse.com");
    // Get
    ParseHttpRequest parseRequest = builder.setMethod(ParseHttpRequest.Method.GET).setBody(null).build();
    Request okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.GET.toString(), okHttpRequest.method());
    // Post
    parseRequest = builder.setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody("test", "application/json")).build();
    okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
    // Delete
    parseRequest = builder.setMethod(ParseHttpRequest.Method.DELETE).setBody(null).build();
    okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.DELETE.toString(), okHttpRequest.method());
    // Put
    parseRequest = builder.setMethod(ParseHttpRequest.Method.PUT).setBody(new ParseByteArrayHttpBody("test", "application/json")).build();
    okHttpRequest = parseClient.getRequest(parseRequest);
    assertEquals(ParseHttpRequest.Method.PUT.toString(), okHttpRequest.method());
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 8 with ParseHttpRequest

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

the class ParseOkHttpClientTest method generateClientRequest.

// Generate a ParseHttpRequest sent to server
private ParseHttpRequest generateClientRequest() throws Exception {
    Map<String, String> headers = new HashMap<>();
    headers.put("requestkey", "requestValue");
    JSONObject json = new JSONObject();
    json.put("key", "value");
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(server.url("/").toString()).setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(json.toString().getBytes(), "application/json")).setHeaders(headers).build();
    return parseRequest;
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap)

Example 9 with ParseHttpRequest

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

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.

@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
    // Make mock response
    Buffer buffer = new Buffer();
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write("content".getBytes());
    gzipOut.close();
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
    // Start mock server
    server.enqueue(mockResponse);
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    final Semaphore done = new Semaphore(0);
    // Add plain interceptor to disable decompress response stream
    client.addExternalInterceptor(new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
            // Make sure the response we get from the interceptor is the raw gzip stream
            byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
            assertArrayEquals(byteOut.toByteArray(), content);
            // We need to set a new stream since we have read it
            return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
        }
    });
    // 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);
    // Make sure the response we get is ungziped by OkHttp library
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals("content".getBytes(), content);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
    server.shutdown();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 10 with ParseHttpRequest

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

the class ParseOkHttpClientTest method testGetOkHttpRequestWithEmptyContentType.

@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();
    ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
    Request okHttpRequest = parseClient.getRequest(parseRequest);
    // Verify Content-Type
    assertNull(okHttpRequest.body().contentType());
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

ParseHttpRequest (com.parse.http.ParseHttpRequest)19 ParseHttpResponse (com.parse.http.ParseHttpResponse)10 HashMap (java.util.HashMap)9 Test (org.junit.Test)9 ParseNetworkInterceptor (com.parse.http.ParseNetworkInterceptor)6 Request (okhttp3.Request)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 IOException (java.io.IOException)5 ParseHttpBody (com.parse.http.ParseHttpBody)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Headers (okhttp3.Headers)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Buffer (okio.Buffer)3 Semaphore (java.util.concurrent.Semaphore)2 Response (okhttp3.Response)2 MockWebServer (okhttp3.mockwebserver.MockWebServer)2 JSONObject (org.json.JSONObject)2