@@ -20,6 +20,7 @@ import aws.smithy.kotlin.runtime.http.response.HttpCall
2020import aws.smithy.kotlin.runtime.http.response.HttpResponse
2121import aws.smithy.kotlin.runtime.http.sdkHttpClient
2222import aws.smithy.kotlin.runtime.time.Instant
23+ import aws.smithy.kotlin.runtime.util.PlatformProvider
2324import aws.smithy.kotlin.runtime.util.get
2425import io.kotest.matchers.string.shouldContain
2526import io.kotest.matchers.string.shouldNotContain
@@ -41,20 +42,23 @@ class UserAgentTest {
4142
4243 private val client = sdkHttpClient(mockEngine)
4344
44- @Test
45- fun itSetsUAHeaders () = runTest {
46- val op = SdkHttpOperation .build<Unit , HttpResponse > {
45+ private fun initializeOp (platformProvider : PlatformProvider = TestPlatformProvider ()) =
46+ SdkHttpOperation .build<Unit , HttpResponse > {
4747 serializer = UnitSerializer
4848 deserializer = IdentityDeserializer
4949 context {
5050 service = " Test Service"
5151 operationName = " testOperation"
5252 }
53+ }.apply {
54+ val apiMd = ApiMetadata (" Test Service" , " 1.2.3" )
55+ val metadata = loadAwsUserAgentMetadataFromEnvironment(platformProvider, apiMd)
56+ install(UserAgent (metadata))
5357 }
5458
55- val provider = TestPlatformProvider ()
56- val metadata = loadAwsUserAgentMetadataFromEnvironment(provider, ApiMetadata ( " Test Service " , " 1.2.3 " ))
57- op.install( UserAgent (metadata) )
59+ @Test
60+ fun itSetsUAHeaders () = runTest {
61+ val op = initializeOp( )
5862
5963 op.roundTrip(client, Unit )
6064 val request = op.context[HttpOperationContext .HttpCallList ].last().request
@@ -68,19 +72,7 @@ class UserAgentTest {
6872
6973 @Test
7074 fun itAddsPerOperationMetadata () = runTest {
71- val op = SdkHttpOperation .build<Unit , HttpResponse > {
72- serializer = UnitSerializer
73- deserializer = IdentityDeserializer
74- context {
75- service = " Test Service"
76- operationName = " testOperation"
77- }
78- }
79-
80- val provider = TestPlatformProvider ()
81- val staticMeta = loadAwsUserAgentMetadataFromEnvironment(provider, ApiMetadata (" Test Service" , " 1.2.3" ))
82- op.install(UserAgent (staticMeta))
83-
75+ val op = initializeOp()
8476 op.context.customUserAgentMetadata.add(" foo" , " bar" )
8577
8678 op.roundTrip(client, Unit )
@@ -89,17 +81,7 @@ class UserAgentTest {
8981 request.headers[USER_AGENT ]!! .shouldContain(" md/foo/bar" )
9082
9183 // verify per/request metadata is actually per/request
92- val op2 = SdkHttpOperation .build<Unit , HttpResponse > {
93- serializer = UnitSerializer
94- deserializer = IdentityDeserializer
95- context {
96- service = " Test Service"
97- operationName = " testOperation2"
98- }
99- }
100-
101- op2.install(UserAgent (staticMeta))
102-
84+ val op2 = initializeOp()
10385 op2.context.customUserAgentMetadata.add(" baz" , " quux" )
10486
10587 op2.roundTrip(client, Unit )
@@ -108,4 +90,48 @@ class UserAgentTest {
10890 request2.headers[USER_AGENT ]!! .shouldNotContain(" md/foo/bar" )
10991 request2.headers[USER_AGENT ]!! .shouldContain(" md/baz/quux" )
11092 }
93+
94+ @Test
95+ fun itMergesCustomMetadataWithExisting () = runTest {
96+ // see: https://github.com/awslabs/aws-sdk-kotlin/issues/694
97+ val platform = TestPlatformProvider (
98+ props = mapOf (
99+ " aws.customMetadata.foo" to " bar" ,
100+ " aws.customMetadata.baz" to " qux" ,
101+ ),
102+ )
103+ val op = initializeOp(platform)
104+ op.context.customUserAgentMetadata.apply {
105+ add(" baz" , " quux" )
106+ add(" blerg" , " blarg" )
107+ }
108+
109+ op.roundTrip(client, Unit )
110+ val request = op.context[HttpOperationContext .HttpCallList ].last().request
111+ val uaString = request.headers[USER_AGENT ]!!
112+
113+ uaString.shouldContain(" md/foo/bar" )
114+ uaString.shouldContain(" md/baz/quux" )
115+ uaString.shouldContain(" md/blerg/blarg" )
116+ uaString.shouldNotContain(" md/baz/qux" ) // This was overwritten by "baz/quux"
117+ }
118+
119+ @Test
120+ fun itDoesNotClobberExistingCustomMetadata () = runTest {
121+ // see: https://github.com/awslabs/aws-sdk-kotlin/issues/694
122+ val platform = TestPlatformProvider (
123+ props = mapOf (
124+ " aws.customMetadata.foo" to " bar" ,
125+ " aws.customMetadata.baz" to " qux" ,
126+ ),
127+ )
128+ val op = initializeOp(platform)
129+
130+ op.roundTrip(client, Unit )
131+ val request = op.context[HttpOperationContext .HttpCallList ].last().request
132+ val uaString = request.headers[USER_AGENT ]!!
133+
134+ uaString.shouldContain(" md/foo/bar" )
135+ uaString.shouldContain(" md/baz/qux" )
136+ }
111137}
0 commit comments