|
13 | 13 |
|
14 | 14 | import java.time.Duration; |
15 | 15 | import java.util.ArrayList; |
| 16 | +import java.util.List; |
16 | 17 | import java.util.Map; |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
17 | 19 | import org.junit.jupiter.api.Assertions; |
18 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import software.amazon.smithy.java.auth.api.SignResult; |
| 22 | +import software.amazon.smithy.java.auth.api.identity.Identity; |
| 23 | +import software.amazon.smithy.java.auth.api.identity.IdentityResolver; |
| 24 | +import software.amazon.smithy.java.auth.api.identity.IdentityResult; |
19 | 25 | import software.amazon.smithy.java.aws.client.restjson.RestJsonClientProtocol; |
| 26 | +import software.amazon.smithy.java.client.core.auth.scheme.AuthScheme; |
| 27 | +import software.amazon.smithy.java.client.core.auth.scheme.AuthSchemeOption; |
20 | 28 | import software.amazon.smithy.java.client.core.auth.scheme.AuthSchemeResolver; |
| 29 | +import software.amazon.smithy.java.client.core.endpoint.Endpoint; |
| 30 | +import software.amazon.smithy.java.client.core.endpoint.EndpointAuthScheme; |
21 | 31 | import software.amazon.smithy.java.client.core.endpoint.EndpointResolver; |
22 | 32 | import software.amazon.smithy.java.client.http.JavaHttpClientTransport; |
23 | 33 | import software.amazon.smithy.java.client.http.mock.MockPlugin; |
24 | 34 | import software.amazon.smithy.java.client.http.mock.MockQueue; |
| 35 | +import software.amazon.smithy.java.context.Context; |
25 | 36 | import software.amazon.smithy.java.core.serde.document.Document; |
26 | 37 | import software.amazon.smithy.java.dynamicclient.DynamicClient; |
| 38 | +import software.amazon.smithy.java.http.api.HttpRequest; |
27 | 39 | import software.amazon.smithy.java.http.api.HttpResponse; |
28 | 40 | import software.amazon.smithy.java.io.datastream.DataStream; |
29 | 41 | import software.amazon.smithy.java.retries.api.AcquireInitialTokenRequest; |
@@ -188,6 +200,65 @@ public Builder toBuilder() { |
188 | 200 | assertThat(calls, contains("Acquire", "Refresh", "Success: 1")); |
189 | 201 | } |
190 | 202 |
|
| 203 | + @Test |
| 204 | + public void endpointAuthSchemeOverridesAugmentSignerProperties() { |
| 205 | + var service = ShapeId.from("smithy.example#Sprockets"); |
| 206 | + var testSchemeId = ShapeId.from("smithy.test#testAuth"); |
| 207 | + var TEST_KEY = Context.<String>key("test-signing-override"); |
| 208 | + var capturedProperties = new AtomicReference<Context>(); |
| 209 | + |
| 210 | + // Auth scheme with a signer that captures the properties it receives. |
| 211 | + var testScheme = AuthScheme.of( |
| 212 | + testSchemeId, |
| 213 | + HttpRequest.class, |
| 214 | + Identity.class, |
| 215 | + (request, identity, properties) -> { |
| 216 | + capturedProperties.set(properties); |
| 217 | + return new SignResult<>(request); |
| 218 | + }); |
| 219 | + |
| 220 | + // Endpoint resolver that returns an endpoint with an auth scheme override. |
| 221 | + EndpointResolver endpointResolver = params -> Endpoint.builder() |
| 222 | + .uri("https://example.com") |
| 223 | + .addAuthScheme( |
| 224 | + EndpointAuthScheme.builder() |
| 225 | + .authSchemeId(testSchemeId.toString()) |
| 226 | + .putProperty(TEST_KEY, "overridden-value") |
| 227 | + .build()) |
| 228 | + .build(); |
| 229 | + |
| 230 | + var mockQueue = new MockQueue() |
| 231 | + .enqueue(HttpResponse.builder() |
| 232 | + .statusCode(200) |
| 233 | + .body(DataStream.ofString("{\"id\":\"1\"}")) |
| 234 | + .build()); |
| 235 | + var mock = MockPlugin.builder().addQueue(mockQueue).build(); |
| 236 | + |
| 237 | + var client = DynamicClient.builder() |
| 238 | + .serviceId(service) |
| 239 | + .model(MODEL) |
| 240 | + .addPlugin(mock) |
| 241 | + .endpointResolver(endpointResolver) |
| 242 | + .authSchemeResolver(params -> List.of(new AuthSchemeOption(testSchemeId))) |
| 243 | + .putSupportedAuthSchemes(testScheme) |
| 244 | + .addIdentityResolver(new IdentityResolver<>() { |
| 245 | + @Override |
| 246 | + public IdentityResult<Identity> resolveIdentity(Context requestProperties) { |
| 247 | + return IdentityResult.of(new Identity() {}); |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public Class<Identity> identityType() { |
| 252 | + return Identity.class; |
| 253 | + } |
| 254 | + }) |
| 255 | + .build(); |
| 256 | + |
| 257 | + client.call("GetSprocket", Document.ofObject(Map.of("id", "1"))); |
| 258 | + |
| 259 | + assertThat(capturedProperties.get().get(TEST_KEY), equalTo("overridden-value")); |
| 260 | + } |
| 261 | + |
191 | 262 | private static final class Token implements RetryToken { |
192 | 263 | int retry; |
193 | 264 |
|
|
0 commit comments