@@ -20,6 +20,7 @@ import aws.smithy.kotlin.runtime.http.request.url
2020import aws.smithy.kotlin.runtime.http.response.HttpResponse
2121import aws.smithy.kotlin.runtime.httptest.TestConnection
2222import aws.smithy.kotlin.runtime.httptest.buildTestConnection
23+ import aws.smithy.kotlin.runtime.retries.StandardRetryStrategyOptions
2324import aws.smithy.kotlin.runtime.time.Instant
2425import aws.smithy.kotlin.runtime.time.TimestampFormat
2526import io.kotest.matchers.string.shouldContain
@@ -57,16 +58,12 @@ class EcsCredentialsProviderTest {
5758 return HttpResponse (HttpStatusCode .OK , Headers .Empty , ByteArrayContent (payload))
5859 }
5960
60- private fun errorResponse (): HttpResponse {
61- val payload = """
62- {
63- "Code" : "TestError",
64- "LastUpdated" : "2021-09-17T20:57:08Z",
65- "Message": "Test error code response"
66- }
67- """ .encodeToByteArray()
68- return HttpResponse (HttpStatusCode .BadRequest , Headers .Empty , ByteArrayContent (payload))
69- }
61+ private fun errorResponse (
62+ statusCode : HttpStatusCode = HttpStatusCode .BadRequest ,
63+ headers : Headers = Headers .Empty ,
64+ body : String = "",
65+ ): HttpResponse =
66+ HttpResponse (statusCode, headers, ByteArrayContent (body.encodeToByteArray()))
7067
7168 private fun ecsRequest (url : String , authToken : String? = null): HttpRequest {
7269 val resolvedUrl = Url .parse(url)
@@ -207,7 +204,117 @@ class EcsCredentialsProviderTest {
207204 val provider = EcsCredentialsProvider (testPlatform, engine)
208205 assertFailsWith<CredentialsProviderException > {
209206 provider.getCredentials()
210- }.message.shouldContain(" Error retrieving credentials from container service: code=TestError; message=Test error code response" )
207+ }.message.shouldContain(" Error retrieving credentials from container service: HTTP 400: Bad Request" )
208+
209+ engine.assertRequests()
210+ }
211+
212+ @Test
213+ fun testThrottledErrorResponse () = runTest {
214+ val engine = buildTestConnection {
215+ repeat(StandardRetryStrategyOptions .Default .maxAttempts) {
216+ expect(
217+ ecsRequest(" http://169.254.170.2/relative" ),
218+ errorResponse(statusCode = HttpStatusCode .TooManyRequests ),
219+ )
220+ }
221+ }
222+
223+ val testPlatform = TestPlatformProvider (
224+ env = mapOf (AwsSdkSetting .AwsContainerCredentialsRelativeUri .environmentVariable to " /relative" ),
225+ )
226+
227+ val provider = EcsCredentialsProvider (testPlatform, engine)
228+ assertFailsWith<CredentialsProviderException > {
229+ provider.getCredentials()
230+ }.message.shouldContain(" Error retrieving credentials from container service: HTTP 429: Too Many Requests" )
231+
232+ engine.assertRequests()
233+ }
234+
235+ @Test
236+ fun testJsonErrorResponse () = runTest {
237+ val engine = buildTestConnection {
238+ expect(
239+ ecsRequest(" http://169.254.170.2/relative" ),
240+ errorResponse(
241+ HttpStatusCode .BadRequest ,
242+ Headers { append(" Content-Type" , " application/json" ) },
243+ """
244+ {
245+ "Code": "failed",
246+ "Message": "request was malformed"
247+ }
248+ """ ,
249+ ),
250+ )
251+ }
252+
253+ val testPlatform = TestPlatformProvider (
254+ env = mapOf (AwsSdkSetting .AwsContainerCredentialsRelativeUri .environmentVariable to " /relative" ),
255+ )
256+
257+ val provider = EcsCredentialsProvider (testPlatform, engine)
258+ assertFailsWith<CredentialsProviderException > {
259+ provider.getCredentials()
260+ }.message.shouldContain(" Error retrieving credentials from container service: code=failed; message=request was malformed" )
261+
262+ engine.assertRequests()
263+ }
264+
265+ @Test
266+ fun testThrottledJsonErrorResponse () = runTest {
267+ val engine = buildTestConnection {
268+ repeat(StandardRetryStrategyOptions .Default .maxAttempts) {
269+ expect(
270+ ecsRequest(" http://169.254.170.2/relative" ),
271+ errorResponse(
272+ HttpStatusCode .TooManyRequests ,
273+ Headers { append(" Content-Type" , " application/json" ) },
274+ """
275+ {
276+ "Code": "failed",
277+ "Message": "too many requests"
278+ }
279+ """ ,
280+ ),
281+ )
282+ }
283+ }
284+
285+ val testPlatform = TestPlatformProvider (
286+ env = mapOf (AwsSdkSetting .AwsContainerCredentialsRelativeUri .environmentVariable to " /relative" ),
287+ )
288+
289+ val provider = EcsCredentialsProvider (testPlatform, engine)
290+ assertFailsWith<CredentialsProviderException > {
291+ provider.getCredentials()
292+ }.message.shouldContain(" Error retrieving credentials from container service: code=failed; message=too many requests" )
293+
294+ engine.assertRequests()
295+ }
296+
297+ @Test
298+ fun test5XXErrorResponse () = runTest {
299+ val engine = buildTestConnection {
300+ repeat(StandardRetryStrategyOptions .Default .maxAttempts) {
301+ expect(
302+ ecsRequest(" http://169.254.170.2/relative" ),
303+ errorResponse(
304+ HttpStatusCode .BadGateway ,
305+ ),
306+ )
307+ }
308+ }
309+
310+ val testPlatform = TestPlatformProvider (
311+ env = mapOf (AwsSdkSetting .AwsContainerCredentialsRelativeUri .environmentVariable to " /relative" ),
312+ )
313+
314+ val provider = EcsCredentialsProvider (testPlatform, engine)
315+ assertFailsWith<CredentialsProviderException > {
316+ provider.getCredentials()
317+ }.message.shouldContain(" Error retrieving credentials from container service: HTTP 502: Bad Gateway" )
211318
212319 engine.assertRequests()
213320 }
0 commit comments