Skip to content
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
13 changes: 9 additions & 4 deletions src/NATS.Client.Core/NatsOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public enum NatsRequestReplyMode
/// </summary>
public sealed record NatsOpts
{
public static readonly NatsOpts Default = new();
public static readonly NatsOpts Default = new()
{
WebSocketOpts = NatsWebSocketOpts.Default,
TlsOpts = NatsTlsOpts.Default,
AuthOpts = NatsAuthOpts.Default,
};

/// <summary>
/// NATS server URL to connect to. (default: nats://localhost:4222)
Expand Down Expand Up @@ -64,11 +69,11 @@ public sealed record NatsOpts

public bool Headers { get; init; } = true;

public NatsAuthOpts AuthOpts { get; init; } = NatsAuthOpts.Default;
public NatsAuthOpts AuthOpts { get; init; } = new();

public NatsTlsOpts TlsOpts { get; init; } = NatsTlsOpts.Default;
public NatsTlsOpts TlsOpts { get; init; } = new();

public NatsWebSocketOpts WebSocketOpts { get; init; } = NatsWebSocketOpts.Default;
public NatsWebSocketOpts WebSocketOpts { get; init; } = new();

public INatsSerializerRegistry SerializerRegistry { get; init; } = NatsDefaultSerializerRegistry.Default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
</PackageReference>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
63 changes: 63 additions & 0 deletions tests/NATS.Client.CoreUnit.Tests/OptsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.Extensions.Configuration;

public class OptsUrlTests
{
[Fact]
public void Default_opts_should_be_equivalent_but_not_same_as_new()
{
// Arrange
var opts1 = NatsOpts.Default;
var opts2 = new NatsOpts();
var opts3 = NatsOpts.Default;

// Assert
opts1.Should().BeEquivalentTo(opts2);
opts1.Should().NotBeSameAs(opts2);
opts1.AuthOpts.Should().NotBeSameAs(opts2.AuthOpts);
opts1.TlsOpts.Should().NotBeSameAs(opts2.TlsOpts);
opts1.WebSocketOpts.Should().NotBeSameAs(opts2.WebSocketOpts);
opts1.SerializerRegistry.Should().BeSameAs(opts2.SerializerRegistry);
opts1.LoggerFactory.Should().BeSameAs(opts2.LoggerFactory);

opts1.Should().BeEquivalentTo(opts3);
opts1.Should().BeSameAs(opts3);
opts1.AuthOpts.Should().BeSameAs(opts3.AuthOpts);
opts1.TlsOpts.Should().BeSameAs(opts3.TlsOpts);
opts1.WebSocketOpts.Should().BeSameAs(opts3.WebSocketOpts);
opts1.SerializerRegistry.Should().BeSameAs(opts3.SerializerRegistry);
opts1.LoggerFactory.Should().BeSameAs(opts3.LoggerFactory);
}

[Fact]
public void Should_bind_configuration_to_natsopts_only_once()
{
// Arrange
var configValues = new Dictionary<string, string?>
{
{ "SOURCE:AUTHOPTS:USERNAME", "HELLO" },
{ "SOURCE:TLSOPTS:CERTBUNDLEFILEPASSWORD", "HELLO" },
{ "SOURCE:WEBSOCKETOPTS:REQUESTHEADERS:HELLO", null },
{ "TARGET:AUTHOPTS:USERNAME", "WORLD" },
{ "TARGET:TLSOPTS:CERTBUNDLEFILEPASSWORD", "WORLD" },
{ "TARGET:WEBSOCKETOPTS:REQUESTHEADERS:WORLD", null },
};
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(configValues)
.Build();

// Act
var opts1 = new NatsOpts();
configuration.Bind("SOURCE", opts1);
var opts2 = new NatsOpts();
configuration.Bind("TARGET", opts2);

// Assert
opts1.AuthOpts.Username.Should().Be("HELLO");
opts1.TlsOpts.CertBundleFilePassword.Should().Be("HELLO");
opts1.WebSocketOpts.RequestHeaders.Should().ContainKey("HELLO");

opts2.AuthOpts.Username.Should().Be("WORLD");
opts2.TlsOpts.CertBundleFilePassword.Should().Be("WORLD");
opts2.WebSocketOpts.RequestHeaders.Should().ContainKey("WORLD");
}
}
Loading