|
| 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.services; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static software.amazon.awssdk.core.useragent.BusinessMetricCollection.METRIC_SEARCH_PATTERN; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 25 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 26 | +import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId; |
| 27 | +import software.amazon.awssdk.http.AbortableInputStream; |
| 28 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 29 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 30 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 31 | +import software.amazon.awssdk.regions.Region; |
| 32 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; |
| 33 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; |
| 34 | +import software.amazon.awssdk.services.sigv4aauth.Sigv4AauthAsyncClient; |
| 35 | +import software.amazon.awssdk.services.sigv4aauth.Sigv4AauthClient; |
| 36 | +import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; |
| 37 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 38 | +import software.amazon.awssdk.utils.StringInputStream; |
| 39 | + |
| 40 | +/** |
| 41 | + * Test class to verify that SIGV4A_SIGNING business metric is correctly included |
| 42 | + * in the User-Agent header when operation called using Sigv4A signing. |
| 43 | + */ |
| 44 | +class Sigv4aBusinessMetricUserAgentTest { |
| 45 | + private static final String USER_AGENT_HEADER_NAME = "User-Agent"; |
| 46 | + private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = |
| 47 | + StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); |
| 48 | + |
| 49 | + private MockSyncHttpClient mockHttpClient; |
| 50 | + private MockAsyncHttpClient mockAsyncHttpClient; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setup() { |
| 54 | + mockHttpClient = new MockSyncHttpClient(); |
| 55 | + mockHttpClient.stubNextResponse(mockResponse()); |
| 56 | + |
| 57 | + mockAsyncHttpClient = new MockAsyncHttpClient(); |
| 58 | + mockAsyncHttpClient.stubNextResponse(mockResponse()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void when_sigv4aServiceIsUsed_correctMetricIsAdded() { |
| 63 | + Sigv4AauthClient client = Sigv4AauthClient.builder() |
| 64 | + .region(Region.US_WEST_2) |
| 65 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 66 | + .httpClient(mockHttpClient) |
| 67 | + .build(); |
| 68 | + |
| 69 | + client.simpleOperationWithNoEndpointParams(r -> r.stringMember("test")); |
| 70 | + |
| 71 | + String userAgent = getUserAgentFromLastRequest(); |
| 72 | + System.out.println("SigV4a service User-Agent: " + userAgent); |
| 73 | + assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.SIGV4A_SIGNING.value())); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void when_sigv4aServiceIsUsedAsync_correctMetricIsAdded() { |
| 78 | + Sigv4AauthAsyncClient asyncClient = Sigv4AauthAsyncClient.builder() |
| 79 | + .region(Region.US_WEST_2) |
| 80 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 81 | + .httpClient(mockAsyncHttpClient) |
| 82 | + .build(); |
| 83 | + |
| 84 | + asyncClient.simpleOperationWithNoEndpointParams(r -> r.stringMember("test")).join(); |
| 85 | + |
| 86 | + String userAgent = getUserAgentFromLastAsyncRequest(); |
| 87 | + System.out.println("SigV4a async service User-Agent: " + userAgent); |
| 88 | + assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.SIGV4A_SIGNING.value())); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void when_regularServiceIsUsed_sigv4aMetricIsNotAdded() { |
| 93 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 94 | + .region(Region.US_WEST_2) |
| 95 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 96 | + .httpClient(mockHttpClient) |
| 97 | + .build(); |
| 98 | + |
| 99 | + client.allTypes(r -> {}); |
| 100 | + |
| 101 | + String userAgent = getUserAgentFromLastRequest(); |
| 102 | + System.out.println("Regular service User-Agent: " + userAgent); |
| 103 | + assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.SIGV4A_SIGNING.value())); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void when_regularServiceIsUsedAsync_sigv4aMetricIsNotAdded() { |
| 108 | + ProtocolRestJsonAsyncClient asyncClient = ProtocolRestJsonAsyncClient.builder() |
| 109 | + .region(Region.US_WEST_2) |
| 110 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 111 | + .httpClient(mockAsyncHttpClient) |
| 112 | + .build(); |
| 113 | + |
| 114 | + asyncClient.allTypes(r -> {}).join(); |
| 115 | + |
| 116 | + String userAgent = getUserAgentFromLastAsyncRequest(); |
| 117 | + System.out.println("Regular async service User-Agent: " + userAgent); |
| 118 | + assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.SIGV4A_SIGNING.value())); |
| 119 | + } |
| 120 | + |
| 121 | + private String getUserAgentFromLastRequest() { |
| 122 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 123 | + assertThat(lastRequest).isNotNull(); |
| 124 | + |
| 125 | + List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); |
| 126 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 127 | + return userAgentHeaders.get(0); |
| 128 | + } |
| 129 | + |
| 130 | + private String getUserAgentFromLastAsyncRequest() { |
| 131 | + SdkHttpRequest lastRequest = mockAsyncHttpClient.getLastRequest(); |
| 132 | + assertThat(lastRequest).isNotNull(); |
| 133 | + |
| 134 | + List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); |
| 135 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 136 | + return userAgentHeaders.get(0); |
| 137 | + } |
| 138 | + |
| 139 | + private static HttpExecuteResponse mockResponse() { |
| 140 | + return HttpExecuteResponse.builder() |
| 141 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 142 | + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) |
| 143 | + .build(); |
| 144 | + } |
| 145 | +} |
0 commit comments