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.EnhancedPushTests.java

License:Open Source License

public void testUnregister() throws Throwable {

    final Container container = new Container();

    MobileServiceClient client = null;/*from   w w  w.  j  av a 2  s.  c o m*/

    String installationId = MobileServiceApplication.getInstallationId(getInstrumentation().getTargetContext());

    final String expectedUrl = appUrl + pnsApiUrl + "/installations/" + Uri.encode(installationId);

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

        client = client.withFilter(new ServiceFilter() {

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

                container.requestUrl = request.getUrl();
                container.requestMethod = request.getMethod();

                ServiceFilterResponseMock mockResponse = new ServiceFilterResponseMock();
                mockResponse.setStatus(new StatusLine(Protocol.HTTP_2, 204, ""));

                ServiceFilterRequestMock mockRequest = new ServiceFilterRequestMock(mockResponse);

                return nextServiceFilterCallback.onNext(mockRequest);
            }
        });

        final MobileServicePush push = client.getPush();
        push.unregister().get();

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

        fail(container.exception.getMessage());
    }

    // Asserts
    Assert.assertEquals(expectedUrl, container.requestUrl);
    Assert.assertEquals(HttpConstants.DeleteMethod, container.requestMethod);
}

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

License:Open Source License

public void testRegister() throws Throwable {

    final Container container = new Container();

    MobileServiceClient client = null;/*from w  ww  . j a v a2 s. co m*/
    final String handle = "handle";

    String installationId = MobileServiceApplication.getInstallationId(getInstrumentation().getTargetContext());

    final String expectedUrl = appUrl + pnsApiUrl + "/installations/" + Uri.encode(installationId);
    final String expectedContent = "{\"pushChannel\":\"handle\",\"platform\":\"gcm\"}";
    try {
        client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

        client = client.withFilter(new ServiceFilter() {

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

                container.requestUrl = request.getUrl();
                container.requestContent = request.getContent();
                container.requestMethod = request.getMethod();

                ServiceFilterResponseMock mockResponse = new ServiceFilterResponseMock();
                mockResponse.setStatus(new StatusLine(Protocol.HTTP_2, 204, ""));

                ServiceFilterRequestMock mockRequest = new ServiceFilterRequestMock(mockResponse);

                return nextServiceFilterCallback.onNext(mockRequest);
            }
        });

        final MobileServicePush push = client.getPush();
        push.register(handle).get();

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

        fail(container.exception.getMessage());
    }

    // Asserts
    Assert.assertEquals(expectedUrl, container.requestUrl);
    Assert.assertEquals(expectedContent, container.requestContent);
    Assert.assertEquals(HttpConstants.PutMethod, container.requestMethod);
}

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

License:Open Source License

public void testRegisterTemplate() throws Throwable {

    final Container container = new Container();

    MobileServiceClient client = null;/*from  ww  w.j a v  a  2s  .c o m*/
    final String handle = "handle";

    String installationId = MobileServiceApplication.getInstallationId(getInstrumentation().getTargetContext());

    final String expectedUrl = appUrl + pnsApiUrl + "/installations/" + Uri.encode(installationId);
    final String expectedContent = "{\"pushChannel\":\"handle\",\"platform\":\"gcm\",\"templates\":{\"template1\":{\"body\":\"{\\\"data\\\":\\\"abc\\\"}\"}}}";
    try {
        client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());

        client = client.withFilter(new ServiceFilter() {

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

                container.requestUrl = request.getUrl();
                container.requestContent = request.getContent();
                container.requestMethod = request.getMethod();

                ServiceFilterResponseMock mockResponse = new ServiceFilterResponseMock();
                mockResponse.setStatus(new StatusLine(Protocol.HTTP_2, 204, ""));

                ServiceFilterRequestMock mockRequest = new ServiceFilterRequestMock(mockResponse);

                return nextServiceFilterCallback.onNext(mockRequest);
            }
        });

        final MobileServicePush push = client.getPush();
        push.registerTemplate(handle, "template1", "{\"data\":\"abc\"}").get();

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

        fail(container.exception.getMessage());
    }

    // Asserts
    Assert.assertEquals(expectedUrl, container.requestUrl);
    Assert.assertEquals(expectedContent, container.requestContent);
    Assert.assertEquals(HttpConstants.PutMethod, container.requestMethod);
}

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

License:Open Source License

