Solution restructured to use multiple test projects
This commit is contained in:
19
src/AMWD.Common.Test/AMWD.Common.Test.csproj
Normal file
19
src/AMWD.Common.Test/AMWD.Common.Test.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
|
||||
<AssemblyName>amwd-common-test</AssemblyName>
|
||||
<RootNamespace>AMWD.Common.Test</RootNamespace>
|
||||
|
||||
<NrtTagMatch>test/v[0-9]*</NrtTagMatch>
|
||||
<PackageId>AMWD.Common.Test</PackageId>
|
||||
<Product>AM.WD Common Library for Unit-Testing</Product>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Moq" Version="4.20.70" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
130
src/AMWD.Common.Test/HttpMessageHandlerMoq.cs
Normal file
130
src/AMWD.Common.Test/HttpMessageHandlerMoq.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Moq.Protected;
|
||||
|
||||
namespace AMWD.Common.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapps the <see cref="Mock{HttpMessageHandler}"/> including the setup.
|
||||
/// </summary>
|
||||
public class HttpMessageHandlerMoq
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HttpMessageHandlerMoq"/> class.
|
||||
/// </summary>
|
||||
public HttpMessageHandlerMoq()
|
||||
{
|
||||
Response = new() { StatusCode = HttpStatusCode.OK };
|
||||
Callbacks = new();
|
||||
|
||||
Mock = new();
|
||||
Mock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
|
||||
.Callback<HttpRequestMessage, CancellationToken>(async (req, _) =>
|
||||
{
|
||||
var callback = new HttpMessageRequestCallback
|
||||
{
|
||||
Headers = req.Headers,
|
||||
Method = req.Method,
|
||||
Properties = req.Properties,
|
||||
RequestUri = req.RequestUri,
|
||||
Version = req.Version
|
||||
};
|
||||
|
||||
if (req.Content != null)
|
||||
{
|
||||
callback.ContentBytes = await req.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
callback.ContentString = await req.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Callbacks.Add(callback);
|
||||
})
|
||||
.ReturnsAsync(Response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mocked <see cref="HttpMessageHandler"/>.
|
||||
/// </summary>
|
||||
public Mock<HttpMessageHandler> Mock { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the placed request.
|
||||
/// </summary>
|
||||
public List<HttpMessageRequestCallback> Callbacks { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HTTP response, that should be "sent".
|
||||
/// </summary>
|
||||
public HttpResponseMessage Response { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Disposes and resets the <see cref="Response"/> and <see cref="Callbacks"/>.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
Response.Dispose();
|
||||
Response = new() { StatusCode = HttpStatusCode.OK };
|
||||
|
||||
Callbacks.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the number of calls to the HTTP request.
|
||||
/// </summary>
|
||||
/// <param name="times"></param>
|
||||
public void Verify(Times times)
|
||||
=> Mock.Protected().Verify("SendAsync", times, ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>());
|
||||
|
||||
/// <summary>
|
||||
/// Represents the placed HTTP request.
|
||||
/// </summary>
|
||||
public class HttpMessageRequestCallback
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the contents of the HTTP message.
|
||||
/// </summary>
|
||||
public byte[] ContentBytes { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the contents of the HTTP message.
|
||||
/// </summary>
|
||||
public string ContentString { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of HTTP request headers.
|
||||
/// </summary>
|
||||
public HttpRequestHeaders Headers { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HTTP method used by the HTTP request message.
|
||||
/// </summary>
|
||||
public HttpMethod Method { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets of properties for the HTTP request.
|
||||
/// </summary>
|
||||
public IDictionary<string, object> Properties { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Uri"/> used for the HTTP request.
|
||||
/// </summary>
|
||||
public Uri RequestUri { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="RequestUri"/> string representation.
|
||||
/// </summary>
|
||||
public string RequestUrl => RequestUri?.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HTTP message version.
|
||||
/// </summary>
|
||||
public Version Version { get; internal set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
87
src/AMWD.Common.Test/SnapshotAssert.cs
Normal file
87
src/AMWD.Common.Test/SnapshotAssert.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace AMWD.Common.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements a snapshot comparison for content aggregation (e.g. files).
|
||||
/// </summary>
|
||||
public sealed class SnapshotAssert
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests whether the specified string is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <param name="actual">The current aggregated content string.</param>
|
||||
/// <param name="message">An error message.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
|
||||
public static void AreEqual(string actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
{
|
||||
string cleanLineEnding = actual
|
||||
.Replace("\r\n", "\n") // Windows
|
||||
.Replace("\r", "\n"); // old MacOS
|
||||
AreEqual(Encoding.UTF8.GetBytes(cleanLineEnding), message, callerFilePath, callerMemberName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified byte array is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <param name="actual">The current aggregated content bytes.</param>
|
||||
/// <param name="message">An error message.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
|
||||
public static void AreEqual(byte[] actual, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
=> AreEqual(actual, 0, -1, message, callerFilePath, callerMemberName);
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified byte array is equal to the saved snapshot.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The past has shown, that e.g. wkhtmltopdf prints the current timestamp at the beginning of the PDF file.
|
||||
/// Therefore only a specific part of that file can be asserted to be equal.
|
||||
/// </remarks>
|
||||
/// <param name="actual">The current aggregated content bytes.</param>
|
||||
/// <param name="firstByteIndex">The first byte to compare.</param>
|
||||
/// <param name="lastByteIndex">The last byte to compare.</param>
|
||||
/// <param name="message">An error message.</param>
|
||||
/// <param name="callerFilePath">The absolute file path of the calling file (filled automatically on compile time).</param>
|
||||
/// <param name="callerMemberName">The name of the calling method (filled automatically on compile time).</param>
|
||||
public static void AreEqual(byte[] actual, int firstByteIndex, int lastByteIndex, string message = null, [CallerFilePath] string callerFilePath = null, [CallerMemberName] string callerMemberName = null)
|
||||
{
|
||||
string callerDir = Path.GetDirectoryName(callerFilePath);
|
||||
string callerFile = Path.GetFileNameWithoutExtension(callerFilePath);
|
||||
|
||||
string snapshotDir = Path.Combine(callerDir, "Snapshots", callerFile);
|
||||
string snapshotFile = Path.Combine(snapshotDir, $"{callerMemberName}.snap");
|
||||
|
||||
if (File.Exists(snapshotFile))
|
||||
{
|
||||
byte[] expected = File.ReadAllBytes(snapshotFile);
|
||||
|
||||
var actualBytes = actual.Skip(firstByteIndex);
|
||||
var expectedBytes = expected.Skip(firstByteIndex);
|
||||
|
||||
if (lastByteIndex > firstByteIndex)
|
||||
{
|
||||
actualBytes = actualBytes.Take(lastByteIndex - firstByteIndex);
|
||||
expectedBytes = expectedBytes.Take(lastByteIndex - firstByteIndex);
|
||||
}
|
||||
|
||||
if (message == null)
|
||||
CollectionAssert.AreEqual(expectedBytes.ToArray(), actualBytes.ToArray());
|
||||
else
|
||||
CollectionAssert.AreEqual(expectedBytes.ToArray(), actualBytes.ToArray(), message);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Directory.Exists(snapshotDir))
|
||||
Directory.CreateDirectory(snapshotDir);
|
||||
|
||||
File.WriteAllBytes(snapshotFile, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
175
src/AMWD.Common.Test/TcpClientMoq.cs
Normal file
175
src/AMWD.Common.Test/TcpClientMoq.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
|
||||
namespace AMWD.Common.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapps the <see cref="Mock{TcpClient}"/> including the setup.
|
||||
/// </summary>
|
||||
public class TcpClientMoq
|
||||
{
|
||||
private readonly Mock<NetworkStream> _streamMock;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TcpClientMoq"/> class.
|
||||
/// </summary>
|
||||
public TcpClientMoq()
|
||||
{
|
||||
Callbacks = new();
|
||||
Response = new byte[0];
|
||||
|
||||
_streamMock = new();
|
||||
_streamMock
|
||||
.Setup(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.Callback<byte[], int, int, CancellationToken>((buffer, offset, count, _) =>
|
||||
{
|
||||
var callback = new TcpClientCallback
|
||||
{
|
||||
Buffer = new byte[count],
|
||||
Offset = offset,
|
||||
Count = count,
|
||||
Type = TcpClientCallback.WriteType.Asynchronous
|
||||
};
|
||||
Array.Copy(buffer, offset, callback.Buffer, 0, count);
|
||||
|
||||
Callbacks.Add(callback);
|
||||
})
|
||||
.Returns(Task.CompletedTask);
|
||||
_streamMock
|
||||
.Setup(s => s.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
|
||||
.Callback<byte[], int, int>((buffer, offset, count) =>
|
||||
{
|
||||
var callback = new TcpClientCallback
|
||||
{
|
||||
Buffer = new byte[count],
|
||||
Offset = offset,
|
||||
Count = count,
|
||||
Type = TcpClientCallback.WriteType.Synchronous
|
||||
};
|
||||
Array.Copy(buffer, offset, callback.Buffer, 0, count);
|
||||
|
||||
Callbacks.Add(callback);
|
||||
});
|
||||
|
||||
_streamMock
|
||||
.Setup(s => s.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.Callback<byte[], int, int, CancellationToken>((buffer, offset, count, _) =>
|
||||
{
|
||||
byte[] bytes = Response ?? new byte[0];
|
||||
Array.Copy(bytes, 0, buffer, offset, Math.Min(bytes.Length, count));
|
||||
})
|
||||
.ReturnsAsync(Response?.Length ?? 0);
|
||||
_streamMock
|
||||
.Setup(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
|
||||
.Callback<byte[], int, int>((buffer, offset, count) =>
|
||||
{
|
||||
byte[] bytes = Response ?? new byte[0];
|
||||
Array.Copy(bytes, 0, buffer, offset, Math.Min(bytes.Length, count));
|
||||
})
|
||||
.Returns(Response?.Length ?? 0);
|
||||
|
||||
Mock = new();
|
||||
Mock
|
||||
.Setup(c => c.GetStream())
|
||||
.Returns(_streamMock.Object);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mocked <see cref="TcpClient"/>.
|
||||
/// </summary>
|
||||
public Mock<TcpClient> Mock { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the placed request.
|
||||
/// </summary>
|
||||
public List<TcpClientCallback> Callbacks { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the byte response, that should be "sent".
|
||||
/// </summary>
|
||||
public byte[] Response { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Resets the <see cref="Response"/> and <see cref="Callbacks"/>.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
Response = new byte[0];
|
||||
Callbacks.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the number of calls writing asynchronous to the stream.
|
||||
/// </summary>
|
||||
/// <param name="times">Number of calls.</param>
|
||||
public void VerifyWriteAsync(Times times)
|
||||
=> _streamMock.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), times);
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the number of calls writing synchronous to the stream.
|
||||
/// </summary>
|
||||
/// <param name="times">Number of calls.</param>
|
||||
public void VerifyWriteSync(Times times)
|
||||
=> _streamMock.Verify(s => s.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), times);
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the number of calls reading asynchronous from the stream.
|
||||
/// </summary>
|
||||
/// <param name="times">Number of calls.</param>
|
||||
public void VerifyReadAsync(Times times)
|
||||
=> _streamMock.Verify(s => s.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), times);
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the number of calls reading synchronous from the stream.
|
||||
/// </summary>
|
||||
/// <param name="times">Number of calls.</param>
|
||||
public void VerifyReadSync(Times times)
|
||||
=> _streamMock.Verify(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), times);
|
||||
|
||||
/// <summary>
|
||||
/// Represents the placed TCP request.
|
||||
/// </summary>
|
||||
public class TcpClientCallback
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the type (a/synchronous call).
|
||||
/// </summary>
|
||||
public WriteType Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the buffer content.
|
||||
/// </summary>
|
||||
public byte[] Buffer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the offset.
|
||||
/// </summary>
|
||||
public int Offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the byte count.
|
||||
/// </summary>
|
||||
public int Count { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Lists the possible request types.
|
||||
/// </summary>
|
||||
public enum WriteType
|
||||
{
|
||||
/// <summary>
|
||||
/// The request was synchronous.
|
||||
/// </summary>
|
||||
Synchronous = 1,
|
||||
|
||||
/// <summary>
|
||||
/// The request was asynchronous.
|
||||
/// </summary>
|
||||
Asynchronous = 2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user