Skip to content

Commit 4e047fd

Browse files
rolfbjarneCopilot
andcommitted
Add test for NoMissingCertificateHandling opt-out switch
Verifies that when the Foundation.NSUrlSessionHandler.NoMissingCertificateHandling switch is enabled, the specific SecureChannelFailure exception is not thrown. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 359782a commit 4e047fd

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/monotouch-test/System.Net.Http/MessageHandlers.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,36 @@ public void TestNSUrlSessionHandlerDetectMissingClientCertificate ()
766766
}
767767
}
768768

769+
[Test]
770+
public void TestNSUrlSessionHandlerDetectMissingClientCertificateOptOut ()
771+
{
772+
AppContext.TryGetSwitch ("Foundation.NSUrlSessionHandler.NoMissingCertificateHandling", out var originalValue);
773+
NWListener? listener = null;
774+
try {
775+
AppContext.SetSwitch ("Foundation.NSUrlSessionHandler.NoMissingCertificateHandling", true);
776+
listener = CreateNWTlsListener (requireClientCert: true);
777+
var port = listener.Port;
778+
779+
var done = TestRuntime.TryRunAsync (TimeSpan.FromSeconds (30), async () => {
780+
using var handler = new NSUrlSessionHandler ();
781+
handler.TrustOverrideForUrl = (sender, url, trust) => true;
782+
using var client = new HttpClient (handler);
783+
await client.GetAsync ($"https://localhost:{port}/");
784+
}, out var ex);
785+
Assert.IsTrue (done, "Request to localhost timed out.");
786+
// With the opt-out switch enabled, the new specific exception is not thrown.
787+
// Instead we get a generic connection error (no WebException/AuthenticationException chain).
788+
Assert.IsNotNull (ex, "Exception was expected.");
789+
Assert.IsInstanceOf (typeof (HttpRequestException), ex, "Exception");
790+
if (ex!.InnerException is WebException we)
791+
Assert.That (we.Status, Is.Not.EqualTo (WebExceptionStatus.SecureChannelFailure), "Should not be SecureChannelFailure");
792+
} finally {
793+
AppContext.SetSwitch ("Foundation.NSUrlSessionHandler.NoMissingCertificateHandling", originalValue);
794+
listener?.Cancel ();
795+
listener?.Dispose ();
796+
}
797+
}
798+
769799
static NWListener CreateNWTlsListener (bool requireClientCert)
770800
{
771801
using var serverCert = CreateSelfSignedServerCertificate ();

0 commit comments

Comments
 (0)