private ServiceFilter getTestFilter(final int statusCode, final String content) {
    return new ServiceFilter() {

        @Override/*from   w  ww.  j  a v  a 2 s  .co  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, ""));
            response.setContent(content);

            // create a mock request to replace the existing one
            ServiceFilterRequestMock requestMock = new ServiceFilterRequestMock(response);
            return nextServiceFilterCallback.onNext(requestMock);
        }
    };
}

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

License:Open Source License

private void testLoginShouldThrowError(final MobileServiceAuthenticationProvider provider) throws Throwable {
    final ResultsContainer result = new ResultsContainer();
    final String errorMessage = "fake error";
    final String errorJson = "{error:'" + errorMessage + "'}";

    // Create client
    MobileServiceClient client = null;/*from   w w  w.j  a va 2 s . c  o m*/
    try {
        client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());
    } catch (MalformedURLException e) {
    }

    // Add a new filter to the client
    client = client.withFilter(new ServiceFilter() {

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

            result.setRequestUrl(request.getUrl());

            ServiceFilterResponseMock response = new ServiceFilterResponseMock();
            response.setContent(errorJson);
            response.setStatus(new StatusLine(Protocol.HTTP_2, 400, ""));

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

            resultFuture.set(response);

            return resultFuture;
        }
    });

    try {
        client.login(provider, "{myToken:123}").get();
        Assert.fail();
    } catch (Exception exception) {
        assertTrue(exception.getCause() instanceof MobileServiceException);
        MobileServiceException cause = (MobileServiceException) exception.getCause().getCause();
        assertEquals(errorMessage, cause.getMessage());
    }
}

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

License:Open Source License

public void testLoginShouldParseJsonUserCorreclty() throws Throwable {

    final String userId = "id";
    final String userToken = "userToken";

    MobileServiceClient client = null;//from w  w  w .j a v a2s. co  m
    try {
        client = new MobileServiceClient(appUrl, getInstrumentation().getTargetContext());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    client = client.withFilter(new ServiceFilter() {

        @Override
        public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request,
                NextServiceFilterCallback nextServiceFilterCallback) {
            // User JSon Template
            String userJsonTemplate = "{\"user\":{\"userId\":\"%s\"}, \"authenticationToken\":\"%s\"}";
            // Create a mock response simulating an error
            ServiceFilterResponseMock response = new ServiceFilterResponseMock();
            response.setStatus(new StatusLine(Protocol.HTTP_2, 200, ""));
            response.setContent(String.format(userJsonTemplate, userId, userToken));

            // create a mock request to replace the existing one
            ServiceFilterRequestMock requestMock = new ServiceFilterRequestMock(response);
            return nextServiceFilterCallback.onNext(requestMock);
        }
    });

    MobileServiceUser user = client.login(MobileServiceAuthenticationProvider.Facebook, "oAuthToken").get();
    // Asserts
    assertNotNull(user);
    assertEquals(userId, user.getUserId());
    assertEquals(userToken, user.getAuthenticationToken());
}

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

License:Open Source License

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

    thrownExceptionFlag.Thrown = true;//from   w  w  w  .  j ava2 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.insert(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

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

        MobileServicePushFailedException mspfe = (MobileServicePushFailedException) ex.getCause();

        assertEquals(mspfe.getPushCompletionResult().getStatus(),
                MobileServicePushStatus.CancelledByAuthenticationError);
        assertEquals(mspfe.getPushCompletionResult().getOperationErrors().size(), 0);
        assertEquals(client.getSyncContext().getPendingOperations(), 1);

        thrownExceptionFlag.Thrown = false;

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

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

        return;
    }

    assertTrue(false);
}

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

License:Open Source License

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

    thrownExceptionFlag.Thrown = true;/*from ww  w  .  j a  v  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.insert(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.PostMethod);
            assertEquals(ex2.getMessage(),
                    "java.lang.IllegalStateException: An insert 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 testDeleteNoCollapseInsertWhenFailedInsertIsInQueueOnAuthentication() throws Throwable {
    MobileServiceLocalStoreMock store = new MobileServiceLocalStoreMock();
    final ServiceFilterContainer serviceFilterContainer = new ServiceFilterContainer();
    final ThrownExceptionFlag thrownExceptionFlag = new ThrownExceptionFlag();
    String expectedUrl = "http://myapp.com/tables/stringidtype/abc";

    thrownExceptionFlag.Thrown = true;//  w  w w. j  av a 2 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.insert(item).get();
    assertEquals(client.getSyncContext().getPendingOperations(), 1);

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

        assertEquals(client.getSyncContext().getPendingOperations(), 1);

        thrownExceptionFlag.Thrown = false;

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

        assertEquals(client.getSyncContext().getPendingOperations(), 0);

        assertEquals(serviceFilterContainer.Requests.get(1).Url, expectedUrl);
        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 testUpdateCollapseWhenInsertIsInQueueOnAuthenticationError() 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;//from ww w .j a v  a 2 s  . 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.insert(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.PostMethod);
        assertEquals(serviceFilterContainer.Requests.get(1).Method, HttpConstants.PostMethod);
        assertEquals(serviceFilterContainer.Requests.get(1).Content, expectedUpdateContent);

        return;
    }

    assertTrue(false);
}