|
16 | 16 | package software.amazon.awssdk.protocol.tests; |
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | | -import static org.assertj.core.api.Assertions.assertThatThrownBy; |
20 | 19 | import static software.amazon.awssdk.core.useragent.BusinessMetricCollection.METRIC_SEARCH_PATTERN; |
21 | 20 |
|
22 | 21 | import java.util.List; |
23 | | -import java.util.Map; |
24 | 22 | import org.junit.jupiter.api.BeforeEach; |
25 | 23 | import org.junit.jupiter.api.Test; |
26 | 24 | import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
27 | 25 | import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
28 | | -import software.amazon.awssdk.core.interceptor.Context; |
29 | | -import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
30 | | -import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
31 | 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; |
32 | 31 | import software.amazon.awssdk.regions.Region; |
33 | | -import software.amazon.awssdk.services.protocolsmithyrpcv2.ProtocolSmithyrpcv2AsyncClient; |
34 | | -import software.amazon.awssdk.services.protocolsmithyrpcv2.ProtocolSmithyrpcv2AsyncClientBuilder; |
35 | | -import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; |
36 | | -import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClientBuilder; |
| 32 | +import software.amazon.awssdk.services.protocolsmithyrpcv2.ProtocolSmithyrpcv2Client; |
| 33 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; |
| 34 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 35 | +import software.amazon.awssdk.utils.StringInputStream; |
37 | 36 |
|
38 | 37 | class ProtocolRpcV2CborUserAgentTest { |
39 | | - private CapturingInterceptor interceptor; |
40 | | - |
41 | 38 | private static final String USER_AGENT_HEADER_NAME = "User-Agent"; |
42 | 39 | private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = |
43 | 40 | StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); |
44 | 41 |
|
| 42 | + private MockSyncHttpClient mockHttpClient; |
| 43 | + |
45 | 44 | @BeforeEach |
46 | 45 | public void setup() { |
47 | | - this.interceptor = new CapturingInterceptor(); |
| 46 | + mockHttpClient = new MockSyncHttpClient(); |
| 47 | + mockHttpClient.stubNextResponse(mockResponse()); |
48 | 48 | } |
49 | 49 |
|
50 | 50 | @Test |
51 | 51 | void when_rpcV2CborProtocolIsUsed_correctMetricIsAdded() { |
52 | | - ProtocolSmithyrpcv2AsyncClientBuilder clientBuilder = asyncClientBuilderForRpcV2Cbor(); |
| 52 | + ProtocolSmithyrpcv2Client client = ProtocolSmithyrpcv2Client.builder() |
| 53 | + .region(Region.US_WEST_2) |
| 54 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 55 | + .httpClient(mockHttpClient) |
| 56 | + .build(); |
53 | 57 |
|
54 | | - assertThatThrownBy(() -> clientBuilder.build().operationWithNoInputOrOutput(r -> {}).join()) |
55 | | - .hasMessageContaining("stop"); |
| 58 | + client.operationWithNoInputOrOutput(r -> {}); |
56 | 59 |
|
57 | | - String userAgent = assertAndGetUserAgentString(); |
| 60 | + String userAgent = getUserAgentFromLastRequest(); |
58 | 61 | assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.PROTOCOL_RPC_V2_CBOR.value())); |
59 | 62 | } |
60 | 63 |
|
61 | 64 | @Test |
62 | 65 | void when_nonRpcV2CborProtocolIsUsed_rpcV2CborMetricIsNotAdded() { |
63 | | - ProtocolRestJsonAsyncClientBuilder clientBuilder = asyncClientBuilderForRestJson(); |
| 66 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 67 | + .region(Region.US_WEST_2) |
| 68 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 69 | + .httpClient(mockHttpClient) |
| 70 | + .build(); |
64 | 71 |
|
65 | | - assertThatThrownBy(() -> clientBuilder.build().allTypes(r -> {}).join()) |
66 | | - .hasMessageContaining("stop"); |
| 72 | + client.allTypes(r -> {}); |
67 | 73 |
|
68 | | - String userAgent = assertAndGetUserAgentString(); |
| 74 | + String userAgent = getUserAgentFromLastRequest(); |
69 | 75 | assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.PROTOCOL_RPC_V2_CBOR.value())); |
70 | 76 | } |
71 | 77 |
|
72 | | - private String assertAndGetUserAgentString() { |
73 | | - Map<String, List<String>> headers = interceptor.context.httpRequest().headers(); |
74 | | - assertThat(headers).containsKey(USER_AGENT_HEADER_NAME); |
75 | | - return headers.get(USER_AGENT_HEADER_NAME).get(0); |
76 | | - } |
| 78 | + private String getUserAgentFromLastRequest() { |
| 79 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 80 | + assertThat(lastRequest).isNotNull(); |
77 | 81 |
|
78 | | - private ProtocolSmithyrpcv2AsyncClientBuilder asyncClientBuilderForRpcV2Cbor() { |
79 | | - return ProtocolSmithyrpcv2AsyncClient.builder() |
80 | | - .region(Region.US_WEST_2) |
81 | | - .credentialsProvider(CREDENTIALS_PROVIDER) |
82 | | - .overrideConfiguration(c -> c.addExecutionInterceptor(interceptor)); |
| 82 | + List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); |
| 83 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 84 | + return userAgentHeaders.get(0); |
83 | 85 | } |
84 | 86 |
|
85 | | - private ProtocolRestJsonAsyncClientBuilder asyncClientBuilderForRestJson() { |
86 | | - return ProtocolRestJsonAsyncClient.builder() |
87 | | - .region(Region.US_WEST_2) |
88 | | - .credentialsProvider(CREDENTIALS_PROVIDER) |
89 | | - .overrideConfiguration(c -> c.addExecutionInterceptor(interceptor)); |
90 | | - } |
91 | | - |
92 | | - public static class CapturingInterceptor implements ExecutionInterceptor { |
93 | | - private Context.BeforeTransmission context; |
94 | | - private ExecutionAttributes executionAttributes; |
95 | | - |
96 | | - @Override |
97 | | - public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttributes executionAttributes) { |
98 | | - this.context = context; |
99 | | - this.executionAttributes = executionAttributes; |
100 | | - throw new RuntimeException("stop"); |
101 | | - } |
102 | | - |
103 | | - public ExecutionAttributes executionAttributes() { |
104 | | - return executionAttributes; |
105 | | - } |
| 87 | + private static HttpExecuteResponse mockResponse() { |
| 88 | + return HttpExecuteResponse.builder() |
| 89 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 90 | + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) |
| 91 | + .build(); |
106 | 92 | } |
107 | 93 | } |
0 commit comments