Skip to content

Commit

Permalink
Adding Uri to exception when error is 500 (#4820)
Browse files Browse the repository at this point in the history
* Adding Uri to exception when error is 500

* Adding test for AAD

* Fixing issue

---------

Co-authored-by: trwalke <[email protected]>
  • Loading branch information
trwalke and trwalke authored Jul 17, 2024
1 parent 9049fe9 commit c1a99c4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/client/Microsoft.Identity.Client/Http/HttpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ public async Task<HttpResponse> SendRequestAsync(
// package 500 errors in a "service not available" exception
if ((int)response.StatusCode >= 500 && (int)response.StatusCode < 600)
{
string requestUriScrubbed = $"{endpoint.AbsoluteUri.Split('?')[0]}";
throw MsalServiceExceptionFactory.FromHttpResponse(
MsalError.ServiceNotAvailable,
"Service is unavailable to process the request",
$"Service is unavailable to process the request. The request Uri is: {requestUriScrubbed} on port {endpoint.Port}",
response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using Microsoft.Identity.Client.Extensibility;
using System.Net;

namespace Microsoft.Identity.Test.Unit.PublicApiTests
{
Expand Down Expand Up @@ -423,6 +424,61 @@ public async Task ConfidentialClientUsingAdfsAsync()
}
}

[TestMethod]
public async Task ClientCreds_And_AAD_LogRequestUri_OnServerError_Async()
{
using (var httpManager = new MockHttpManager())
{
var cca = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId)
.WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true)
.WithClientSecret(TestConstants.ClientSecret)
.WithHttpManager(httpManager)
.WithExtraQueryParameters("parameter=x")
.BuildConcrete();
var appCacheAccess = cca.AppTokenCache.RecordAccess();
var userCacheAccess = cca.UserTokenCache.RecordAccess();

httpManager.AddInstanceDiscoveryMockHandler();
httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.InternalServerError, retryAfter: 0);

// Acquire Token
var ex = await AssertException.TaskThrowsAsync<MsalServiceException>(
() => cca.AcquireTokenForClient(TestConstants.s_scope.ToArray()).ExecuteAsync())
.ConfigureAwait(false);

//Assert
Assert.AreEqual(MsalError.ServiceNotAvailable, ex.ErrorCode);
Assert.IsTrue(ex.Message.Contains(ClientApplicationBase.DefaultAuthority + "oauth2/v2.0/token"));
}
}

[TestMethod]
public async Task ClientCreds_And_ADFS_LogRequestUri_OnServerError_Async()
{
using (var httpManager = new MockHttpManager())
{
var cca = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId)
.WithAuthority(new Uri(TestConstants.OnPremiseAuthority), false)
.WithClientSecret(TestConstants.ClientSecret)
.WithHttpManager(httpManager)
.WithExtraQueryParameters("parameter=x")
.BuildConcrete();
var appCacheAccess = cca.AppTokenCache.RecordAccess();
var userCacheAccess = cca.UserTokenCache.RecordAccess();

httpManager.AddResiliencyMessageMockHandler(HttpMethod.Post, HttpStatusCode.InternalServerError, retryAfter: 0);

// Acquire Token
var ex = await AssertException.TaskThrowsAsync<MsalServiceException>(
() => cca.AcquireTokenForClient(TestConstants.s_scope.ToArray()).ExecuteAsync())
.ConfigureAwait(false);

//Assert
Assert.AreEqual(MsalError.ServiceNotAvailable, ex.ErrorCode);
Assert.IsTrue(ex.Message.Contains(TestConstants.OnPremiseAuthority + "oauth2/token"));
}
}

private enum CredentialType
{
Certificate,
Expand Down

0 comments on commit c1a99c4

Please sign in to comment.