Search in sources :

Example 16 with ParseHttpRequest

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

the class ParseDecompressInterceptorTest method testDecompressInterceptorWithGZIPResponse.

@Test
public void testDecompressInterceptorWithGZIPResponse() throws Exception {
    ParseDecompressInterceptor interceptor = new ParseDecompressInterceptor();
    final String responseContent = "content";
    ParseHttpResponse interceptedResponse = interceptor.intercept(new ParseNetworkInterceptor.Chain() {

        @Override
        public ParseHttpRequest getRequest() {
            // Generate test request
            return new ParseHttpRequest.Builder().setUrl("www.parse.com").setMethod(ParseHttpRequest.Method.GET).build();
        }

        @Override
        public ParseHttpResponse proceed(ParseHttpRequest request) throws IOException {
            // Make gzip response content
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
            gzipOut.write(responseContent.getBytes());
            gzipOut.close();
            // Make gzip encoding headers
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Encoding", "gzip");
            // Generate test response
            return new ParseHttpResponse.Builder().setStatusCode(200).setTotalSize(byteOut.toByteArray().length).setReasonPhrase("Success").setContentType("text/plain").setContent(new ByteArrayInputStream(byteOut.toByteArray())).setHeaders(headers).build();
        }
    });
    // Verify response is correct
    assertEquals(200, interceptedResponse.getStatusCode());
    assertEquals(-1, interceptedResponse.getTotalSize());
    assertEquals("Success", interceptedResponse.getReasonPhrase());
    assertEquals("text/plain", interceptedResponse.getContentType());
    assertNull(interceptedResponse.getHeader("Content-Encoding"));
    byte[] content = ParseIOUtils.toByteArray(interceptedResponse.getContent());
    assertArrayEquals(responseContent.getBytes(), content);
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 17 with ParseHttpRequest

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

the class ParseHttpRequestTest method testParseHttpRequestGetMethod.

@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()));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpBody(com.parse.http.ParseHttpBody) Test(org.junit.Test)

Example 18 with ParseHttpRequest

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

the class ParseHttpRequestTest method testParseHttpRequestBuilderInitialization.

@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()));
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) ParseHttpBody(com.parse.http.ParseHttpBody) Test(org.junit.Test)

Example 19 with ParseHttpRequest

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

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithInterceptor.

// This test is used to test okHttp interceptors. The difference between external and
// internal interceptor is the external interceptor is added to OkHttpClient level, an internal
// interceptor is added to ParseHttpClient level.
// In the interceptor, we change request and response to see whether our server and
// ParseHttpClient can receive the correct value.
private void testParseOkHttpClientExecuteWithInterceptor(boolean isInternalInterceptorTest) throws Exception {
    // Start mock server
    server.enqueue(generateServerResponse());
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    // Make ParseHttpRequest
    ParseHttpRequest parseRequest = generateClientRequest();
    final Semaphore done = new Semaphore(0);
    ParseNetworkInterceptor interceptor = new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpRequest request = chain.getRequest();
            // Verify original request
            verifyClientRequest(request);
            // Change request
            ParseHttpRequest requestAgain = generateInterceptorRequest();
            // Proceed
            ParseHttpResponse parseResponse = chain.proceed(requestAgain);
            // Verify original response
            verifyServerResponse(parseResponse);
            // Change response
            return generateInterceptorResponse();
        }
    };
    // Add interceptor
    if (isInternalInterceptorTest) {
        client.addInternalInterceptor(interceptor);
    } else {
        client.addExternalInterceptor(interceptor);
    }
    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(5, TimeUnit.SECONDS));
    RecordedRequest recordedRequest = server.takeRequest();
    // Verify request changed by interceptor
    verifyInterceptorRequest(recordedRequest);
    // Verify response changed by interceptor
    verifyInterceptorResponse(parseResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ParseHttpRequest(com.parse.http.ParseHttpRequest) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) Semaphore(java.util.concurrent.Semaphore) ParseHttpResponse(com.parse.http.ParseHttpResponse)

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