-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathHttpClientFactoryTests.cs
More file actions
104 lines (85 loc) · 3.63 KB
/
HttpClientFactoryTests.cs
File metadata and controls
104 lines (85 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Identity.Client.PlatformsCommon.Shared;
using Microsoft.Identity.Test.Common;
using Microsoft.Identity.Test.Common.Core.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.Identity.Test.Unit.CoreTests.HttpTests
{
[TestClass]
public class HttpClientFactoryTests : TestBase
{
[TestMethod]
public void TestGetHttpClientWithCustomCallback()
{
// Arrange
var factory = new SimpleHttpClientFactory();
// Act
HttpClient client = factory.GetHttpClient((sender, cert, chain, errors) => true);
// Assert
Assert.IsNotNull(client);
}
[TestMethod]
public void TestGetHttpClientWithNoCallback()
{
// Arrange
var factory = new SimpleHttpClientFactory();
// Act
HttpClient client = factory.GetHttpClient();
// Assert
Assert.IsNotNull(client);
}
[TestMethod]
public void TestHttpClientIsNotCached()
{
// Arrange
var factory = new SimpleHttpClientFactory();
Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> customCallback = (sender, cert, chain, errors) => true;
// Act
HttpClient client1 = factory.GetHttpClient(customCallback);
HttpClient client2 = factory.GetHttpClient(customCallback);
// Assert
Assert.IsNotNull(client1);
Assert.IsNotNull(client2);
Assert.AreNotSame(client1, client2); // A new instance should be created each time to ensure callback is applied
}
[TestMethod]
public void TestHttpClientWithMtlsCertificateAndCustomHandler()
{
// Arrange
var factory = new SimpleHttpClientFactory();
var cert = CertHelper.GetOrCreateTestCert();
var customHandler = new HttpClientHandler();
// Act
HttpClient mtlsClient = factory.GetHttpClient(cert);
HttpClient handlerClient = factory.GetHttpClient((sender, cert, chain, errors) => true);
// Assert
Assert.IsNotNull(mtlsClient);
Assert.IsNotNull(handlerClient);
Assert.AreNotSame(mtlsClient, handlerClient); // Should be different instances
}
[TestMethod]
public void TestGetHttpClient_DoesNotLeakHttpClients()
{
// Arrange - reset static state so we start from a clean pool
SimpleHttpClientFactory.ResetStaticStateForTest();
var factory = new SimpleHttpClientFactory();
// Act - call GetHttpClient multiple times with the same (default) key
factory.GetHttpClient();
factory.GetHttpClient();
factory.GetHttpClient();
int created = SimpleHttpClientFactory.HttpClientCreationCount;
// Assert - CreateHttpClient should be called exactly once.
// Before the fix, GetOrAdd(key, CreateHttpClient()) eagerly evaluates
// CreateHttpClient() on every call, causing unnecessary throwaway
// HttpClient/HttpClientHandler allocations.
Assert.AreEqual(1, created,
$"CreateHttpClient was called {created} times for 3 lookups. " +
"Use GetOrAdd(key, factory_delegate) to avoid creating throwaway HttpClient instances.");
}
}
}