Send Text Message
This commit is contained in:
56
test/LinkMobility.Tests/Helpers/HttpMessageHandlerMock.cs
Normal file
56
test/LinkMobility.Tests/Helpers/HttpMessageHandlerMock.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Moq.Protected;
|
||||
|
||||
namespace LinkMobility.Tests.Helpers
|
||||
{
|
||||
internal class HttpMessageHandlerMock
|
||||
{
|
||||
public HttpMessageHandlerMock()
|
||||
{
|
||||
Mock = new();
|
||||
|
||||
Mock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
|
||||
.Callback<HttpRequestMessage, CancellationToken>(async (request, cancellationToken) =>
|
||||
{
|
||||
var callback = new HttpRequestMessageCallback
|
||||
{
|
||||
HttpMethod = request.Method,
|
||||
Url = request.RequestUri.ToString(),
|
||||
Headers = request.Headers.ToDictionary(h => h.Key, h => string.Join(", ", h.Value))
|
||||
};
|
||||
|
||||
if (request.Content != null)
|
||||
{
|
||||
callback.ContentRaw = await request.Content.ReadAsByteArrayAsync(cancellationToken);
|
||||
callback.Content = await request.Content.ReadAsStringAsync(cancellationToken);
|
||||
}
|
||||
|
||||
RequestCallbacks.Add(callback);
|
||||
})
|
||||
.ReturnsAsync(Responses.Dequeue);
|
||||
}
|
||||
|
||||
public List<HttpRequestMessageCallback> RequestCallbacks { get; } = [];
|
||||
|
||||
public Queue<HttpResponseMessage> Responses { get; } = new();
|
||||
|
||||
public Mock<HttpClientHandler> Mock { get; }
|
||||
}
|
||||
|
||||
internal class HttpRequestMessageCallback
|
||||
{
|
||||
public HttpMethod HttpMethod { get; set; }
|
||||
|
||||
public string Url { get; set; }
|
||||
|
||||
public Dictionary<string, string> Headers { get; set; }
|
||||
|
||||
public byte[] ContentRaw { get; set; }
|
||||
|
||||
public string Content { get; set; }
|
||||
}
|
||||
}
|
||||
64
test/LinkMobility.Tests/Helpers/ReflectionHelper.cs
Normal file
64
test/LinkMobility.Tests/Helpers/ReflectionHelper.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LinkMobility.Tests.Helpers
|
||||
{
|
||||
internal static class ReflectionHelper
|
||||
{
|
||||
public static T GetPrivateField<T>(object obj, string fieldName)
|
||||
{
|
||||
var field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (field == null)
|
||||
throw new ArgumentException($"Field '{fieldName}' not found in type '{obj.GetType().FullName}'.");
|
||||
|
||||
return (T)field.GetValue(obj);
|
||||
}
|
||||
|
||||
public static void SetPrivateField<T>(object obj, string fieldName, T value)
|
||||
{
|
||||
var field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (field == null)
|
||||
throw new ArgumentException($"Field '{fieldName}' not found in type '{obj.GetType().FullName}'.");
|
||||
|
||||
field.SetValue(obj, value);
|
||||
}
|
||||
|
||||
public static async Task<TResult> InvokePrivateMethodAsync<TResult>(object obj, string methodName, params object[] parameters)
|
||||
{
|
||||
var method = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (method == null)
|
||||
throw new ArgumentException($"Method '{methodName}' not found in type '{obj.GetType().FullName}'.");
|
||||
|
||||
// If the method is a generic method definition, construct it with concrete type arguments.
|
||||
if (method.IsGenericMethodDefinition)
|
||||
{
|
||||
var genericArgs = method.GetGenericArguments();
|
||||
var typeArgs = new Type[genericArgs.Length];
|
||||
|
||||
// First generic argument is the return type (TResult)
|
||||
if (typeArgs.Length > 0)
|
||||
typeArgs[0] = typeof(TResult);
|
||||
|
||||
// For additional generic arguments (e.g., TRequest) try to infer from provided parameters
|
||||
if (typeArgs.Length > 1)
|
||||
{
|
||||
// Common pattern: second generic parameter corresponds to the second method parameter (index 1)
|
||||
Type inferred = typeof(object);
|
||||
if (parameters.Length > 1 && parameters[1] != null)
|
||||
inferred = parameters[1].GetType();
|
||||
|
||||
for (int i = 1; i < typeArgs.Length; i++)
|
||||
typeArgs[i] = inferred;
|
||||
}
|
||||
|
||||
method = method.MakeGenericMethod(typeArgs);
|
||||
}
|
||||
|
||||
var task = (Task)method.Invoke(obj, parameters);
|
||||
await task.ConfigureAwait(false);
|
||||
|
||||
var resultProperty = task.GetType().GetProperty("Result");
|
||||
return (TResult)resultProperty.GetValue(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user