public void CreateResponseAuth() { CFHTTPMessage response = null; var done = false; var taskCompletionSource = new TaskCompletionSource <CFHTTPMessage> (); // the following code has to be in a diff thread, else, we are blocking the current loop, not cool // perform a request so that we fail in the auth, then create the auth object and check the count TestRuntime.RunAsync(DateTime.Now.AddSeconds(30), async() => { using (var request = CFHTTPMessage.CreateRequest( new Uri(NetworkResources.Httpbin.GetStatusCodeUrl(HttpStatusCode.Unauthorized)), "GET", null)) { request.SetBody(Array.Empty <byte> ()); // empty body, we are not interested using (var stream = CFStream.CreateForHTTPRequest(request)) { Assert.IsNotNull(stream, "Null stream"); // we are only interested in the completed event stream.ClosedEvent += (sender, e) => { taskCompletionSource.SetResult(stream.GetResponseHeader()); done = true; }; // enable events and run in the current loop stream.EnableEvents(CFRunLoop.Main, CFRunLoop.ModeDefault); stream.Open(); response = await taskCompletionSource.Task; } } }, () => done); using (var auth = CFHTTPAuthentication.CreateFromResponse(response)) { Assert.NotNull(auth, "Null Auth"); Assert.IsTrue(auth.IsValid, "Auth is valid"); Assert.That(TestRuntime.CFGetRetainCount(auth.Handle), Is.EqualTo((nint)1), "RetainCount"); } }
void FindAuthenticationObject(CFHTTPMessage response) { if (auth != null) { if (auth.IsValid) { return; } auth.Dispose(); auth = null; } if (auth == null) { auth = CFHTTPAuthentication.CreateFromResponse(response); if (auth == null) { throw new HttpRequestException("Failed to create CFHTTPAuthentication"); } } if (!auth.IsValid) { throw new HttpRequestException("Failed to validate CFHTTPAuthentication"); } }