Example usage for com.squareup.okhttp Protocol HTTP_2

List of usage examples for com.squareup.okhttp Protocol HTTP_2

Introduction

In this page you can find the example usage for com.squareup.okhttp Protocol HTTP_2.

Prototype

Protocol HTTP_2

To view the source code for com.squareup.okhttp Protocol HTTP_2.

Click Source Link

Document

The IETF's binary-framed protocol that includes header compression, multiplexing multiple requests on the same socket, and server-push.

Usage

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testUpdateCollapseWhenUpdateIsInQueueOnAuthentication() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();
    final String expectedUpdateContent = "{\"String\":\"fooo\",\"id\":\"abc\"}";

    thrownExceptionFlag.Thrown = true;//w w w . ja  va2s . c  o  m

    MobileServiceClient client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus(new StatusLine(Protocol.HTTP_2, 401, ""));

    final MobileServiceException innerException = new MobileServiceException("", response);

    Function<ServiceFilterRequest, Void> onHandleRequest = new Function<ServiceFilterRequest, Void>() {
        public Void apply(ServiceFilterRequest request) {
            try {
                if (thrownExceptionFlag.Thrown) {
                    throw innerException;
                }
            } catch (Exception e) {
                serviceFilterContainer.Exception = e;
            }

            return null;
        }
    };

    client = client.withFilter(
            getTestFilter(serviceFilterContainer, onHandleRequest, "{\"id\":\"abc\",\"String\":\"Hey\"}"));

    client.getSyncContext().initialize(store, new SimpleSyncHandler()).get();

    MobileServiceSyncTable<StringIdType> table = client.getSyncTable(StringIdType.class);

    StringIdType item = new StringIdType();

    item.Id = "abc";
    item.String = "what?";

    table.update(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

    try {
        client.getSyncContext().push().get();
    } catch (Exception ex) {

        StringIdType itemUpdated = new StringIdType();

        itemUpdated.Id = "abc";
        itemUpdated.String = "fooo";

        table.update(itemUpdated).get();

        thrownExceptionFlag.Thrown = false;

        client.getSyncContext().push().get();

        assertEquals(client.getSyncContext().getPendingOperations(), 0);
        assertEquals(serviceFilterContainer.Requests.get(0).Method, HttpConstants.PatchMethod);
        assertEquals(serviceFilterContainer.Requests.get(1).Method, HttpConstants.PatchMethod);
        assertEquals(serviceFilterContainer.Requests.get(1).Content, expectedUpdateContent);

        return;
    }

    assertTrue(false);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testDeleteCollapseUpdateWhenUpdateIsInQueueOnAuthenticationError() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();

    thrownExceptionFlag.Thrown = true;/*from  w  w w .j a  v  a 2s  .co m*/

    MobileServiceClient client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus((new StatusLine(Protocol.HTTP_2, 401, "")));

    final MobileServiceException innerException = new MobileServiceException("", response);

    Function<ServiceFilterRequest, Void> onHandleRequest = new Function<ServiceFilterRequest, Void>() {
        public Void apply(ServiceFilterRequest request) {
            try {
                if (thrownExceptionFlag.Thrown) {
                    throw innerException;
                }
            } catch (Exception e) {
                serviceFilterContainer.Exception = e;
            }

            return null;
        }
    };

    client = client.withFilter(
            getTestFilter(serviceFilterContainer, onHandleRequest, "{\"id\":\"abc\",\"String\":\"Hey\"}"));

    client.getSyncContext().initialize(store, new SimpleSyncHandler()).get();

    MobileServiceSyncTable<StringIdType> table = client.getSyncTable(StringIdType.class);

    StringIdType item = new StringIdType();

    item.Id = "abc";
    item.String = "what?";

    table.update(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

    try {
        client.getSyncContext().push().get();
    } catch (Exception ex) {

        table.delete(item).get();

        thrownExceptionFlag.Thrown = false;

        client.getSyncContext().push().get();

        assertEquals(client.getSyncContext().getPendingOperations(), 0);
        assertEquals(serviceFilterContainer.Requests.get(0).Method, HttpConstants.PatchMethod);
        assertEquals(serviceFilterContainer.Requests.get(1).Method, HttpConstants.DeleteMethod);

        return;
    }

    assertTrue(false);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testInsertThrowsWhenUpdateIsInQueueOnAuthenticationError() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();

    thrownExceptionFlag.Thrown = true;//from   w w w  . j  a  va2  s .  c  o  m

    MobileServiceClient client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus(new StatusLine(Protocol.HTTP_2, 401, ""));

    final MobileServiceException innerException = new MobileServiceException("", response);

    Function<ServiceFilterRequest, Void> onHandleRequest = new Function<ServiceFilterRequest, Void>() {
        public Void apply(ServiceFilterRequest request) {
            try {
                if (thrownExceptionFlag.Thrown) {
                    throw innerException;
                }
            } catch (Exception e) {
                serviceFilterContainer.Exception = e;
            }

            return null;
        }
    };

    client = client.withFilter(
            getTestFilter(serviceFilterContainer, onHandleRequest, "{\"id\":\"abc\",\"String\":\"Hey\"}"));

    client.getSyncContext().initialize(store, new SimpleSyncHandler()).get();

    MobileServiceSyncTable<StringIdType> table = client.getSyncTable(StringIdType.class);

    StringIdType item = new StringIdType();

    item.Id = "abc";
    item.String = "what?";

    table.update(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

    try {
        client.getSyncContext().push().get();
    } catch (Exception ex) {
        try {
            table.insert(item).get();
        } catch (Exception ex2) {
            assertEquals(client.getSyncContext().getPendingOperations(), 1);
            assertEquals(serviceFilterContainer.Requests.get(0).Method, HttpConstants.PatchMethod);
            assertEquals(ex2.getMessage(),
                    "java.lang.IllegalStateException: An update operation on the item is already in the queue.");

            return;
        }
    }

    assertTrue(false);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testDeleteThrowsWhenDeleteIsInQueueOnAuthenticationError() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();

    thrownExceptionFlag.Thrown = true;/*from www  .  ja v a2s .  co m*/

    MobileServiceClient client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus(new StatusLine(Protocol.HTTP_2, 401, ""));

    final MobileServiceException innerException = new MobileServiceException("", response);

    Function<ServiceFilterRequest, Void> onHandleRequest = new Function<ServiceFilterRequest, Void>() {
        public Void apply(ServiceFilterRequest request) {
            try {
                if (thrownExceptionFlag.Thrown) {
                    throw innerException;
                }
            } catch (Exception e) {
                serviceFilterContainer.Exception = e;
            }

            return null;
        }
    };

    client = client.withFilter(
            getTestFilter(serviceFilterContainer, onHandleRequest, "{\"id\":\"abc\",\"String\":\"Hey\"}"));

    client.getSyncContext().initialize(store, new SimpleSyncHandler()).get();

    MobileServiceSyncTable<StringIdType> table = client.getSyncTable(StringIdType.class);

    StringIdType item = new StringIdType();

    item.Id = "abc";
    item.String = "what?";

    table.delete(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

    try {
        client.getSyncContext().push().get();
    } catch (Exception ex) {
        try {
            table.delete(item).get();
        } catch (Exception ex2) {
            assertEquals(client.getSyncContext().getPendingOperations(), 1);
            assertEquals(serviceFilterContainer.Requests.get(0).Method, HttpConstants.DeleteMethod);
            assertEquals(ex2.getMessage(),
                    "java.lang.IllegalStateException: A delete operation on the item is already in the queue.");

            return;
        }
    }

    assertTrue(false);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testInsertThrowsWhenDeleteIsInQueueOnAuthenticationError() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();

    thrownExceptionFlag.Thrown = true;//from w w w  . j a va 2s .  c o m

    MobileServiceClient client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus((new StatusLine(Protocol.HTTP_2, 401, "")));

    final MobileServiceException innerException = new MobileServiceException("", response);

    Function<ServiceFilterRequest, Void> onHandleRequest = new Function<ServiceFilterRequest, Void>() {
        public Void apply(ServiceFilterRequest request) {
            try {
                if (thrownExceptionFlag.Thrown) {
                    throw innerException;
                }
            } catch (Exception e) {
                serviceFilterContainer.Exception = e;
            }

            return null;
        }
    };

    client = client.withFilter(
            getTestFilter(serviceFilterContainer, onHandleRequest, "{\"id\":\"abc\",\"String\":\"Hey\"}"));

    client.getSyncContext().initialize(store, new SimpleSyncHandler()).get();

    MobileServiceSyncTable<StringIdType> table = client.getSyncTable(StringIdType.class);

    StringIdType item = new StringIdType();

    item.Id = "abc";
    item.String = "what?";

    table.delete(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

    try {
        client.getSyncContext().push().get();
    } catch (Exception ex) {
        try {
            table.insert(item).get();
        } catch (Exception ex2) {
            assertEquals(client.getSyncContext().getPendingOperations(), 1);
            assertEquals(serviceFilterContainer.Requests.get(0).Method, HttpConstants.DeleteMethod);
            assertEquals(ex2.getMessage(),
                    "java.lang.IllegalStateException: A delete operation on the item is already in the queue.");

            return;
        }
    }

    assertTrue(false);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testUpdateThrowsWhenDeleteIsInQueueOnAuthenticationError() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();

    thrownExceptionFlag.Thrown = true;//from w w w  . j  av  a 2  s.com

    MobileServiceClient client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus((new StatusLine(Protocol.HTTP_2, 401, "")));

    final MobileServiceException innerException = new MobileServiceException("", response);

    Function<ServiceFilterRequest, Void> onHandleRequest = new Function<ServiceFilterRequest, Void>() {
        public Void apply(ServiceFilterRequest request) {
            try {
                if (thrownExceptionFlag.Thrown) {
                    throw innerException;
                }
            } catch (Exception e) {
                serviceFilterContainer.Exception = e;
            }

            return null;
        }
    };

    client = client.withFilter(
            getTestFilter(serviceFilterContainer, onHandleRequest, "{\"id\":\"abc\",\"String\":\"Hey\"}"));

    client.getSyncContext().initialize(store, new SimpleSyncHandler()).get();

    MobileServiceSyncTable<StringIdType> table = client.getSyncTable(StringIdType.class);

    StringIdType item = new StringIdType();

    item.Id = "abc";
    item.String = "what?";

    table.delete(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

    try {
        client.getSyncContext().push().get();
    } catch (Exception ex) {
        try {
            table.update(item).get();
        } catch (Exception ex2) {
            assertEquals(client.getSyncContext().getPendingOperations(), 1);
            assertEquals(serviceFilterContainer.Requests.get(0).Method, HttpConstants.DeleteMethod);
            assertEquals(ex2.getMessage(),
                    "java.lang.IllegalStateException: A delete operation on the item is already in the queue.");

            return;
        }
    }

    assertTrue(false);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

public void testPushIsAbortedOnAuthenticationError() throws Throwable {
    // Create a mock response simulating an error
    ServiceFilterResponseMock response = new ServiceFilterResponseMock();
    response.setStatus((new StatusLine(Protocol.HTTP_2, 401, "")));

    MobileServiceException authError = new MobileServiceException("", response);
    TestPushAbort(authError, MobileServicePushStatus.CancelledByAuthenticationError);
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceSyncTableTests.java

License:Open Source License

private ServiceFilter getTestFilter(final ServiceFilterContainer serviceFilterContainer, final int statusCode,
        final Function<ServiceFilterRequest, Void> onHandleRequest, final boolean getLastContentAsDefault,
        final String... contents) {

    return new ServiceFilter() {

        @Override/*  www  . ja  v a  2  s . c o  m*/
        public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request,
                NextServiceFilterCallback nextServiceFilterCallback) {

            // Create a mock response simulating an error
            ServiceFilterResponseMock response = new ServiceFilterResponseMock();
            response.setStatus((new StatusLine(Protocol.HTTP_2, statusCode, "")));

            String content = "";

            if (contents.length > serviceFilterContainer.Requests.size()) {
                content = contents[serviceFilterContainer.Requests.size()];
            } else {
                if (getLastContentAsDefault) {
                    content = contents[contents.length - 1];
                } else {
                    content = "[]";
                }
            }

            response.setContent(content);

            ServiceFilterRequestData serviceFilterRequestData = new ServiceFilterRequestData();
            serviceFilterRequestData.Headers = request.getHeaders();
            serviceFilterRequestData.Content = request.getContent();
            serviceFilterRequestData.Url = request.getUrl();
            serviceFilterRequestData.Method = request.getMethod();

            serviceFilterContainer.Url = request.getUrl();
            serviceFilterContainer.Requests.add(serviceFilterRequestData);

            if (onHandleRequest != null) {
                onHandleRequest.apply(request);
            }

            // create a mock request to replace the existing one
            ServiceFilterRequestMock requestMock = new ServiceFilterRequestMock(response);

            if (serviceFilterContainer.Exception != null) {
                requestMock.setExceptionToThrow(serviceFilterContainer.Exception);
                requestMock.setHasErrorOnExecute(true);

                // used exception
                serviceFilterContainer.Exception = null;
            }

            return nextServiceFilterCallback.onNext(requestMock);
        }

    };
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceTableTests.java

License:Open Source License

public void testOperationHandleServerErrorProperly() throws Throwable {

    String tableName = "MyTableName";
    MobileServiceClient client = null;/*w  w w  .  j av a 2 s. c  om*/

    try {
        client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    // Add a filter to handle the request and create a new JSon
    // object with an id defined
    client = client.withFilter(new ServiceFilter() {

        @Override
        public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request,
                NextServiceFilterCallback nextServiceFilterCallback) {

            ServiceFilterResponseMock response = new ServiceFilterResponseMock();
            response.setStatus(new StatusLine(Protocol.HTTP_2, 500, ""));

            response.setContent("{'error': 'Internal server error'}");
            // call onResponse with the mocked response

            final SettableFuture<ServiceFilterResponse> resultFuture = SettableFuture.create();

            resultFuture.setException(new MobileServiceException("ERROR"));

            return resultFuture;

        }
    });

    // Create get the MobileService table
    MobileServiceJsonTable msTable = client.getTable(tableName);

    JsonObject json = new JsonParser().parse("{'myField': 'invalid value'}").getAsJsonObject();

    // Call the insert method
    try {
        msTable.insert(json).get();
        Assert.fail();
    } catch (Exception exception) {

        Exception testException = null;

        if (exception instanceof ExecutionException) {
            testException = (Exception) exception.getCause();
        } else {
            testException = exception;
        }

        assertTrue(testException instanceof MobileServiceException);
    }
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.test.MobileServiceTableTests.java

License:Open Source License

public void testOperationWithErrorAndNoContentShowStatusCode() throws Throwable {

    String tableName = "MyTableName";
    MobileServiceClient client = null;/*from   w  ww.j  av  a  2  s  .c  om*/

    try {
        client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    // Add a filter to handle the request and create a new JSon
    // object with an id defined
    client = client.withFilter(new ServiceFilter() {

        @Override
        public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request,
                NextServiceFilterCallback nextServiceFilterCallback) {

            ServiceFilterResponseMock response = new ServiceFilterResponseMock();
            response.setStatus(new StatusLine(Protocol.HTTP_2, 500, ""));

            response.setContent((String) null);
            // call onResponse with the mocked response

            final SettableFuture<ServiceFilterResponse> resultFuture = SettableFuture.create();

            resultFuture.setException(new MobileServiceException("Error while processing request",
                    new MobileServiceException(String.format("{'code': %d}", response.getStatus().code))));

            return resultFuture;

        }
    });

    // Create get the MobileService table
    MobileServiceJsonTable msTable = client.getTable(tableName);

    JsonObject json = new JsonParser().parse("{'myField': 'invalid value'}").getAsJsonObject();

    // Call the insert method
    try {
        msTable.insert(json).get();
        Assert.fail();
    } catch (Exception exception) {

        Exception testException = null;

        if (exception instanceof ExecutionException) {
            testException = (Exception) exception.getCause();
        } else {
            testException = exception;
        }

        assertTrue(testException instanceof MobileServiceException);
        assertTrue(testException.getCause().getMessage().contains("500"));
    }
}