From f1847ef5f6add04ca1579504fd63ec0bbe181a96 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 29 Mar 2021 10:26:10 -0700 Subject: [PATCH] test: add tests for scopes field in TokenRequest (#640) * test: add failing test empty scope list * fix: ignore scopes param if provided an empty list * revert: change to TokenRequest, test asserts current behavior * add charset * chore: fix lint --- .../client/auth/oauth2/TokenRequestTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/TokenRequestTest.java b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/TokenRequestTest.java index da34ed982..0b5c50e33 100644 --- a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/TokenRequestTest.java +++ b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/TokenRequestTest.java @@ -15,8 +15,14 @@ package com.google.api.client.auth.oauth2; import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpContent; +import com.google.api.client.http.UrlEncodedContent; import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.testing.http.MockHttpTransport; +import com.google.common.collect.ImmutableList; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import junit.framework.TestCase; /** @@ -45,4 +51,36 @@ static void check(TokenRequest request, String grantType) { assertEquals(JSON_FACTORY, request.getJsonFactory()); assertEquals(AUTHORIZATION_SERVER_URL, request.getTokenServerUrl()); } + + public void testScopes() throws IOException { + TokenRequest request = + new TokenRequest(TRANSPORT, JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo") + .setScopes(ImmutableList.of("scope1")); + HttpContent content = new UrlEncodedContent(request); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + content.writeTo(outputStream); + String encoded = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); + assertEquals("grant_type=foo&scope=scope1", encoded); + } + + public void testEmptyScopes() throws IOException { + TokenRequest request = + new TokenRequest(TRANSPORT, JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo") + .setScopes(ImmutableList.of()); + HttpContent content = new UrlEncodedContent(request); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + content.writeTo(outputStream); + String encoded = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); + assertEquals("grant_type=foo&scope", encoded); + } + + public void testNullScopes() throws IOException { + TokenRequest request = + new TokenRequest(TRANSPORT, JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo").setScopes(null); + HttpContent content = new UrlEncodedContent(request); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + content.writeTo(outputStream); + String encoded = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); + assertEquals("grant_type=foo", encoded); + } }