|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.IO; |
7 | | -using System.Net.Http; |
8 | 7 | using System.Text; |
9 | 8 | using System.Threading; |
10 | 9 | using System.Threading.Tasks; |
@@ -459,6 +458,58 @@ public async Task VerifyClientAuthenticateThrows() |
459 | 458 | await Task.CompletedTask; |
460 | 459 | } |
461 | 460 |
|
| 461 | + [Test] |
| 462 | + public async Task VerifyClientAuthenticateReturnsInvalidJson([Values(200, 404)] int status) |
| 463 | + { |
| 464 | + using var environment = new TestEnvVar( |
| 465 | + new() |
| 466 | + { |
| 467 | + { "MSI_ENDPOINT", null }, |
| 468 | + { "MSI_SECRET", null }, |
| 469 | + { "IDENTITY_ENDPOINT", null }, |
| 470 | + { "IDENTITY_HEADER", null }, |
| 471 | + { "AZURE_POD_IDENTITY_AUTHORITY_HOST", null } |
| 472 | + }); |
| 473 | + var mockTransport = new MockTransport(request => CreateInvalidJsonResponse(status)); |
| 474 | + var options = new TokenCredentialOptions() { Transport = mockTransport }; |
| 475 | + options.Retry.MaxDelay = TimeSpan.Zero; |
| 476 | + var pipeline = CredentialPipeline.GetInstance(options); |
| 477 | + |
| 478 | + ManagedIdentityCredential credential = InstrumentClient(new ManagedIdentityCredential("mock-client-id", pipeline)); |
| 479 | + |
| 480 | + var ex = Assert.ThrowsAsync<AuthenticationFailedException>(async () => await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default))); |
| 481 | + Assert.IsInstanceOf(typeof(RequestFailedException), ex.InnerException); |
| 482 | + Assert.That(ex.Message, Does.Contain(ManagedIdentitySource.UnexpectedResponse)); |
| 483 | + await Task.CompletedTask; |
| 484 | + } |
| 485 | + |
| 486 | + [Test] |
| 487 | + public async Task VerifyClientAuthenticateReturnsErrorResponse() |
| 488 | + { |
| 489 | + using var environment = new TestEnvVar( |
| 490 | + new() |
| 491 | + { |
| 492 | + { "MSI_ENDPOINT", null }, |
| 493 | + { "MSI_SECRET", null }, |
| 494 | + { "IDENTITY_ENDPOINT", null }, |
| 495 | + { "IDENTITY_HEADER", null }, |
| 496 | + { "AZURE_POD_IDENTITY_AUTHORITY_HOST", null } |
| 497 | + }); |
| 498 | + var errorMessage = "Some error happened"; |
| 499 | + var mockTransport = new MockTransport(request => CreateErrorMockResponse(404, errorMessage)); |
| 500 | + var options = new TokenCredentialOptions { Transport = mockTransport}; |
| 501 | + options.Retry.MaxDelay = TimeSpan.Zero; |
| 502 | + var pipeline = CredentialPipeline.GetInstance(options); |
| 503 | + |
| 504 | + ManagedIdentityCredential credential = InstrumentClient(new ManagedIdentityCredential("mock-client-id", pipeline)); |
| 505 | + |
| 506 | + var ex = Assert.ThrowsAsync<AuthenticationFailedException>(async () => await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default))); |
| 507 | + Assert.IsInstanceOf(typeof(RequestFailedException), ex.InnerException); |
| 508 | + Assert.That(ex.Message, Does.Contain(errorMessage)); |
| 509 | + |
| 510 | + await Task.CompletedTask; |
| 511 | + } |
| 512 | + |
462 | 513 | [Test] |
463 | 514 | [TestCaseSource("ExceptionalEnvironmentConfigs")] |
464 | 515 | public async Task VerifyAuthenticationFailedExceptionsAreDeferredToGetToken(Dictionary<string, string> environmentVariables) |
@@ -503,5 +554,12 @@ private MockResponse CreateErrorMockResponse(int responseCode, string message) |
503 | 554 | response.SetContent($"{{\"StatusCode\":400,\"Message\":\"{message}\",\"CorrelationId\":\"f3c9aec0-7fa2-4184-ad0f-0c68ce5fc748\"}}"); |
504 | 555 | return response; |
505 | 556 | } |
| 557 | + |
| 558 | + private static MockResponse CreateInvalidJsonResponse(int status) |
| 559 | + { |
| 560 | + var response = new MockResponse(status); |
| 561 | + response.SetContent("invalid json"); |
| 562 | + return response; |
| 563 | + } |
506 | 564 | } |
507 | 565 | } |
0 commit comments