1
0

Added custom tagging, moved AMWD.Common.Moq to AMWD.Common.Test

This commit is contained in:
2023-10-05 12:39:58 +02:00
parent 885fc3f1a5
commit 0fab447292
17 changed files with 287 additions and 88 deletions

View File

@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>Debug;Release;DebugLocal</Configurations>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<AssemblyName>AMWD.Common.Test</AssemblyName>
<RootNamespace>AMWD.Common.Test</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>AMWD.Common.Test</PackageId>
<PackageIcon>icon.png</PackageIcon>
<Product>AM.WD Common Library for Unit-Testing</Product>
</PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup Condition="'$(CI)' == 'true'">
<SourceLinkGitLabHost Include="$(CI_SERVER_HOST)" Version="$(CI_SERVER_VERSION)" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<None Include="../icon.png" Pack="true" PackagePath="/" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AMWD.NetRevisionTask" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,26 @@
<Project>
<PropertyGroup>
<NrtRevisionFormat>{semvertag:main}{!:-dev}</NrtRevisionFormat>
<NrtTagMatch>test/v[0-9]*</NrtTagMatch>
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EmbedUntrackedSources>false</EmbedUntrackedSources>
<PackageProjectUrl>https://wiki.am-wd.de/libs/common</PackageProjectUrl>
<Description>Library with classes and extensions used frequently on AM.WD projects.</Description>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://git.am-wd.de/AM.WD/common.git</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Company>AM.WD</Company>
<Authors>Andreas Müller</Authors>
<Copyright>© {copyright:2020-} AM.WD</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
</Project>

View 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();
callback.ContentString = await req.Content.ReadAsStringAsync();
}
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; }
}
}
}

View 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
}
}
}
}