mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2026-10-28 02:38:55 -04:00
Fix Transmission connections failing when the 409 session challenge is retried (#714)
This commit is contained in:
1 parent
f541cfa97c
commit
f7e3ba4196
3 files changed
+117
-9
No files matched your search
+43
@@ -0,0 +1,43 @@
|
||||
using System.Net;
|
||||
using Cleanuparr.Infrastructure.Http.DynamicHttpClientSystem;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Cleanuparr.Infrastructure.Tests.Http;
|
||||
|
||||
public sealed class DynamicHttpClientConfigurationTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsRetryable_ShouldReturnFalse_WhenResponseIsConflict()
|
||||
{
|
||||
using HttpResponseMessage response = new(HttpStatusCode.Conflict);
|
||||
|
||||
DynamicHttpClientConfiguration.IsRetryable(response, excludeUnauthorized: true).ShouldBeFalse();
|
||||
DynamicHttpClientConfiguration.IsRetryable(response, excludeUnauthorized: false).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsRetryable_ShouldReturnFalse_WhenResponseIsSuccessful()
|
||||
{
|
||||
using HttpResponseMessage response = new(HttpStatusCode.OK);
|
||||
|
||||
DynamicHttpClientConfiguration.IsRetryable(response, excludeUnauthorized: true).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsRetryable_ShouldRespectExcludeUnauthorized()
|
||||
{
|
||||
using HttpResponseMessage response = new(HttpStatusCode.Unauthorized);
|
||||
|
||||
DynamicHttpClientConfiguration.IsRetryable(response, excludeUnauthorized: true).ShouldBeFalse();
|
||||
DynamicHttpClientConfiguration.IsRetryable(response, excludeUnauthorized: false).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsRetryable_ShouldReturnTrue_WhenResponseIsServerError()
|
||||
{
|
||||
using HttpResponseMessage response = new(HttpStatusCode.InternalServerError);
|
||||
|
||||
DynamicHttpClientConfiguration.IsRetryable(response, excludeUnauthorized: true).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
+16
-9
@@ -103,15 +103,7 @@ public class DynamicHttpClientConfiguration : IConfigureNamedOptions<HttpClientF
|
||||
var retryPolicy = HttpPolicyExtensions
|
||||
.HandleTransientHttpError();
|
||||
|
||||
if (retryConfig.ExcludeUnauthorized)
|
||||
{
|
||||
retryPolicy = retryPolicy.OrResult(response =>
|
||||
!response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.Unauthorized);
|
||||
}
|
||||
else
|
||||
{
|
||||
retryPolicy = retryPolicy.OrResult(response => !response.IsSuccessStatusCode);
|
||||
}
|
||||
retryPolicy = retryPolicy.OrResult(response => IsRetryable(response, retryConfig.ExcludeUnauthorized));
|
||||
|
||||
var policy = retryPolicy.WaitAndRetryAsync(
|
||||
retryConfig.MaxRetries,
|
||||
@@ -121,6 +113,21 @@ public class DynamicHttpClientConfiguration : IConfigureNamedOptions<HttpClientF
|
||||
builder.AdditionalHandlers.Add(new PolicyHttpMessageHandler(policy));
|
||||
}
|
||||
|
||||
internal static bool IsRetryable(HttpResponseMessage response, bool excludeUnauthorized)
|
||||
{
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response.StatusCode is HttpStatusCode.Conflict)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return !excludeUnauthorized || response.StatusCode != HttpStatusCode.Unauthorized;
|
||||
}
|
||||
|
||||
public void Configure(HttpClientFactoryOptions options)
|
||||
{
|
||||
// This is called for unnamed clients - we don't need to do anything here
|
||||
|
||||
Reference in new issue
Block a user