Added custom tagging, moved AMWD.Common.Moq to AMWD.Common.Test
This commit is contained in:
130
AMWD.Common.Test/HttpMessageHandlerMoq.cs
Normal file
130
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();
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user