Fix arr commands failing when the response body is empty (#709)

This commit is contained in:
Flaminel authored and GitHub committed 2026-08-11 17:57:03 +03:00
1 parent d524eb5632
commit fc919a52ab
2 files changed
+35

No files matched your search

@@ -143,6 +143,28 @@ public class RadarrClientTests
_httpMessageHandler.CapturedRequests.ShouldBeEmpty();
}
[Fact]
public async Task SearchItemsAsync_WithAnEmptyCommandBody_ReturnsEmpty()
{
// An arr answers some commands with an empty body.
_httpMessageHandler.SetupResponse((req, _) =>
{
if (req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/command"))
{
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(string.Empty, Encoding.UTF8, "application/json"),
});
}
return Task.FromResult(JsonNullResponse());
});
var ids = await _client.SearchItemsAsync(_arrInstance, new HashSet<SearchItem> { new() { Id = 10 } });
ids.ShouldBeEmpty();
}
[Fact]
public async Task SearchItemsAsync_PostsMoviesSearchCommandWithAllIds()
{
@@ -1,3 +1,4 @@
using System.Net;
using Cleanuparr.Domain.Entities.Arr;
using Cleanuparr.Domain.Entities.Arr.Queue;
using Cleanuparr.Domain.Enums;
@@ -307,8 +308,20 @@ public abstract class ArrClient : IArrClient
return response;
}
/// <summary>
/// Reads the body of a response from an arr.
/// </summary>
/// <remarks>
/// An arr answers some requests with an empty body. An empty body gives null,
/// because each caller has a value for a null result.
/// </remarks>
protected static async Task<T?> DeserializeStreamAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken = default)
{
if (response.StatusCode is HttpStatusCode.NoContent || response.Content.Headers.ContentLength is 0)
{
return default;
}
await using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken);
return await JsonSerializer.DeserializeAsync<T>(stream, CleanuparrJsonOptions.ExternalApiRead, cancellationToken);
}