diff --git a/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs b/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs index f58157a961a..c240ac94ccb 100644 --- a/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs +++ b/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs @@ -39,7 +39,20 @@ protected override HttpMessageHandler CreateHandler () #if NET [Test] [TestCaseSource (nameof (DecompressionSource))] + [Retry (5)] public async Task Decompression (string urlPath, string encoding, string jsonFieldName) + { + // Catch all the exceptions and warn about them or otherwise [Retry] above won't work + try { + DoDecompression (urlPath, encoding, jsonFieldName); + } catch (Exception ex) { + Assert.Warn ("Unexpected exception thrown"); + Assert.Warn (ex.ToString ()); + Assert.Fail ("Exception should have not been thrown"); + } + } + + void DoDecompression (string urlPath, string encoding, string jsonFieldName) { var handler = new AndroidMessageHandler { AutomaticDecompression = DecompressionMethods.All @@ -48,6 +61,14 @@ public async Task Decompression (string urlPath, string encoding, string jsonFie var client = new HttpClient (handler); HttpResponseMessage response = await client.GetAsync ($"https://httpbin.org/{urlPath}"); + // Failing on error codes other than 2xx will make NUnit retry the test up to the number of times specified in the + // [Retry] attribute above. This may or may not the desired effect if httpbin.org is throttling the requests, thus + // we will sleep a short while before failing the test + if (!response.IsSuccessStatusCode) { + System.Threading.Thread.Sleep (1000); + Assert.Fail ($"Request ended with a failure error code: {response.StatusCode}"); + } + foreach (string enc in response.Content.Headers.ContentEncoding) { if (String.Compare (enc, encoding, StringComparison.Ordinal) == 0) { Assert.Fail ($"Encoding '{encoding}' should have been removed from the Content-Encoding header"); @@ -56,6 +77,10 @@ public async Task Decompression (string urlPath, string encoding, string jsonFie string responseBody = await response.Content.ReadAsStringAsync (); + Assert.Warn ("-- Retrieved JSON start"); + Assert.Warn (responseBody); + Assert.Warn ("-- Retrieved JSON end"); + Assert.IsTrue (responseBody.Length > 0, "Response was empty"); Assert.AreEqual (response.Content.Headers.ContentLength, responseBody.Length, "Retrieved data length is different than the one specified in the Content-Length header"); Assert.IsTrue (responseBody.Contains ($"\"{jsonFieldName}\"", StringComparison.OrdinalIgnoreCase), $"\"{jsonFieldName}\" should have been in the response JSON");