Skip to content

Commit 1e6c019

Browse files
committed
refactor(homeMatic): improve test readability and update annotations
- Replaced `Encoding.UTF8.GetBytes` with `u8.ToArray` for concise byte array creation in tests. - Updated `BackupCcuOptions` annotation from `[UsedImplicitly]` to `[PublicAPI]` for better tooling support. - Simplified `SingleHandlerHttpClientFactory` by converting to a C# primary constructor. - Refactored conditional logic in `QueueingHttpMessageHandler` for cleaner exception handling.
1 parent c4fba9c commit 1e6c019

4 files changed

Lines changed: 9 additions & 18 deletions

File tree

source/Tools/Cli/CreativeCoders.HomeMatic.Tools.Cli.Commands/Ccu/Backup/BackupCcuOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace CreativeCoders.HomeMatic.Tools.Cli.Commands.Ccu.Backup;
55

6-
[UsedImplicitly]
6+
[PublicAPI]
77
public class BackupCcuOptions
88
{
99
[OptionValue(0, IsRequired = true, HelpText = "Name of the configured CCU connection")]

tests/CreativeCoders.HomeMatic.Tests/FirmwareBackup/FirmwareBackupClientFactoryIntegrationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace CreativeCoders.HomeMatic.Tests.FirmwareBackup;
1111
public class FirmwareBackupClientFactoryIntegrationTests
1212
{
1313
private const string FakeSessionId = "session-id-xyz";
14-
private static readonly byte[] BackupPayload = Encoding.UTF8.GetBytes("BACKUP-CONTENT");
14+
private static readonly byte[] BackupPayload = "BACKUP-CONTENT"u8.ToArray();
1515

1616
[Fact]
1717
public async Task CreateBackupAsync_HappyPath_PerformsLoginDownloadAndLogout()
@@ -42,7 +42,8 @@ public async Task CreateBackupAsync_HappyPath_PerformsLoginDownloadAndLogout()
4242
handler.Requests[0].Body.Should().Contain("\"Session.login\"").And.Contain("\"Admin\"");
4343
handler.Requests[1].Uri.AbsolutePath.Should().Be("/config/cp_security.cgi");
4444
handler.Requests[1].Method.Should().Be(HttpMethod.Get);
45-
handler.Requests[1].Uri.Query.Should().Contain($"sid=%40{FakeSessionId}%40").And.Contain("action=create_backup");
45+
handler.Requests[1].Uri.Query.Should().Contain($"sid=%40{FakeSessionId}%40").And
46+
.Contain("action=create_backup");
4647
}
4748

4849
[Fact]

tests/CreativeCoders.HomeMatic.Tests/FirmwareBackup/QueueingHttpMessageHandler.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ protected override async Task<HttpResponseMessage> SendAsync(
5252

5353
Requests.Add(new RecordedRequest(request.Method, request.RequestUri!, body));
5454

55-
if (_responders.Count == 0)
56-
{
57-
throw new InvalidOperationException("No more responses queued.");
58-
}
59-
60-
return _responders.Dequeue()(request);
55+
return _responders.Count == 0
56+
? throw new InvalidOperationException("No more responses queued.")
57+
: _responders.Dequeue()(request);
6158
}
6259
}
6360

tests/CreativeCoders.HomeMatic.Tests/FirmwareBackup/SingleHandlerHttpClientFactory.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@ namespace CreativeCoders.HomeMatic.Tests.FirmwareBackup;
44
/// Minimal <see cref="IHttpClientFactory"/> that returns a single <see cref="HttpClient"/> backed by
55
/// the given <see cref="HttpMessageHandler"/>.
66
/// </summary>
7-
internal sealed class SingleHandlerHttpClientFactory : IHttpClientFactory
7+
internal sealed class SingleHandlerHttpClientFactory(HttpMessageHandler handler) : IHttpClientFactory
88
{
9-
private readonly HttpMessageHandler _handler;
10-
11-
public SingleHandlerHttpClientFactory(HttpMessageHandler handler)
12-
{
13-
_handler = handler;
14-
}
15-
169
public HttpClient CreateClient(string name)
1710
{
18-
return new HttpClient(_handler, disposeHandler: false);
11+
return new HttpClient(handler, disposeHandler: false);
1912
}
2013
}

0 commit comments

Comments
 (0)