Skip to content

[Mono.Android-Tests] Ignore temporary HTTP errors in async test methods. #9942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,27 @@ public void EnsureSuccessStatusCode (HttpResponseMessage response)
{
// These status codes all indicate a temporary network/server failure,
// so just ignore the test if we hit them.
switch (response.StatusCode) {
if (ShouldIgnoreSuccessStatusCode (response.StatusCode)) {
Assert.Ignore ($"Ignoring network/server failure: {response.StatusCode}");
return;
}

response.EnsureSuccessStatusCode ();
}

public bool ShouldIgnoreSuccessStatusCode (HttpStatusCode code)
{
// These status codes all indicate a temporary network/server failure,
// so just ignore the test if we hit them.
switch (code) {
case HttpStatusCode.InternalServerError:
case HttpStatusCode.BadGateway:
case HttpStatusCode.ServiceUnavailable:
case HttpStatusCode.GatewayTimeout:
Assert.Ignore ($"Ignoring network/server failure: {response.StatusCode}");
return;
return true;
}

response.EnsureSuccessStatusCode ();
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ public async Task ServerCertificateCustomValidationCallback_Redirects ()
var client = new HttpClient (handler);
var result = await client.GetAsync ("https://httpbin.org/redirect-to?url=https://www.microsoft.com/");

// Our dated NUnit test runner doesn't handle Assert.Ignore in an async context, so just exit
if (ShouldIgnoreSuccessStatusCode (result.StatusCode)) {
Console.WriteLine ($"Ignoring {result.StatusCode} status code");
return;
}

EnsureSuccessStatusCode (result);

Assert.AreEqual (2, callbackCounter);
Expand All @@ -246,6 +252,12 @@ public async Task AndroidMessageHandlerFollows308PermanentRedirect ()
var client = new HttpClient (handler);
var result = await client.GetAsync ("https://httpbin.org/redirect-to?url=https://www.microsoft.com/&status_code=308");

// Our dated NUnit test runner doesn't handle Assert.Ignore in an async context, so just exit
if (ShouldIgnoreSuccessStatusCode (result.StatusCode)) {
Console.WriteLine ($"Ignoring {result.StatusCode} status code");
return;
}

EnsureSuccessStatusCode (result);

Assert.AreEqual ("https://www.microsoft.com/", result.RequestMessage.RequestUri.ToString ());
Expand Down
Loading