|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | + |
| 8 | +import org.gitlab4j.api.utils.Oauth2LoginStreamingOutput; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | + |
| 13 | +public class TestOauth2LoginStreamingOutput { |
| 14 | + |
| 15 | + private static final String USERNAME = "test-user"; |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testPasswordsWithBackslashes() throws Exception { |
| 19 | + |
| 20 | + final String password = "Password with \\backslashes\\"; |
| 21 | + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { |
| 22 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 23 | + oauth2Stream.write(stream); |
| 24 | + |
| 25 | + String json = stream.toString(StandardCharsets.UTF_8.name()); |
| 26 | + System.out.println(json); |
| 27 | + JsonNode tree = JsonUtils.readTreeFromString(json); |
| 28 | + assertEquals(password, tree.path("password").asText()); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testPasswordsWithDoubleQuotes() throws Exception { |
| 34 | + |
| 35 | + final String password = "Password with \"double quotes\""; |
| 36 | + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { |
| 37 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 38 | + oauth2Stream.write(stream); |
| 39 | + |
| 40 | + String json = stream.toString(StandardCharsets.UTF_8.name()); |
| 41 | + System.out.println(json); |
| 42 | + JsonNode tree = JsonUtils.readTreeFromString(json); |
| 43 | + assertEquals(password, tree.path("password").asText()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testPasswordsWithSpecialLetters() throws Exception { |
| 49 | + |
| 50 | + final String password = "Password with special letters 'Ää - Öö - Üü - ẞ'"; |
| 51 | + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { |
| 52 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 53 | + oauth2Stream.write(stream); |
| 54 | + |
| 55 | + String json = stream.toString(StandardCharsets.UTF_8.name()); |
| 56 | + System.out.println(json); |
| 57 | + JsonNode tree = JsonUtils.readTreeFromString(json); |
| 58 | + assertEquals(password, tree.path("password").asText()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testPasswordsWithManySpecialChars() throws Exception { |
| 64 | + |
| 65 | + final String password = "Password with many special chars '\\ - \" - [] - () - ~ - ! - ^ - ` - Ää - Öö - Üü - ẞ'"; |
| 66 | + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { |
| 67 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 68 | + oauth2Stream.write(stream); |
| 69 | + |
| 70 | + String json = stream.toString(StandardCharsets.UTF_8.name()); |
| 71 | + System.out.println(json); |
| 72 | + JsonNode tree = JsonUtils.readTreeFromString(json); |
| 73 | + assertEquals(password, tree.path("password").asText()); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments