Skip to content

Commit

Permalink
Merge pull request #90 from ejball/clone-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball authored Jan 24, 2024
2 parents 157af88 + 23da6d0 commit 858d54f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>

<PropertyGroup>
<VersionPrefix>2.23.1</VersionPrefix>
<PackageValidationBaselineVersion>2.23.0</PackageValidationBaselineVersion>
<VersionPrefix>2.24.0</VersionPrefix>
<PackageValidationBaselineVersion>2.23.1</PackageValidationBaselineVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
5 changes: 5 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## 2.24.0

* Add `HttpClientServiceSettings.Clone`.
* Support collection syntax with MessagePack properties.

## 2.23.1

* Throw `ArgumentNullException` instead of `NullReferenceException` when using `ServiceDelegators.Validate` with a null request.
Expand Down
17 changes: 17 additions & 0 deletions src/Facility.Core/Http/HttpClientServiceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,21 @@ public sealed class HttpClientServiceSettings
/// True to prevent the validation of response DTOs after receiving.
/// </summary>
public bool SkipResponseValidation { get; set; }

/// <summary>
/// Creates a deep clone of the instance.
/// </summary>
public HttpClientServiceSettings Clone() => new()
{
BaseUri = BaseUri,
HttpClient = HttpClient,
ContentSerializer = ContentSerializer,
BytesSerializer = BytesSerializer,
TextSerializer = TextSerializer,
DisableChunkedTransfer = DisableChunkedTransfer,
Aspects = Aspects?.ToList(),
Synchronous = Synchronous,
SkipRequestValidation = SkipRequestValidation,
SkipResponseValidation = SkipResponseValidation,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections;
using Facility.Core.Http;
using FluentAssertions;
using NUnit.Framework;

namespace Facility.Core.UnitTests.Http;

public class HttpClientServiceSettingsTests
{
[Test]
public async Task CloneSettings()
{
var original = new HttpClientServiceSettings
{
BaseUri = new Uri("https://example.com"),
HttpClient = new HttpClient(),
ContentSerializer = HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance),
BytesSerializer = BytesHttpContentSerializer.Instance,
TextSerializer = TextHttpContentSerializer.Instance,
DisableChunkedTransfer = true,
Aspects = Array.Empty<HttpClientServiceAspect>(),
Synchronous = true,
SkipRequestValidation = true,
SkipResponseValidation = true,
};

var clone = original.Clone();

foreach (var property in typeof(HttpClientServiceSettings).GetProperties())
{
var propertyType = property.PropertyType;

var originalValue = property.GetValue(original);
var nullOrDefault = propertyType.IsValueType && Nullable.GetUnderlyingType(propertyType) is null ? Activator.CreateInstance(propertyType) : null;
originalValue.Should().NotBe(nullOrDefault, $"original {property.Name} should not be null/default.");

var clonedValue = property.GetValue(clone);
if (clonedValue is IEnumerable and not string)
clonedValue.Should().BeEquivalentTo(originalValue, $"cloned {property.Name} should be equivalent to original.");
else
clonedValue.Should().Be(originalValue, $"cloned {property.Name} should be equal to original.");
}
}
}

0 comments on commit 858d54f

Please sign in to comment.