-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
2,939 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2,744 changes: 2,626 additions & 118 deletions
2,744
src/Microsoft.Identity.Web/Microsoft.Identity.Web.xml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Microsoft.Identity.Web.Test.Common/Mocks/MockHttpClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Identity.Client; | ||
using Xunit; | ||
|
||
namespace Microsoft.Identity.Web.Test.Common.Mocks | ||
{ | ||
public class MockHttpClientFactory : IMsalHttpClientFactory, IDisposable | ||
{ | ||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
// This ensures we only check the mock queue on dispose when we're not in the middle of an | ||
// exception flow. Otherwise, any early assertion will cause this to likely fail | ||
// even though it's not the root cause. | ||
#pragma warning disable CS0618 // Type or member is obsolete - this is non-production code so it's fine | ||
if (Marshal.GetExceptionCode() == 0) | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
{ | ||
string remainingMocks = string.Join( | ||
" ", | ||
_httpMessageHandlerQueue.Select( | ||
h => (h as MockHttpMessageHandler)?.ExpectedUrl ?? string.Empty)); | ||
|
||
Assert.Empty(_httpMessageHandlerQueue); | ||
} | ||
} | ||
|
||
public MockHttpMessageHandler AddMockHandler(MockHttpMessageHandler handler) | ||
{ | ||
_httpMessageHandlerQueue.Enqueue(handler); | ||
return handler; | ||
} | ||
|
||
private Queue<HttpMessageHandler> _httpMessageHandlerQueue = new Queue<HttpMessageHandler>(); | ||
|
||
public HttpClient GetHttpClient() | ||
{ | ||
HttpMessageHandler messageHandler; | ||
|
||
Assert.NotEmpty(_httpMessageHandlerQueue); | ||
messageHandler = _httpMessageHandlerQueue.Dequeue(); | ||
|
||
var httpClient = new HttpClient(messageHandler); | ||
|
||
httpClient.DefaultRequestHeaders.Accept.Clear(); | ||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
|
||
return httpClient; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
tests/Microsoft.Identity.Web.Test.Common/Mocks/MockHttpCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Net; | ||
using System.Net.Http; | ||
using Microsoft.Identity.Web.Test.Common; | ||
using Microsoft.Identity.Web.Util; | ||
|
||
namespace Microsoft.Identity.Web.Test.Common.Mocks | ||
{ | ||
public static class MockHttpCreator | ||
{ | ||
private static HttpResponseMessage CreateSuccessfulClientCredentialTokenResponseMessage(string token = "header.payload.signature", string expiry = "3599") | ||
{ | ||
return CreateSuccessResponseMessage( | ||
"{\"token_type\":\"Bearer\",\"expires_in\":\"" + expiry + "\",\"client_info\":\"" + CreateClientInfo() + "\",\"access_token\":\"" + token + "\"}"); | ||
} | ||
|
||
public static HttpResponseMessage CreateSuccessResponseMessage(string successResponse) | ||
{ | ||
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK); | ||
HttpContent content = | ||
new StringContent(successResponse); | ||
responseMessage.Content = content; | ||
return responseMessage; | ||
} | ||
|
||
private static string CreateClientInfo(string uid = TestConstants.Uid, string utid = TestConstants.Utid) | ||
{ | ||
return Base64UrlHelpers.Encode("{\"uid\":\"" + uid + "\",\"utid\":\"" + utid + "\"}"); | ||
} | ||
|
||
public static MockHttpMessageHandler CreateInstanceDiscoveryMockHandler( | ||
string discoveryEndpoint = "https://login.microsoftonline.com/common/discovery/instance", | ||
string content = TestConstants.DiscoveryJsonResponse) | ||
{ | ||
return new MockHttpMessageHandler() | ||
{ | ||
ExpectedUrl = discoveryEndpoint, | ||
ExpectedMethod = HttpMethod.Get, | ||
ResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = new StringContent(content), | ||
}, | ||
}; | ||
} | ||
|
||
public static MockHttpMessageHandler CreateClientCredentialTokenHandler( | ||
string token = "header.payload.signature", string expiresIn = "3599") | ||
{ | ||
var handler = new MockHttpMessageHandler() | ||
{ | ||
ExpectedMethod = HttpMethod.Post, | ||
ResponseMessage = CreateSuccessfulClientCredentialTokenResponseMessage(token, expiresIn), | ||
}; | ||
|
||
return handler; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/Microsoft.Identity.Web.Test.Common/Mocks/MockHttpMessageHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Identity.Web.Test.Common.Mocks | ||
{ | ||
public class MockHttpMessageHandler : HttpMessageHandler | ||
{ | ||
public HttpResponseMessage ResponseMessage { get; set; } | ||
public string ExpectedUrl { get; set; } | ||
|
||
public HttpMethod ExpectedMethod { get; set; } | ||
|
||
public Exception ExceptionToThrow { get; set; } | ||
|
||
/// <summary> | ||
/// Once the http message is executed, this property holds the request message | ||
/// </summary> | ||
public HttpRequestMessage ActualRequestMessage { get; private set; } | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
ActualRequestMessage = request; | ||
|
||
if (ExceptionToThrow != null) | ||
{ | ||
throw ExceptionToThrow; | ||
} | ||
|
||
var uri = request.RequestUri; | ||
if (!string.IsNullOrEmpty(ExpectedUrl)) | ||
{ | ||
Assert.Equal( | ||
ExpectedUrl, | ||
uri.AbsoluteUri.Split( | ||
new[] | ||
{ | ||
'?', | ||
})[0]); | ||
} | ||
|
||
Assert.Equal(ExpectedMethod, request.Method); | ||
|
||
if (request.Method != HttpMethod.Get && request.Content != null) | ||
{ | ||
string postData = request.Content.ReadAsStringAsync().Result; | ||
} | ||
|
||
return new TaskFactory().StartNew(() => ResponseMessage, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.