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
9 changes: 6 additions & 3 deletions src/Discovery/src/Consul/ConsulServiceInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace Steeltoe.Discovery.Consul;
/// </summary>
internal sealed class ConsulServiceInstance : IServiceInstance
{
private static readonly IReadOnlyList<string> EmptyStringList = Array.Empty<string>();
private static readonly IReadOnlyDictionary<string, string?> EmptyStringDictionary = new Dictionary<string, string?>().AsReadOnly();

/// <inheritdoc />
public string ServiceId { get; }

Expand Down Expand Up @@ -53,9 +56,9 @@ internal ConsulServiceInstance(ServiceEntry serviceEntry)
ArgumentNullException.ThrowIfNull(serviceEntry);

Host = ConsulServerUtils.FindHost(serviceEntry);
Tags = serviceEntry.Service.Tags;
Metadata = serviceEntry.Service.Meta.AsReadOnly();
IsSecure = serviceEntry.Service.Meta != null && serviceEntry.Service.Meta.TryGetValue("secure", out string? secureString) && bool.Parse(secureString);
Tags = serviceEntry.Service.Tags ?? EmptyStringList;
Metadata = serviceEntry.Service.Meta?.AsReadOnly() ?? EmptyStringDictionary;
IsSecure = Metadata.TryGetValue("secure", out string? secureString) && secureString != null && bool.Parse(secureString);
ServiceId = serviceEntry.Service.Service;
InstanceId = serviceEntry.Service.ID;
Port = serviceEntry.Service.Port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@ public void Constructor_Initializes()
serviceInstance.NonSecureUri.Should().BeNull();
serviceInstance.SecureUri.Should().Be(serviceInstance.Uri);
}

[Fact]
public void Constructor_accepts_null_tags_and_meta()
{
var healthService = new ServiceEntry
{
Service = new AgentService
{
Service = "ServiceId",
ID = "Instance1",
Address = "foo.bar.com",
Port = 1234
}
};

var serviceInstance = new ConsulServiceInstance(healthService);

serviceInstance.Tags.Should().BeEmpty();
serviceInstance.Metadata.Should().BeEmpty();
}
}