Skip to content

Commit 72a2e92

Browse files
authored
chore: rework NatsOpt.Default initialization (#921)
* chore: rework NatsOpt.Default initialization * chore: removed unused using statement
1 parent c775647 commit 72a2e92

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

src/NATS.Client.Core/NatsOpts.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public enum NatsRequestReplyMode
3030
/// </summary>
3131
public sealed record NatsOpts
3232
{
33-
public static readonly NatsOpts Default = new();
33+
public static readonly NatsOpts Default = new()
34+
{
35+
WebSocketOpts = NatsWebSocketOpts.Default,
36+
TlsOpts = NatsTlsOpts.Default,
37+
AuthOpts = NatsAuthOpts.Default,
38+
};
3439

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

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

67-
public NatsAuthOpts AuthOpts { get; init; } = NatsAuthOpts.Default;
72+
public NatsAuthOpts AuthOpts { get; init; } = new();
6873

69-
public NatsTlsOpts TlsOpts { get; init; } = NatsTlsOpts.Default;
74+
public NatsTlsOpts TlsOpts { get; init; } = new();
7075

71-
public NatsWebSocketOpts WebSocketOpts { get; init; } = NatsWebSocketOpts.Default;
76+
public NatsWebSocketOpts WebSocketOpts { get; init; } = new();
7277

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

tests/NATS.Client.CoreUnit.Tests/NATS.Client.CoreUnit.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
</PackageReference>
2929
<PackageReference Include="System.Net.Http" Version="4.3.4" />
3030
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
31+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
32+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
3133
</ItemGroup>
3234

3335
<ItemGroup>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Microsoft.Extensions.Configuration;
2+
3+
public class OptsUrlTests
4+
{
5+
[Fact]
6+
public void Default_opts_should_be_equivalent_but_not_same_as_new()
7+
{
8+
// Arrange
9+
var opts1 = NatsOpts.Default;
10+
var opts2 = new NatsOpts();
11+
var opts3 = NatsOpts.Default;
12+
13+
// Assert
14+
opts1.Should().BeEquivalentTo(opts2);
15+
opts1.Should().NotBeSameAs(opts2);
16+
opts1.AuthOpts.Should().NotBeSameAs(opts2.AuthOpts);
17+
opts1.TlsOpts.Should().NotBeSameAs(opts2.TlsOpts);
18+
opts1.WebSocketOpts.Should().NotBeSameAs(opts2.WebSocketOpts);
19+
opts1.SerializerRegistry.Should().BeSameAs(opts2.SerializerRegistry);
20+
opts1.LoggerFactory.Should().BeSameAs(opts2.LoggerFactory);
21+
22+
opts1.Should().BeEquivalentTo(opts3);
23+
opts1.Should().BeSameAs(opts3);
24+
opts1.AuthOpts.Should().BeSameAs(opts3.AuthOpts);
25+
opts1.TlsOpts.Should().BeSameAs(opts3.TlsOpts);
26+
opts1.WebSocketOpts.Should().BeSameAs(opts3.WebSocketOpts);
27+
opts1.SerializerRegistry.Should().BeSameAs(opts3.SerializerRegistry);
28+
opts1.LoggerFactory.Should().BeSameAs(opts3.LoggerFactory);
29+
}
30+
31+
[Fact]
32+
public void Should_bind_configuration_to_natsopts_only_once()
33+
{
34+
// Arrange
35+
var configValues = new Dictionary<string, string?>
36+
{
37+
{ "SOURCE:AUTHOPTS:USERNAME", "HELLO" },
38+
{ "SOURCE:TLSOPTS:CERTBUNDLEFILEPASSWORD", "HELLO" },
39+
{ "SOURCE:WEBSOCKETOPTS:REQUESTHEADERS:HELLO", null },
40+
{ "TARGET:AUTHOPTS:USERNAME", "WORLD" },
41+
{ "TARGET:TLSOPTS:CERTBUNDLEFILEPASSWORD", "WORLD" },
42+
{ "TARGET:WEBSOCKETOPTS:REQUESTHEADERS:WORLD", null },
43+
};
44+
IConfiguration configuration = new ConfigurationBuilder()
45+
.AddInMemoryCollection(configValues)
46+
.Build();
47+
48+
// Act
49+
var opts1 = new NatsOpts();
50+
configuration.Bind("SOURCE", opts1);
51+
var opts2 = new NatsOpts();
52+
configuration.Bind("TARGET", opts2);
53+
54+
// Assert
55+
opts1.AuthOpts.Username.Should().Be("HELLO");
56+
opts1.TlsOpts.CertBundleFilePassword.Should().Be("HELLO");
57+
opts1.WebSocketOpts.RequestHeaders.Should().ContainKey("HELLO");
58+
59+
opts2.AuthOpts.Username.Should().Be("WORLD");
60+
opts2.TlsOpts.CertBundleFilePassword.Should().Be("WORLD");
61+
opts2.WebSocketOpts.RequestHeaders.Should().ContainKey("WORLD");
62+
}
63+
}

0 commit comments

Comments
 (0)