diff --git a/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs b/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs index b990aa50e4f..71d972d0a87 100644 --- a/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs +++ b/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs @@ -425,6 +425,7 @@ string EncodeUrl (Uri url) if (redirectState.NewUrl == null) throw new InvalidOperationException ("Request redirected but no new URI specified"); request.Method = redirectState.Method; + request.RequestUri = redirectState.NewUrl; } catch (Java.Net.SocketTimeoutException ex) when (JNIEnv.ShouldWrapJavaException (ex)) { throw new WebException (ex.Message, ex, WebExceptionStatus.Timeout, null); } catch (Java.Net.UnknownServiceException ex) when (JNIEnv.ShouldWrapJavaException (ex)) { diff --git a/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs b/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs index 8d748c47bae..2f1517d57c6 100644 --- a/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs +++ b/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs @@ -27,9 +27,9 @@ public async Task ServerCertificateCustomValidationCallback_ApproveRequest () var handler = new AndroidMessageHandler { ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => { Assert.NotNull (request, "request"); - Assert.AreEqual ("microsoft.com", request.RequestUri.Host); + Assert.AreEqual ("www.microsoft.com", request.RequestUri.Host); Assert.NotNull (cert, "cert"); - Assert.True (cert!.Subject.Contains ("microsoft.com"), $"Unexpected certificate subject {cert!.Subject}"); + Assert.True (cert!.Subject.Contains ("www.microsoft.com"), $"Unexpected certificate subject {cert!.Subject}"); Assert.True (cert!.Issuer.Contains ("Microsoft"), $"Unexpected certificate issuer {cert!.Issuer}"); Assert.NotNull (chain, "chain"); Assert.AreEqual (SslPolicyErrors.None, errors); @@ -40,7 +40,7 @@ public async Task ServerCertificateCustomValidationCallback_ApproveRequest () }; var client = new HttpClient (handler); - await client.GetStringAsync ("https://microsoft.com/"); + await client.GetStringAsync ("https://www.microsoft.com/"); Assert.IsTrue (callbackHasBeenCalled, "custom validation callback hasn't been called"); } @@ -58,7 +58,7 @@ public async Task ServerCertificateCustomValidationCallback_RejectRequest () }; var client = new HttpClient (handler); - await AssertRejectsRemoteCertificate (() => client.GetStringAsync ("https://microsoft.com/")); + await AssertRejectsRemoteCertificate (() => client.GetStringAsync ("https://www.microsoft.com/")); Assert.IsTrue (callbackHasBeenCalled, "custom validation callback hasn't been called"); } @@ -111,6 +111,25 @@ public async Task ServerCertificateCustomValidationCallback_IgnoresCertificateHo Assert.AreEqual (SslPolicyErrors.RemoteCertificateNameMismatch, reportedErrors & SslPolicyErrors.RemoteCertificateNameMismatch); } + [Test] + public async Task ServerCertificateCustomValidationCallback_Redirects () + { + int callbackCounter = 0; + + var handler = new AndroidMessageHandler { + ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => { + callbackCounter++; + return errors == SslPolicyErrors.None; + } + }; + + var client = new HttpClient (handler); + var result = await client.GetAsync ("https://httpbin.org/redirect-to?url=https://www.microsoft.com/"); + + Assert.AreEqual (2, callbackCounter); + Assert.IsTrue (result.IsSuccessStatusCode); + } + private async Task AssertRejectsRemoteCertificate (Func makeRequest) { // there is a difference between the exception that's thrown in the .NET build and the legacy Xamarin