Skip to content

Commit 8feda13

Browse files
committed
Use provided port value for netty and crt http client if set
1 parent cdc4f97 commit 8feda13

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/ProxyConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ private String resolveHost(String host) {
207207
}
208208

209209
private int resolvePort(int port) {
210-
return useSystemPropertyValues ? ProxySystemSetting.PROXY_PORT.getStringValue().map(Integer::parseInt).orElse(0)
210+
return port == 0 && useSystemPropertyValues ?
211+
ProxySystemSetting.PROXY_PORT.getStringValue().map(Integer::parseInt).orElse(0)
211212
: port;
212213
}
213214

@@ -222,7 +223,7 @@ private String resolveValue(String value, ProxySystemSetting systemSetting) {
222223
private static final class BuilderImpl implements Builder {
223224
private String scheme;
224225
private String host;
225-
private int port;
226+
private int port = 0;
226227
private String username;
227228
private String password;
228229
private Boolean useSystemPropertyValues = Boolean.TRUE;

http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/ProxyConfigurationTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void build_systemPropertyDefault() {
6464
@Test
6565
public void build_systemPropertyEnabled() {
6666
setProxyProperties();
67-
ProxyConfiguration config = ProxyConfiguration.builder().useSystemPropertyValues(true).build();
67+
ProxyConfiguration config = ProxyConfiguration.builder().useSystemPropertyValues(Boolean.TRUE).build();
6868

6969
assertThat(config.host()).isEqualTo(TEST_HOST);
7070
assertThat(config.port()).isEqualTo(TEST_PORT);
@@ -81,7 +81,24 @@ public void build_systemPropertyDisabled() {
8181
.port(8888)
8282
.username("username")
8383
.password("password")
84-
.useSystemPropertyValues(false).build();
84+
.useSystemPropertyValues(Boolean.FALSE).build();
85+
86+
assertThat(config.host()).isEqualTo("localhost");
87+
assertThat(config.port()).isEqualTo(8888);
88+
assertThat(config.username()).isEqualTo("username");
89+
assertThat(config.password()).isEqualTo("password");
90+
assertThat(config.scheme()).isNull();
91+
}
92+
93+
@Test
94+
public void build_systemPropertyOverride() {
95+
setProxyProperties();
96+
ProxyConfiguration config = ProxyConfiguration.builder()
97+
.host("localhost")
98+
.port(8888)
99+
.username("username")
100+
.password("password")
101+
.build();
85102

86103
assertThat(config.host()).isEqualTo("localhost");
87104
assertThat(config.port()).isEqualTo(8888);

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/ProxyConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ private String resolveHost(String host) {
231231
}
232232

233233
private int resolvePort(int port) {
234-
return useSystemPropertyValues ? ProxySystemSetting.PROXY_PORT.getStringValue().map(Integer::parseInt).orElse(0)
234+
return port == 0 && useSystemPropertyValues ?
235+
ProxySystemSetting.PROXY_PORT.getStringValue().map(Integer::parseInt).orElse(0)
235236
: port;
236237
}
237238

@@ -258,7 +259,7 @@ private Set<String> parseNonProxyHostsProperty() {
258259
private static final class BuilderImpl implements Builder {
259260
private String scheme;
260261
private String host;
261-
private int port;
262+
private int port = 0;
262263
private String username;
263264
private String password;
264265
private Set<String> nonProxyHosts = Collections.emptySet();

http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/ProxyConfigurationTest.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void build_setsAllProperties() {
5454

5555
@Test
5656
public void build_systemPropertyDefault() {
57-
setSystemProperty();
57+
setProxyProperties();
5858
ProxyConfiguration config = ProxyConfiguration.builder().build();
5959

6060
assertThat(config.host()).isEqualTo(TEST_HOST);
@@ -66,8 +66,8 @@ public void build_systemPropertyDefault() {
6666

6767
@Test
6868
public void build_systemPropertyEnabled() {
69-
setSystemProperty();
70-
ProxyConfiguration config = ProxyConfiguration.builder().useSystemPropertyValues(true).build();
69+
setProxyProperties();
70+
ProxyConfiguration config = ProxyConfiguration.builder().useSystemPropertyValues(Boolean.TRUE).build();
7171

7272
assertThat(config.host()).isEqualTo(TEST_HOST);
7373
assertThat(config.port()).isEqualTo(TEST_PORT);
@@ -78,13 +78,30 @@ public void build_systemPropertyEnabled() {
7878

7979
@Test
8080
public void build_systemPropertyDisabled() {
81-
setSystemProperty();
81+
setProxyProperties();
8282
ProxyConfiguration config = ProxyConfiguration.builder()
8383
.host("localhost")
8484
.port(8888)
8585
.username("username")
8686
.password("password")
87-
.useSystemPropertyValues(false).build();
87+
.useSystemPropertyValues(Boolean.FALSE).build();
88+
89+
assertThat(config.host()).isEqualTo("localhost");
90+
assertThat(config.port()).isEqualTo(8888);
91+
assertThat(config.username()).isEqualTo("username");
92+
assertThat(config.password()).isEqualTo("password");
93+
assertThat(config.scheme()).isNull();
94+
}
95+
96+
@Test
97+
public void build_systemPropertyOverride() {
98+
setProxyProperties();
99+
ProxyConfiguration config = ProxyConfiguration.builder()
100+
.host("localhost")
101+
.port(8888)
102+
.username("username")
103+
.password("password")
104+
.build();
88105

89106
assertThat(config.host()).isEqualTo("localhost");
90107
assertThat(config.port()).isEqualTo(8888);
@@ -190,7 +207,7 @@ private Set<String> randomSet() {
190207
return ss;
191208
}
192209

193-
private void setSystemProperty() {
210+
private void setProxyProperties() {
194211
System.setProperty("http.proxyHost", TEST_HOST);
195212
System.setProperty("http.proxyPort", Integer.toString(TEST_PORT));
196213
System.setProperty("http.proxyUser", TEST_USER);

0 commit comments

Comments
 (0)