|
15 | 15 | package com.google.api.client.auth.oauth2;
|
16 | 16 |
|
17 | 17 | import com.google.api.client.http.GenericUrl;
|
| 18 | +import com.google.api.client.http.HttpContent; |
| 19 | +import com.google.api.client.http.UrlEncodedContent; |
18 | 20 | import com.google.api.client.json.gson.GsonFactory;
|
19 | 21 | 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; |
20 | 26 | import junit.framework.TestCase;
|
21 | 27 |
|
22 | 28 | /**
|
@@ -45,4 +51,36 @@ static void check(TokenRequest request, String grantType) {
|
45 | 51 | assertEquals(JSON_FACTORY, request.getJsonFactory());
|
46 | 52 | assertEquals(AUTHORIZATION_SERVER_URL, request.getTokenServerUrl());
|
47 | 53 | }
|
| 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 | + } |
48 | 86 | }
|
0 commit comments