Search in sources :

Example 1 with ParseHttpRequest

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

the class ParseOkHttpClient method executeInternal.

@Override
/* package */
ParseHttpResponse executeInternal(ParseHttpRequest parseRequest) throws IOException {
    Request okHttpRequest = getRequest(parseRequest);
    Call okHttpCall = okHttpClient.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();
    return getResponse(okHttpResponse);
}
Also used : Response(okhttp3.Response) ParseHttpResponse(com.parse.http.ParseHttpResponse) Call(okhttp3.Call) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest)

Example 2 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 3 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 4 with ParseHttpRequest

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

the class ParseHttpRequestTest method testParseHttpRequestBuildWithParseHttpRequest.

@Test
public void testParseHttpRequestBuildWithParseHttpRequest() 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();
    String newURL = "www.api.parse.com";
    ParseHttpRequest newRequest = new ParseHttpRequest.Builder(request).setUrl(newURL).build();
    assertEquals(newURL, newRequest.getUrl());
    assertEquals(method.toString(), newRequest.getMethod().toString());
    assertEquals(1, newRequest.getAllHeaders().size());
    assertEquals(value, newRequest.getHeader(name));
    ParseHttpBody bodyAgain = newRequest.getBody();
    assertEquals(contentType, bodyAgain.getContentType());
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpBody(com.parse.http.ParseHttpBody) Test(org.junit.Test)

Example 5 with ParseHttpRequest

use of com.parse.http.ParseHttpRequest 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)

Aggregations

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