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