itfx/tests/InterfaceForward.Application.Tests/DefaultForwardFlowTests.cs
2026-06-05 14:50:40 +08:00

106 lines
3.4 KiB
C#

using InterfaceForward.Application.Contracts.ForwardCore;
using InterfaceForward.Application.ForwardCore;
using InterfaceForward.Domain.Shared;
using InterfaceForward.Domain.Shared.Dtos;
using InterfaceForward.Domain.Shared.Enum;
using Newtonsoft.Json.Linq;
namespace InterfaceForward.Application.Tests;
public class DefaultForwardFlowTests
{
[Fact]
public void SerializationBody_ShouldUseMappedInputForJson()
{
var flow = new TestForwardFlow();
var context = new ForwardCoreContext
{
TargetInterface = new InterfaceDto { ContentType = ContentType.Json },
OriginalInterfaceMappedInput = new JObject { ["orderId"] = "A001" }
};
var body = flow.SerializationBody(context);
Assert.Equal("{\"orderId\":\"A001\"}", body);
}
[Fact]
public async Task SetContentType_ShouldReplaceFromMappingBodyForFormUrlEncoded()
{
var flow = new TestForwardFlow();
var context = new ForwardCoreContext
{
TargetInterface = new InterfaceDto { ContentType = ContentType.FormUrlEncoded },
TargetInterfaceInput = "{\"orderId\":\"A001\"}",
OriginalInterfaceMappedInput = new JObject { ["orderId"] = "A001" },
FormFieldList =
[
new InterfaceFormFieldDto
{
Name = "notifyBizEventDTO",
Value = GlobalConst.FromMappingBody
}
]
};
using var message = new HttpRequestMessage();
flow.SetContentTypeForTest(context, message);
var content = await message.Content!.ReadAsStringAsync();
Assert.Equal("notifyBizEventDTO=%7B%22orderId%22%3A%22A001%22%7D", content);
}
[Fact]
public void NoneContentType_ShouldNotCreateRequestContent()
{
var flow = new TestForwardFlow();
var context = new ForwardCoreContext
{
TargetInterface = new InterfaceDto { ContentType = ContentType.None },
OriginalInterfaceMappedInput = new JObject(),
TargetInterfaceInput = string.Empty
};
using var message = new HttpRequestMessage();
flow.SetContentTypeForTest(context, message);
Assert.Equal(string.Empty, flow.SerializationBody(context));
Assert.Null(message.Content);
}
[Fact]
public void ReplacePlaceholder_ShouldReplaceQueryFromMappingBodyForNoneContentType()
{
var flow = new TestForwardFlow();
var context = new ForwardCoreContext
{
TargetInterface = new InterfaceDto
{
ContentType = ContentType.None,
RequestAddress = "https://example.com/callback"
},
OriginalInterfaceMappedInput = new JObject { ["orderId"] = "A001" },
QueryFieldList =
[
new InterfaceFormFieldDto
{
Name = "notifyBizEventDTO",
Value = GlobalConst.FromMappingBody
}
]
};
flow.ReplacePlaceholder(context);
Assert.Equal("{\"orderId\":\"A001\"}", Assert.Single(context.QueryFieldList).Value);
}
class TestForwardFlow : DefaultForwardFlow
{
public void SetContentTypeForTest(ForwardCoreContext context, HttpRequestMessage message)
{
SetContentType(context, message);
}
}
}