Skip to content

Commit f05f2c1

Browse files
committed
test(http): 添加 HTTP 路由注册单元测试
添加 AppStartUpByHttpServerRouteTests 测试类, 验证路由去重逻辑的正确性。
1 parent daef0a7 commit f05f2c1

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Tests/GameFrameX.Tests/GameFrameX.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<ProjectReference Include="..\..\GameFrameX.Apps\GameFrameX.Apps.csproj" />
3434
<ProjectReference Include="..\..\GameFrameX.Core\GameFrameX.Core.csproj" />
3535
<ProjectReference Include="..\..\GameFrameX.ProtoBuf.Net\GameFrameX.ProtoBuf.Net.csproj" />
36+
<ProjectReference Include="..\..\GameFrameX.StartUp\GameFrameX.StartUp.csproj" />
3637
<ProjectReference Include="..\..\GameFrameX.Utility\GameFrameX.Utility.csproj" />
3738
<ProjectReference Include="..\..\GameFrameX.NetWork.Kcp\GameFrameX.NetWork.Kcp.csproj" />
3839
</ItemGroup>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ==========================================================================================
2+
// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+
// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+
// 均受中华人民共和国及相关国际法律法规保护。
5+
// are protected by the laws of the People's Republic of China and relevant international regulations.
6+
//
7+
// 使用本项目须严格遵守相应法律法规及开源许可证之规定。
8+
// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
9+
//
10+
// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
11+
// This project is dual-licensed under the MIT License and Apache License 2.0,
12+
// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
13+
// please refer to the LICENSE file in the root directory of the source code for the full license text.
14+
//
15+
// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
16+
// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
17+
// 侵犯他人合法权益等法律法规所禁止的行为!
18+
// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
19+
// 因基于本项目二次开发所产生的一切法律纠纷与责任,
20+
// Any legal disputes and liabilities arising from secondary development based on this project
21+
// 本项目组织与贡献者概不承担。
22+
// shall be borne solely by the developer; the project organization and contributors assume no responsibility.
23+
//
24+
// GitHub 仓库:https://github.com/GameFrameX
25+
// GitHub Repository: https://github.com/GameFrameX
26+
// Gitee 仓库:https://gitee.com/GameFrameX
27+
// Gitee Repository: https://gitee.com/GameFrameX
28+
// 官方文档:https://gameframex.doc.alianblank.com/
29+
// Official Documentation: https://gameframex.doc.alianblank.com/
30+
// ==========================================================================================
31+
32+
using System.Reflection;
33+
using GameFrameX.NetWork.HTTP;
34+
using GameFrameX.StartUp;
35+
using Xunit;
36+
37+
namespace GameFrameX.Tests.StartUp;
38+
39+
/// <summary>
40+
/// HTTP 路由注册去重测试。
41+
/// </summary>
42+
public class AppStartUpByHttpServerRouteTests
43+
{
44+
/// <summary>
45+
/// 验证同一HTTP方法下仅大小写不同的路径会被识别为重复路由。
46+
/// </summary>
47+
[Fact]
48+
public void TryAddRouteKey_SameMethodDifferentCasePath_ShouldTreatAsDuplicate()
49+
{
50+
var registeredRouteKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
51+
52+
var firstAddResult = InvokeTryAddRouteKey(HttpMethodType.POST, "/game/api/Test", registeredRouteKeys);
53+
var secondAddResult = InvokeTryAddRouteKey(HttpMethodType.POST, "/game/api/test", registeredRouteKeys);
54+
55+
Assert.True(firstAddResult);
56+
Assert.False(secondAddResult);
57+
Assert.Single(registeredRouteKeys);
58+
}
59+
60+
/// <summary>
61+
/// 验证不同HTTP方法允许注册相同路径。
62+
/// </summary>
63+
[Fact]
64+
public void TryAddRouteKey_DifferentMethodSamePath_ShouldAllowBoth()
65+
{
66+
var registeredRouteKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
67+
68+
var postAddResult = InvokeTryAddRouteKey(HttpMethodType.POST, "/game/api/test", registeredRouteKeys);
69+
var getAddResult = InvokeTryAddRouteKey(HttpMethodType.GET, "/game/api/test", registeredRouteKeys);
70+
71+
Assert.True(postAddResult);
72+
Assert.True(getAddResult);
73+
Assert.Equal(2, registeredRouteKeys.Count);
74+
}
75+
76+
/// <summary>
77+
/// 通过反射调用 AppStartUpBase 的路由键去重方法。
78+
/// </summary>
79+
/// <param name="httpMethod">HTTP方法</param>
80+
/// <param name="apiPath">API路径</param>
81+
/// <param name="registeredRouteKeys">已注册路由集合</param>
82+
/// <returns>返回去重结果</returns>
83+
private static bool InvokeTryAddRouteKey(HttpMethodType httpMethod, string apiPath, ISet<string> registeredRouteKeys)
84+
{
85+
var method = typeof(AppStartUpBase).GetMethod("TryAddRouteKey", BindingFlags.NonPublic | BindingFlags.Static);
86+
Assert.NotNull(method);
87+
88+
var result = method.Invoke(null, new object[] { httpMethod, apiPath, registeredRouteKeys });
89+
Assert.NotNull(result);
90+
return (bool)result;
91+
}
92+
}

0 commit comments

Comments
 (0)