Skip to content

Commit 7b2e172

Browse files
committed
Tweak the test to retry up to 5 times and dump the retrieved content
1 parent eb92c4d commit 7b2e172

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ protected override HttpMessageHandler CreateHandler ()
3939
#if NET
4040
[Test]
4141
[TestCaseSource (nameof (DecompressionSource))]
42+
[Retry (5)]
4243
public async Task Decompression (string urlPath, string encoding, string jsonFieldName)
44+
{
45+
// Catch all the exceptions and warn about them or otherwise [Retry] above won't work
46+
try {
47+
DoDecompression (urlPath, encoding, jsonFieldName);
48+
} catch (Exception ex) {
49+
Assert.Warn ("Unexpected exception thrown");
50+
Assert.Warn (ex.ToString ());
51+
Assert.Fail ("Exception should have not been thrown");
52+
}
53+
}
54+
55+
void DoDecompression (string urlPath, string encoding, string jsonFieldName)
4356
{
4457
var handler = new AndroidMessageHandler {
4558
AutomaticDecompression = DecompressionMethods.All
@@ -48,6 +61,14 @@ public async Task Decompression (string urlPath, string encoding, string jsonFie
4861
var client = new HttpClient (handler);
4962
HttpResponseMessage response = await client.GetAsync ($"https://httpbin.org/{urlPath}");
5063

64+
// Failing on error codes other than 2xx will make NUnit retry the test up to the number of times specified in the
65+
// [Retry] attribute above. This may or may not the desired effect if httpbin.org is throttling the requests, thus
66+
// we will sleep a short while before failing the test
67+
if (!response.IsSuccessStatusCode) {
68+
System.Threading.Thread.Sleep (1000);
69+
Assert.Fail ($"Request ended with a failure error code: {response.StatusCode}");
70+
}
71+
5172
foreach (string enc in response.Content.Headers.ContentEncoding) {
5273
if (String.Compare (enc, encoding, StringComparison.Ordinal) == 0) {
5374
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
5677

5778
string responseBody = await response.Content.ReadAsStringAsync ();
5879

80+
Assert.Warn ("-- Retrieved JSON start");
81+
Assert.Warn (responseBody);
82+
Assert.Warn ("-- Retrieved JSON end");
83+
5984
Assert.IsTrue (responseBody.Length > 0, "Response was empty");
6085
Assert.AreEqual (response.Content.Headers.ContentLength, responseBody.Length, "Retrieved data length is different than the one specified in the Content-Length header");
6186
Assert.IsTrue (responseBody.Contains ($"\"{jsonFieldName}\"", StringComparison.OrdinalIgnoreCase), $"\"{jsonFieldName}\" should have been in the response JSON");

0 commit comments

Comments
 (0)