|
| 1 | +package com.testingbot.tunnel; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import static org.assertj.core.api.Assertions.assertThat; |
| 7 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests for proxy configuration validation |
| 11 | + */ |
| 12 | +class ProxyValidationTest { |
| 13 | + |
| 14 | + private App app; |
| 15 | + |
| 16 | + @BeforeEach |
| 17 | + void setUp() { |
| 18 | + app = new App(); |
| 19 | + app.setClientKey("test"); |
| 20 | + app.setClientSecret("test"); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void setProxy_withValidHostAndPort_shouldSucceed() { |
| 25 | + // Given: Valid proxy configuration |
| 26 | + // When: Setting proxy |
| 27 | + app.setProxy("proxy.example.com:8080"); |
| 28 | + |
| 29 | + // Then: Should be set |
| 30 | + assertThat(app.getProxy()).isEqualTo("proxy.example.com:8080"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void setProxy_withValidHostOnly_shouldSucceed() { |
| 35 | + // Given: Valid proxy hostname without port |
| 36 | + // When: Setting proxy |
| 37 | + app.setProxy("proxy.example.com"); |
| 38 | + |
| 39 | + // Then: Should be set |
| 40 | + assertThat(app.getProxy()).isEqualTo("proxy.example.com"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void setProxy_withIPAddress_shouldSucceed() { |
| 45 | + // Given: Valid IP address |
| 46 | + // When: Setting proxy |
| 47 | + app.setProxy("192.168.1.1:3128"); |
| 48 | + |
| 49 | + // Then: Should be set |
| 50 | + assertThat(app.getProxy()).isEqualTo("192.168.1.1:3128"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void setProxy_withInvalidPort_shouldThrowException() { |
| 55 | + // Given: Invalid port number |
| 56 | + // When/Then: Setting proxy should throw exception |
| 57 | + assertThatThrownBy(() -> app.setProxy("proxy.example.com:99999")) |
| 58 | + .isInstanceOf(IllegalArgumentException.class) |
| 59 | + .hasMessageContaining("Invalid proxy port"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void setProxy_withNegativePort_shouldThrowException() { |
| 64 | + // Given: Negative port number |
| 65 | + // When/Then: Setting proxy should throw exception |
| 66 | + assertThatThrownBy(() -> app.setProxy("proxy.example.com:-1")) |
| 67 | + .isInstanceOf(IllegalArgumentException.class) |
| 68 | + .hasMessageContaining("Invalid proxy port"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void setProxy_withNonNumericPort_shouldThrowException() { |
| 73 | + // Given: Non-numeric port |
| 74 | + // When/Then: Setting proxy should throw exception |
| 75 | + assertThatThrownBy(() -> app.setProxy("proxy.example.com:abc")) |
| 76 | + .isInstanceOf(IllegalArgumentException.class) |
| 77 | + .hasMessageContaining("Invalid proxy port"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void setProxy_withEmptyHost_shouldThrowException() { |
| 82 | + // Given: Empty hostname |
| 83 | + // When/Then: Setting proxy should throw exception |
| 84 | + assertThatThrownBy(() -> app.setProxy(":8080")) |
| 85 | + .isInstanceOf(IllegalArgumentException.class) |
| 86 | + .hasMessageContaining("Invalid proxy format"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void setProxy_withWhitespace_shouldTrim() { |
| 91 | + // Given: Proxy with whitespace |
| 92 | + // When: Setting proxy |
| 93 | + app.setProxy(" proxy.example.com:8080 "); |
| 94 | + |
| 95 | + // Then: Should be trimmed |
| 96 | + assertThat(app.getProxy()).isEqualTo("proxy.example.com:8080"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void setProxyAuth_withValidCredentials_shouldSucceed() { |
| 101 | + // Given: Valid credentials |
| 102 | + // When: Setting proxy auth |
| 103 | + app.setProxyAuth("username:password"); |
| 104 | + |
| 105 | + // Then: Should be set |
| 106 | + assertThat(app.getProxyAuth()).isEqualTo("username:password"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void setProxyAuth_withPasswordContainingColon_shouldSucceed() { |
| 111 | + // Given: Password containing colon |
| 112 | + // When: Setting proxy auth |
| 113 | + app.setProxyAuth("user:pass:word"); |
| 114 | + |
| 115 | + // Then: Should be set (split on first colon only) |
| 116 | + assertThat(app.getProxyAuth()).isEqualTo("user:pass:word"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void setProxyAuth_withoutColon_shouldThrowException() { |
| 121 | + // Given: Credentials without colon |
| 122 | + // When/Then: Setting proxy auth should throw exception |
| 123 | + assertThatThrownBy(() -> app.setProxyAuth("usernamepassword")) |
| 124 | + .isInstanceOf(IllegalArgumentException.class) |
| 125 | + .hasMessageContaining("Invalid proxy auth format"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void setProxyAuth_withEmptyUsername_shouldThrowException() { |
| 130 | + // Given: Empty username |
| 131 | + // When/Then: Setting proxy auth should throw exception |
| 132 | + assertThatThrownBy(() -> app.setProxyAuth(":password")) |
| 133 | + .isInstanceOf(IllegalArgumentException.class) |
| 134 | + .hasMessageContaining("Username cannot be empty"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void setProxyAuth_withWhitespace_shouldTrim() { |
| 139 | + // Given: Credentials with whitespace |
| 140 | + // When: Setting proxy auth |
| 141 | + app.setProxyAuth(" user:pass "); |
| 142 | + |
| 143 | + // Then: Should be trimmed |
| 144 | + assertThat(app.getProxyAuth()).isEqualTo("user:pass"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void setProxyAuth_withNull_shouldAcceptNull() { |
| 149 | + // Given: Null credentials |
| 150 | + // When: Setting null |
| 151 | + app.setProxyAuth(null); |
| 152 | + |
| 153 | + // Then: Should be null |
| 154 | + assertThat(app.getProxyAuth()).isNull(); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void setProxy_withNull_shouldAcceptNull() { |
| 159 | + // Given: Null proxy |
| 160 | + // When: Setting null |
| 161 | + app.setProxy(null); |
| 162 | + |
| 163 | + // Then: Should be null |
| 164 | + assertThat(app.getProxy()).isNull(); |
| 165 | + } |
| 166 | +} |
0 commit comments