|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.http.apache5;
|
17 | 17 |
|
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
18 | 20 | import java.net.InetAddress;
|
19 | 21 | import java.time.Duration;
|
20 | 22 | import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
|
|
23 | 25 | import org.apache.hc.core5.http.HttpException;
|
24 | 26 | import org.apache.hc.core5.http.HttpHost;
|
25 | 27 | import org.apache.hc.core5.http.protocol.HttpContext;
|
| 28 | +import org.apache.logging.log4j.Level; |
26 | 29 | import org.junit.jupiter.api.DisplayName;
|
| 30 | +import org.junit.jupiter.api.Nested; |
27 | 31 | import software.amazon.awssdk.http.SdkHttpClient;
|
28 | 32 | import software.amazon.awssdk.http.SdkHttpClientLocalAddressFunctionalTestSuite;
|
| 33 | +import software.amazon.awssdk.testutils.LogCaptor; |
29 | 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 | + */ |
30 | 42 | @DisplayName("Apache5 HTTP Client - Local Address Functional Tests")
|
31 |
| -class Apache5HttpClientLocalAddressFunctionalTest extends SdkHttpClientLocalAddressFunctionalTestSuite { |
| 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 { |
32 | 60 |
|
33 |
| - @Override |
34 |
| - protected SdkHttpClient createHttpClient(InetAddress localAddress, Duration connectionTimeout) { |
35 |
| - HttpRoutePlanner routePlanner = createLocalAddressRoutePlanner(localAddress); |
| 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 | + } |
36 | 69 |
|
37 |
| - return Apache5HttpClient.builder() |
38 |
| - .httpRoutePlanner(routePlanner) |
39 |
| - .connectionTimeout(connectionTimeout) |
40 |
| - .build(); |
| 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 | + } |
41 | 78 | }
|
42 | 79 |
|
43 |
| - private HttpRoutePlanner createLocalAddressRoutePlanner(InetAddress localAddress) { |
44 |
| - return new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE) { |
45 |
| - @Override |
46 |
| - protected InetAddress determineLocalAddress(HttpHost firstHop, HttpContext context) throws HttpException { |
47 |
| - return localAddress != null ? localAddress : super.determineLocalAddress(firstHop, context); |
| 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 | + }); |
48 | 105 | }
|
49 |
| - }; |
| 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 | + } |
50 | 118 | }
|
51 | 119 | }
|
0 commit comments