Skip to content

Commit c003837

Browse files
authored
WinHttpHandler tests: AllowAllCertificates should not be static (#48817)
Altering the global state can break prerequisites of unrelated tests.
1 parent 4b143df commit c003837

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.ServerCertificates.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public abstract partial class HttpClientHandler_ServerCertificates_Test : HttpCl
3030

3131
public HttpClientHandler_ServerCertificates_Test(ITestOutputHelper output) : base(output) { }
3232

33+
// This enables customizing ServerCertificateCustomValidationCallback in WinHttpHandler variants:
34+
protected bool AllowAllHttp2Certificates { get; set; } = true;
35+
protected new HttpClientHandler CreateHttpClientHandler() => CreateHttpClientHandler(UseVersion, allowAllHttp2Certificates: AllowAllHttp2Certificates);
36+
3337
[Fact]
3438
public void Ctor_ExpectedDefaultValues()
3539
{
@@ -386,15 +390,19 @@ public void HttpClientUsesSslCertEnvironmentVariables()
386390
File.WriteAllText(sslCertFile, "");
387391
psi.Environment.Add("SSL_CERT_FILE", sslCertFile);
388392

389-
RemoteExecutor.Invoke(async (useVersionString) =>
393+
RemoteExecutor.Invoke(async (useVersionString, allowAllHttp2CertificatesString) =>
390394
{
391395
const string Url = "https://www.microsoft.com";
392396

393-
using (HttpClient client = CreateHttpClient(useVersionString))
397+
HttpClientHandler handler = CreateHttpClientHandler(
398+
Version.Parse(useVersionString),
399+
allowAllHttp2Certificates: bool.Parse(allowAllHttp2CertificatesString));
400+
401+
using (HttpClient client = CreateHttpClient(handler, useVersionString))
394402
{
395403
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(Url));
396404
}
397-
}, UseVersion.ToString(), new RemoteInvokeOptions { StartInfo = psi }).Dispose();
405+
}, UseVersion.ToString(), AllowAllHttp2Certificates.ToString(), new RemoteInvokeOptions { StartInfo = psi }).Dispose();
398406
}
399407
}
400408
}

src/libraries/System.Net.Http.WinHttpHandler/tests/FunctionalTests/HttpClientHandlerTestBase.WinHttpHandler.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ public abstract partial class HttpClientHandlerTestBase : FileCleanupTestBase
1010
{
1111
protected static bool IsWinHttpHandler => true;
1212

13-
protected static bool AllowAllCertificates { get; set; } = true;
14-
15-
protected static WinHttpClientHandler CreateHttpClientHandler(Version useVersion = null)
13+
protected static WinHttpClientHandler CreateHttpClientHandler(Version useVersion = null, bool allowAllHttp2Certificates = true)
1614
{
1715
useVersion ??= HttpVersion.Version11;
1816

1917
WinHttpClientHandler handler = new WinHttpClientHandler(useVersion);
2018

21-
if (useVersion >= HttpVersion20.Value && AllowAllCertificates)
19+
if (useVersion >= HttpVersion20.Value && allowAllHttp2Certificates)
2220
{
2321
handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;
2422
}

src/libraries/System.Net.Http.WinHttpHandler/tests/FunctionalTests/PlatformHandlerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public sealed class PlatformHandler_HttpClientHandler_ServerCertificates_Http2_T
277277
protected override Version UseVersion => HttpVersion20.Value;
278278

279279
public PlatformHandler_HttpClientHandler_ServerCertificates_Http2_Test(ITestOutputHelper output) : base(output) {
280-
AllowAllCertificates = false;
280+
AllowAllHttp2Certificates = false;
281281
}
282282
}
283283

src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTestBase.SocketsHttpHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public abstract partial class HttpClientHandlerTestBase : FileCleanupTestBase
1818

1919
public static bool IsMsQuicSupported => QuicImplementationProviders.MsQuic.IsSupported;
2020

21-
protected static HttpClientHandler CreateHttpClientHandler(Version useVersion = null, QuicImplementationProvider quicImplementationProvider = null)
21+
protected static HttpClientHandler CreateHttpClientHandler(Version useVersion = null, QuicImplementationProvider quicImplementationProvider = null, bool allowAllHttp2Certificates = true)
2222
{
2323
useVersion ??= HttpVersion.Version11;
2424

2525
HttpClientHandler handler = (PlatformDetection.SupportsAlpn && useVersion != HttpVersion.Version30) ? new HttpClientHandler() : new VersionHttpClientHandler(useVersion);
2626

27-
if (useVersion >= HttpVersion.Version20)
27+
if (useVersion >= HttpVersion.Version20 && allowAllHttp2Certificates)
2828
{
2929
handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;
3030
}

0 commit comments

Comments
 (0)