Skip to content

Commit 8f086d7

Browse files
committed
Added a single test for localaddress , handled review comments
1 parent 1b1caa0 commit 8f086d7

File tree

3 files changed

+119
-86
lines changed

3 files changed

+119
-86
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache5;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.net.InetAddress;
21+
import java.time.Duration;
22+
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
23+
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner;
24+
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
25+
import org.apache.hc.core5.http.HttpException;
26+
import org.apache.hc.core5.http.HttpHost;
27+
import org.apache.hc.core5.http.protocol.HttpContext;
28+
import org.apache.logging.log4j.Level;
29+
import org.junit.jupiter.api.DisplayName;
30+
import org.junit.jupiter.api.Nested;
31+
import software.amazon.awssdk.http.SdkHttpClient;
32+
import software.amazon.awssdk.http.SdkHttpClientLocalAddressFunctionalTestSuite;
33+
import software.amazon.awssdk.testutils.LogCaptor;
34+
35+
/**
36+
* Functional tests for Apache5 HTTP Client's local address binding capabilities.
37+
* Tests three scenarios:
38+
* 1. Local address configured via builder
39+
* 2. Local address configured via custom route planner
40+
* 3. Both methods used together (route planner takes precedence)
41+
*/
42+
@DisplayName("Apache5 HTTP Client - Local Address Functional Tests")
43+
class Apache5HttpClientLocalAddressFunctionalTest {
44+
45+
@Nested
46+
@DisplayName("When local address is configured via builder")
47+
class LocalAddressViaBuilderTest extends SdkHttpClientLocalAddressFunctionalTestSuite {
48+
@Override
49+
protected SdkHttpClient createHttpClient(InetAddress localAddress, Duration connectionTimeout) {
50+
return Apache5HttpClient.builder()
51+
.localAddress(localAddress)
52+
.connectionTimeout(connectionTimeout)
53+
.build();
54+
}
55+
}
56+
57+
@Nested
58+
@DisplayName("When local address is configured via custom route planner")
59+
class LocalAddressViaRoutePlannerTest extends SdkHttpClientLocalAddressFunctionalTestSuite {
60+
61+
@Override
62+
protected SdkHttpClient createHttpClient(InetAddress localAddress, Duration connectionTimeout) {
63+
HttpRoutePlanner routePlanner = createLocalAddressRoutePlanner(localAddress);
64+
return Apache5HttpClient.builder()
65+
.httpRoutePlanner(routePlanner)
66+
.connectionTimeout(connectionTimeout)
67+
.build();
68+
}
69+
70+
private HttpRoutePlanner createLocalAddressRoutePlanner(InetAddress localAddress) {
71+
return new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE) {
72+
@Override
73+
protected InetAddress determineLocalAddress(HttpHost firstHop, HttpContext context) throws HttpException {
74+
return localAddress != null ? localAddress : super.determineLocalAddress(firstHop, context);
75+
}
76+
};
77+
}
78+
}
79+
80+
@Nested
81+
@DisplayName("When both route planner and builder local address are configured (route planner takes precedence)")
82+
class RoutePlannerPrecedenceTest extends SdkHttpClientLocalAddressFunctionalTestSuite {
83+
84+
private final InetAddress BUILDER_LOCAL_ADDRESS = InetAddress.getLoopbackAddress();
85+
86+
@Override
87+
protected SdkHttpClient createHttpClient(InetAddress localAddress, Duration connectionTimeout) {
88+
// The localAddress parameter will be used by the route planner
89+
// The builder's localAddress will be overridden
90+
HttpRoutePlanner routePlanner = createLocalAddressRoutePlanner(localAddress);
91+
SdkHttpClient httpClient;
92+
93+
try (LogCaptor logCaptor = LogCaptor.create(Level.DEBUG)) {
94+
httpClient = Apache5HttpClient.builder()
95+
.httpRoutePlanner(routePlanner)
96+
.localAddress(BUILDER_LOCAL_ADDRESS) // This will be overridden by route planner
97+
.connectionTimeout(connectionTimeout)
98+
.build();
99+
100+
assertThat(logCaptor.loggedEvents()).anySatisfy(logEvent -> {
101+
assertThat(logEvent.getLevel()).isEqualTo(Level.DEBUG);
102+
assertThat(logEvent.getMessage().getFormattedMessage())
103+
.contains("localAddress configuration was ignored since Route planner was explicitly provided");
104+
});
105+
}
106+
return httpClient;
107+
}
108+
109+
private HttpRoutePlanner createLocalAddressRoutePlanner(InetAddress routePlannerAddress) {
110+
return new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE) {
111+
@Override
112+
protected InetAddress determineLocalAddress(HttpHost firstHop, HttpContext context) throws HttpException {
113+
// Route planner's address takes precedence over builder's address
114+
return routePlannerAddress != null ? routePlannerAddress : super.determineLocalAddress(firstHop, context);
115+
}
116+
};
117+
}
118+
}
119+
}

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5HttpClientLocalAddressOnBuilderFunctionalTest.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5HttpClientLocalAddressRoutePlannerFunctionalTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)