Skip to content

Commit f1847ef

Browse files
authored
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
1 parent fc40bce commit f1847ef

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/TokenRequestTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
package com.google.api.client.auth.oauth2;
1616

1717
import com.google.api.client.http.GenericUrl;
18+
import com.google.api.client.http.HttpContent;
19+
import com.google.api.client.http.UrlEncodedContent;
1820
import com.google.api.client.json.gson.GsonFactory;
1921
import com.google.api.client.testing.http.MockHttpTransport;
22+
import com.google.common.collect.ImmutableList;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.nio.charset.StandardCharsets;
2026
import junit.framework.TestCase;
2127

2228
/**
@@ -45,4 +51,36 @@ static void check(TokenRequest request, String grantType) {
4551
assertEquals(JSON_FACTORY, request.getJsonFactory());
4652
assertEquals(AUTHORIZATION_SERVER_URL, request.getTokenServerUrl());
4753
}
54+
55+
public void testScopes() throws IOException {
56+
TokenRequest request =
57+
new TokenRequest(TRANSPORT, JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo")
58+
.setScopes(ImmutableList.of("scope1"));
59+
HttpContent content = new UrlEncodedContent(request);
60+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
61+
content.writeTo(outputStream);
62+
String encoded = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
63+
assertEquals("grant_type=foo&scope=scope1", encoded);
64+
}
65+
66+
public void testEmptyScopes() throws IOException {
67+
TokenRequest request =
68+
new TokenRequest(TRANSPORT, JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo")
69+
.setScopes(ImmutableList.<String>of());
70+
HttpContent content = new UrlEncodedContent(request);
71+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
72+
content.writeTo(outputStream);
73+
String encoded = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
74+
assertEquals("grant_type=foo&scope", encoded);
75+
}
76+
77+
public void testNullScopes() throws IOException {
78+
TokenRequest request =
79+
new TokenRequest(TRANSPORT, JSON_FACTORY, AUTHORIZATION_SERVER_URL, "foo").setScopes(null);
80+
HttpContent content = new UrlEncodedContent(request);
81+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
82+
content.writeTo(outputStream);
83+
String encoded = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
84+
assertEquals("grant_type=foo", encoded);
85+
}
4886
}

0 commit comments

Comments
 (0)