Search in sources :

Example 6 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method firebasePost.

@Test
public void firebasePost() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Game game = new Game();
    firebaseChannel.firebasePost(FIREBASE_DB_URL + "/my/path", game);
    verify(mockHttpTransport, times(1)).buildRequest("POST", FIREBASE_DB_URL + "/my/path");
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 7 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class MoveServletTest method doPost_myTurn_move.

@Test
public void doPost_myTurn_move() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    when(mockRequest.getParameter("cell")).thenReturn("1");
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doPost(mockRequest, mockResponse);
    game = ofy.load().type(Game.class).id(gameKey).safe();
    assertEquals(game.board, " X       ");
    verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 8 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class OpenedServletTest method doPost_open.

@Test
public void doPost_open() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doPost(mockRequest, mockResponse);
    verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 9 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class TicTacToeServletTest method doGet_noGameKey.

@Test
public void doGet_noGameKey() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doGet(mockRequest, mockResponse);
    // Make sure the game object was created for a new game
    Objectify ofy = ObjectifyService.ofy();
    Game game = ofy.load().type(Game.class).first().safe();
    assertEquals(game.userX, USER_ID);
    verify(mockHttpTransport, times(1)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
    verify(requestDispatcher).forward(mockRequest, mockResponse);
    verify(mockRequest).setAttribute(eq("token"), anyString());
    verify(mockRequest).setAttribute("game_key", game.id);
    verify(mockRequest).setAttribute("me", USER_ID);
    verify(mockRequest).setAttribute("channel_id", USER_ID + game.id);
    verify(mockRequest).setAttribute(eq("initial_message"), anyString());
    verify(mockRequest).setAttribute(eq("game_link"), anyString());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) Mockito.anyString(org.mockito.Mockito.anyString) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 10 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project google-auth-library-java by google.

the class MockTokenServerTransport method buildRequest.

@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
    buildRequestCount++;
    if (error != null) {
        throw error;
    }
    int questionMarkPos = url.indexOf('?');
    final String urlWithoutQUery = (questionMarkPos > 0) ? url.substring(0, questionMarkPos) : url;
    final String query = (questionMarkPos > 0) ? url.substring(questionMarkPos + 1) : "";
    if (urlWithoutQUery.equals(tokenServerUri.toString())) {
        return new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() throws IOException {
                IOException responseError = responseErrorSequence.poll();
                if (responseError != null) {
                    throw responseError;
                }
                LowLevelHttpResponse response = responseSequence.poll();
                if (response != null) {
                    return response;
                }
                String content = this.getContentAsString();
                Map<String, String> query = TestUtils.parseQuery(content);
                String accessToken;
                String refreshToken = null;
                String foundId = query.get("client_id");
                if (foundId != null) {
                    if (!clients.containsKey(foundId)) {
                        throw new IOException("Client ID not found.");
                    }
                    String foundSecret = query.get("client_secret");
                    String expectedSecret = clients.get(foundId);
                    if (foundSecret == null || !foundSecret.equals(expectedSecret)) {
                        throw new IOException("Client secret not found.");
                    }
                    String grantType = query.get("grant_type");
                    if (grantType != null && grantType.equals("authorization_code")) {
                        String foundCode = query.get("code");
                        if (!codes.containsKey(foundCode)) {
                            throw new IOException("Authorization code not found");
                        }
                        refreshToken = codes.get(foundCode);
                    } else {
                        refreshToken = query.get("refresh_token");
                    }
                    if (!refreshTokens.containsKey(refreshToken)) {
                        throw new IOException("Refresh Token not found.");
                    }
                    accessToken = refreshTokens.get(refreshToken);
                } else if (query.containsKey("grant_type")) {
                    String grantType = query.get("grant_type");
                    if (!EXPECTED_GRANT_TYPE.equals(grantType)) {
                        throw new IOException("Unexpected Grant Type.");
                    }
                    String assertion = query.get("assertion");
                    JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
                    String foundEmail = signature.getPayload().getIssuer();
                    if (!serviceAccounts.containsKey(foundEmail)) {
                        throw new IOException("Service Account Email not found as issuer.");
                    }
                    accessToken = serviceAccounts.get(foundEmail);
                    String foundScopes = (String) signature.getPayload().get("scope");
                    if (foundScopes == null || foundScopes.length() == 0) {
                        throw new IOException("Scopes not found.");
                    }
                } else {
                    throw new IOException("Unknown token type.");
                }
                // Create the JSON response
                GenericJson refreshContents = new GenericJson();
                refreshContents.setFactory(JSON_FACTORY);
                refreshContents.put("access_token", accessToken);
                refreshContents.put("expires_in", expiresInSeconds);
                refreshContents.put("token_type", "Bearer");
                if (refreshToken != null) {
                    refreshContents.put("refresh_token", refreshToken);
                }
                String refreshText = refreshContents.toPrettyString();
                return new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE).setContent(refreshText);
            }
        };
    } else if (urlWithoutQUery.equals(OAuth2Utils.TOKEN_REVOKE_URI.toString())) {
        return new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() throws IOException {
                Map<String, String> parameters = TestUtils.parseQuery(query);
                String token = parameters.get("token");
                if (token == null) {
                    throw new IOException("Token to revoke not found.");
                }
                // Token could be access token or refresh token so remove keys and values
                refreshTokens.values().removeAll(Collections.singleton(token));
                refreshTokens.remove(token);
                return new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE);
            }
        };
    }
    return super.buildRequest(method, url);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) JsonWebSignature(com.google.api.client.json.webtoken.JsonWebSignature) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Aggregations

LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)24 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)21 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)20 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)18 IOException (java.io.IOException)18 Test (org.junit.Test)16 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)13 GenericJson (com.google.api.client.json.GenericJson)6 Objectify (com.googlecode.objectify.Objectify)5 JsonFactory (com.google.api.client.json.JsonFactory)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 Storage (com.google.api.services.storage.Storage)3 HttpResponse (com.google.api.client.http.HttpResponse)2 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)2 Mockito.anyString (org.mockito.Mockito.anyString)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 JsonWebSignature (com.google.api.client.json.webtoken.JsonWebSignature)1 Get (com.google.api.services.storage.Storage.Objects.Get)1