|
1 | 1 | package com.getindata.connectors.http.internal.utils;
|
2 | 2 |
|
| 3 | +import java.net.*; |
| 4 | +import java.util.Arrays; |
3 | 5 | import java.util.Map;
|
4 | 6 | import java.util.Optional;
|
5 | 7 | import java.util.Properties;
|
@@ -51,29 +53,133 @@ public void shouldConvertNoProperty() {
|
51 | 53 | }
|
52 | 54 |
|
53 | 55 | @Test
|
54 |
| - public void shouldGetProxyConfigWithAuthenticator() { |
| 56 | + public void shouldGetProxyConfigWithAuthenticator() throws UnknownHostException { |
55 | 57 | String proxyHost = "proxy";
|
56 |
| - Integer proxyPort = 8080; |
| 58 | + Integer proxyPort = 9090; |
57 | 59 | Optional<String> proxyUsername = Optional.of("username");
|
58 | 60 | Optional<String> proxyPassword = Optional.of("password");
|
59 | 61 |
|
60 | 62 | ProxyConfig proxyConfig = new ProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword );
|
61 | 63 | assertThat(proxyConfig.getHost().equals("proxy"));
|
62 |
| - assertThat(proxyConfig.getPort() == 8080); |
63 | 64 | assertThat(proxyConfig.getAuthenticator().isPresent());
|
| 65 | + |
| 66 | + PasswordAuthentication auth = proxyConfig.getAuthenticator().orElseGet(null) |
| 67 | + .requestPasswordAuthenticationInstance( |
| 68 | + "proxy", // host |
| 69 | + InetAddress.getByName("127.0.0.1"), // address |
| 70 | + 9090, // port |
| 71 | + "http", // protocol |
| 72 | + "Please authenticate", // prompt |
| 73 | + "basic", // scheme |
| 74 | + null, // URL |
| 75 | + Authenticator.RequestorType.SERVER // Requestor type |
| 76 | + ); |
| 77 | + |
| 78 | + PasswordAuthentication auth2 = proxyConfig.getAuthenticator().orElseGet(null) |
| 79 | + .requestPasswordAuthenticationInstance( |
| 80 | + "proxy", // host |
| 81 | + InetAddress.getByName("127.0.0.1"), // address |
| 82 | + 9090, // port |
| 83 | + "http", // protocol |
| 84 | + "Please authenticate", // prompt |
| 85 | + "basic", // scheme |
| 86 | + null, // URL |
| 87 | + Authenticator.RequestorType.PROXY // Requestor type |
| 88 | + ); |
| 89 | + |
| 90 | + assertThat(auth).isNull(); |
| 91 | + assertThat(auth2).isNotNull(); |
| 92 | + assertThat(auth2.getUserName().equals("username")).isTrue(); |
| 93 | + assertThat(Arrays.equals(auth2.getPassword(), "password".toCharArray())).isTrue(); |
64 | 94 | }
|
65 | 95 |
|
66 | 96 | @Test
|
67 |
| - public void shouldGetProxyConfigWithoutAuthenticator() { |
| 97 | + public void shouldGetProxyConfigWithAuthenticatorServer() throws UnknownHostException { |
68 | 98 | String proxyHost = "proxy";
|
69 | 99 | Integer proxyPort = 8080;
|
70 | 100 | Optional<String> proxyUsername = Optional.of("username");
|
71 |
| - Optional<String> proxyPassword = Optional.empty(); |
| 101 | + Optional<String> proxyPassword = Optional.of("password"); |
72 | 102 |
|
73 | 103 | ProxyConfig proxyConfig = new ProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword );
|
74 |
| - assertThat(proxyConfig.getHost().equals("proxy")); |
75 |
| - assertThat(proxyConfig.getPort() == 8080); |
76 |
| - assertThat(proxyConfig.getAuthenticator().isEmpty()); |
| 104 | + assertThat(proxyConfig.getHost().equals("proxy")).isTrue(); |
| 105 | + assertThat(proxyConfig.getAuthenticator().isPresent()).isTrue(); |
| 106 | + |
| 107 | + PasswordAuthentication auth = proxyConfig.getAuthenticator().orElseGet(null) |
| 108 | + .requestPasswordAuthenticationInstance( |
| 109 | + "proxy", // host |
| 110 | + InetAddress.getByName("127.0.0.1"), // address |
| 111 | + 8080, // port |
| 112 | + "http", // protocol |
| 113 | + "Please authenticate", // prompt |
| 114 | + "basic", // scheme |
| 115 | + null, // URL |
| 116 | + Authenticator.RequestorType.SERVER // Requestor type |
| 117 | + ); |
| 118 | + |
| 119 | + PasswordAuthentication auth2 = proxyConfig.getAuthenticator().orElseGet(null) |
| 120 | + .requestPasswordAuthenticationInstance( |
| 121 | + "proxy", // host |
| 122 | + InetAddress.getByName("127.0.0.1"), // address |
| 123 | + 8080, // port |
| 124 | + "http", // protocol |
| 125 | + "Please authenticate", // prompt |
| 126 | + "basic", // scheme |
| 127 | + null, // URL |
| 128 | + Authenticator.RequestorType.PROXY // Requestor type |
| 129 | + ); |
| 130 | + |
| 131 | + assertThat(auth).isNull(); |
| 132 | + assertThat(auth2).isNotNull(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void shouldGetProxyConfigWithAuthenticatorWrongHost() throws UnknownHostException { |
| 137 | + String proxyHost = "proxy"; |
| 138 | + Integer proxyPort = 8080; |
| 139 | + Optional<String> proxyUsername = Optional.of("username"); |
| 140 | + Optional<String> proxyPassword = Optional.of("password"); |
| 141 | + |
| 142 | + ProxyConfig proxyConfig = new ProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword ); |
| 143 | + assertThat(proxyConfig.getHost().equals("proxy")).isTrue(); |
| 144 | + assertThat(proxyConfig.getAuthenticator().isPresent()).isTrue(); |
| 145 | + |
| 146 | + PasswordAuthentication auth = proxyConfig.getAuthenticator().get() |
| 147 | + .requestPasswordAuthenticationInstance( |
| 148 | + "wrong", // host |
| 149 | + InetAddress.getByName("127.0.0.1"), // address |
| 150 | + 8080, // port |
| 151 | + "http", // protocol |
| 152 | + "Please authenticate", // prompt |
| 153 | + "basic", // scheme |
| 154 | + null, // URL |
| 155 | + Authenticator.RequestorType.PROXY // Requestor type |
| 156 | + ); |
| 157 | + |
| 158 | + PasswordAuthentication auth2 = proxyConfig.getAuthenticator().orElseGet(null) |
| 159 | + .requestPasswordAuthenticationInstance( |
| 160 | + "proxy", // host |
| 161 | + InetAddress.getByName("127.0.0.1"), // address |
| 162 | + 8080, // port |
| 163 | + "http", // protocol |
| 164 | + "Please authenticate", // prompt |
| 165 | + "basic", // scheme |
| 166 | + null, // URL |
| 167 | + Authenticator.RequestorType.PROXY // Requestor type |
| 168 | + ); |
| 169 | + |
| 170 | + assertThat(auth).isNull(); |
| 171 | + assertThat(auth2).isNotNull(); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void shouldGetProxyConfigWithoutAuthenticator() throws MalformedURLException, UnknownHostException { |
| 176 | + String proxyHost = "proxy"; |
| 177 | + Optional<String> proxyUsername = Optional.of("username"); |
| 178 | + Optional<String> proxyPassword = Optional.empty(); |
| 179 | + |
| 180 | + ProxyConfig proxyConfig = new ProxyConfig(proxyHost, 80, proxyUsername, proxyPassword ); |
| 181 | + assertThat(proxyConfig.getHost().equals("proxy")).isTrue(); |
| 182 | + assertThat(proxyConfig.getAuthenticator().isEmpty()).isTrue(); |
77 | 183 | }
|
78 | 184 |
|
79 | 185 | @Test
|
|
0 commit comments