Skip to content

Commit

Permalink
Tweak the test to retry up to 5 times and dump the retrieved content
Browse files Browse the repository at this point in the history
  • Loading branch information
grendello committed Feb 28, 2023
1 parent eb92c4d commit 7b2e172
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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");
Expand All @@ -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");
Expand Down

0 comments on commit 7b2e172

Please sign in to comment.