Add unit tests and logging infrastructure for InMemory and RabbitMQ transports
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implemented RecordingLogger and RecordingLoggerFactory for capturing log entries in tests. - Added unit tests for InMemoryChannel, covering constructor behavior, property assignments, channel communication, and disposal. - Created InMemoryTransportOptionsTests to validate default values and customizable options for InMemory transport. - Developed RabbitMqFrameProtocolTests to ensure correct parsing and property creation for RabbitMQ frames. - Added RabbitMqTransportOptionsTests to verify default settings and customization options for RabbitMQ transport. - Updated project files for testing libraries and dependencies.
This commit is contained in:
@@ -0,0 +1,383 @@
|
||||
namespace StellaOps.Microservice.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="HeaderCollection"/>.
|
||||
/// </summary>
|
||||
public sealed class HeaderCollectionTests
|
||||
{
|
||||
#region Constructor Tests
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Default_CreatesEmptyCollection()
|
||||
{
|
||||
// Arrange & Act
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Assert
|
||||
headers.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithKeyValuePairs_AddsAllHeaders()
|
||||
{
|
||||
// Arrange
|
||||
var pairs = new[]
|
||||
{
|
||||
new KeyValuePair<string, string>("Content-Type", "application/json"),
|
||||
new KeyValuePair<string, string>("Accept", "application/json")
|
||||
};
|
||||
|
||||
// Act
|
||||
var headers = new HeaderCollection(pairs);
|
||||
|
||||
// Assert
|
||||
headers["Content-Type"].Should().Be("application/json");
|
||||
headers["Accept"].Should().Be("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithDuplicateKeys_AddsMultipleValues()
|
||||
{
|
||||
// Arrange
|
||||
var pairs = new[]
|
||||
{
|
||||
new KeyValuePair<string, string>("Accept", "application/json"),
|
||||
new KeyValuePair<string, string>("Accept", "text/plain")
|
||||
};
|
||||
|
||||
// Act
|
||||
var headers = new HeaderCollection(pairs);
|
||||
|
||||
// Assert
|
||||
headers.GetValues("Accept").Should().BeEquivalentTo(["application/json", "text/plain"]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Empty Tests
|
||||
|
||||
[Fact]
|
||||
public void Empty_IsSharedInstance()
|
||||
{
|
||||
// Arrange & Act
|
||||
var empty1 = HeaderCollection.Empty;
|
||||
var empty2 = HeaderCollection.Empty;
|
||||
|
||||
// Assert
|
||||
empty1.Should().BeSameAs(empty2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Empty_HasNoHeaders()
|
||||
{
|
||||
// Arrange & Act
|
||||
var empty = HeaderCollection.Empty;
|
||||
|
||||
// Assert
|
||||
empty.Should().BeEmpty();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer Tests
|
||||
|
||||
[Fact]
|
||||
public void Indexer_ExistingKey_ReturnsFirstValue()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act
|
||||
var value = headers["Content-Type"];
|
||||
|
||||
// Assert
|
||||
value.Should().Be("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Indexer_MultipleValues_ReturnsFirstValue()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Accept", "application/json");
|
||||
headers.Add("Accept", "text/plain");
|
||||
|
||||
// Act
|
||||
var value = headers["Accept"];
|
||||
|
||||
// Assert
|
||||
value.Should().Be("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Indexer_NonexistentKey_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act
|
||||
var value = headers["X-Missing"];
|
||||
|
||||
// Assert
|
||||
value.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Indexer_IsCaseInsensitive()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act & Assert
|
||||
headers["content-type"].Should().Be("application/json");
|
||||
headers["CONTENT-TYPE"].Should().Be("application/json");
|
||||
headers["Content-TYPE"].Should().Be("application/json");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Add Tests
|
||||
|
||||
[Fact]
|
||||
public void Add_NewKey_AddsHeader()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Assert
|
||||
headers["Content-Type"].Should().Be("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_ExistingKey_AppendsValue()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Accept", "application/json");
|
||||
|
||||
// Act
|
||||
headers.Add("Accept", "text/plain");
|
||||
|
||||
// Assert
|
||||
headers.GetValues("Accept").Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_CaseInsensitiveKey_AppendsToExisting()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act
|
||||
headers.Add("content-type", "text/plain");
|
||||
|
||||
// Assert
|
||||
headers.GetValues("Content-Type").Should().HaveCount(2);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Set Tests
|
||||
|
||||
[Fact]
|
||||
public void Set_NewKey_AddsHeader()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act
|
||||
headers.Set("Content-Type", "application/json");
|
||||
|
||||
// Assert
|
||||
headers["Content-Type"].Should().Be("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Set_ExistingKey_ReplacesValue()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "text/plain");
|
||||
headers.Add("Content-Type", "text/html");
|
||||
|
||||
// Act
|
||||
headers.Set("Content-Type", "application/json");
|
||||
|
||||
// Assert
|
||||
headers.GetValues("Content-Type").Should().BeEquivalentTo(["application/json"]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetValues Tests
|
||||
|
||||
[Fact]
|
||||
public void GetValues_ExistingKey_ReturnsAllValues()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Accept", "application/json");
|
||||
headers.Add("Accept", "text/plain");
|
||||
headers.Add("Accept", "text/html");
|
||||
|
||||
// Act
|
||||
var values = headers.GetValues("Accept");
|
||||
|
||||
// Assert
|
||||
values.Should().BeEquivalentTo(["application/json", "text/plain", "text/html"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetValues_NonexistentKey_ReturnsEmptyEnumerable()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act
|
||||
var values = headers.GetValues("X-Missing");
|
||||
|
||||
// Assert
|
||||
values.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetValues_IsCaseInsensitive()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Accept", "application/json");
|
||||
|
||||
// Act & Assert
|
||||
headers.GetValues("accept").Should().Contain("application/json");
|
||||
headers.GetValues("ACCEPT").Should().Contain("application/json");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region TryGetValue Tests
|
||||
|
||||
[Fact]
|
||||
public void TryGetValue_ExistingKey_ReturnsTrueAndValue()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act
|
||||
var result = headers.TryGetValue("Content-Type", out var value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
value.Should().Be("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValue_NonexistentKey_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act
|
||||
var result = headers.TryGetValue("X-Missing", out var value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
value.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValue_IsCaseInsensitive()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act
|
||||
var result = headers.TryGetValue("content-type", out var value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
value.Should().Be("application/json");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ContainsKey Tests
|
||||
|
||||
[Fact]
|
||||
public void ContainsKey_ExistingKey_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act & Assert
|
||||
headers.ContainsKey("Content-Type").Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContainsKey_NonexistentKey_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act & Assert
|
||||
headers.ContainsKey("X-Missing").Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContainsKey_IsCaseInsensitive()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
|
||||
// Act & Assert
|
||||
headers.ContainsKey("content-type").Should().BeTrue();
|
||||
headers.ContainsKey("CONTENT-TYPE").Should().BeTrue();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Enumeration Tests
|
||||
|
||||
[Fact]
|
||||
public void GetEnumerator_EnumeratesAllHeaderValues()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
headers.Add("Content-Type", "application/json");
|
||||
headers.Add("Accept", "text/plain");
|
||||
headers.Add("Accept", "text/html");
|
||||
|
||||
// Act
|
||||
var list = headers.ToList();
|
||||
|
||||
// Assert
|
||||
list.Should().HaveCount(3);
|
||||
list.Should().Contain(new KeyValuePair<string, string>("Content-Type", "application/json"));
|
||||
list.Should().Contain(new KeyValuePair<string, string>("Accept", "text/plain"));
|
||||
list.Should().Contain(new KeyValuePair<string, string>("Accept", "text/html"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetEnumerator_EmptyCollection_EnumeratesNothing()
|
||||
{
|
||||
// Arrange
|
||||
var headers = new HeaderCollection();
|
||||
|
||||
// Act
|
||||
var list = headers.ToList();
|
||||
|
||||
// Assert
|
||||
list.Should().BeEmpty();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user