XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.ServerInteractive

[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.ServerInteractive

Add test entry point services and comprehensive test cases for TServer and Test projects

Agent-Logs-Url: https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive/sessions/a07d561e-176f-4015-8b8c-5e8b6ef5907c Co-authored-by: XFEstudio <132526994+XFEstudio@users.noreply.github.com>

274ba03
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
提交于

代码差异

7 个文件 +473 -36
Modified XFEExtension.NetCore.ServerInteractive.TServer/Program.cs +5 -0
@@ -23,6 +23,11 @@ var server = XFEServerBuilder.CreateBuilder() // 创建服务器构建器
23 23 options.LoginResultConvertFunction = static user => (IUserFaceInfo)user; // 登录结果转换方法
24 24 })
25 25 .AddService<TestCoreService>()
26 .AddService<EchoCoreService>()
27 .AddService<MathCoreService>()
28 .AddService<StatusCoreService>()
29 .AddService<GreetCoreService>()
30 .AddService<TimeCoreService>()
26 31 .Build(options =>
27 32 {
28 33 options.MainEntryPoint = "api";
Added XFEExtension.NetCore.ServerInteractive.TServer/Services/EchoCoreService.cs +17 -0
@@ -0,0 +1,17 @@
1 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 using XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
3
4 namespace XFEExtension.NetCore.ServerInteractive.TServer.Services;
5
6 /// <summary>
7 /// Echo服务:将客户端发送的消息原样返回
8 /// </summary>
9 public partial class EchoCoreService : ServerCoreStandardServiceBase
10 {
11 [EntryPoint("echo")]
12 public async Task EchoEntryPoint()
13 {
14 var message = Json?["message"]?.GetValue<string>() ?? string.Empty;
15 await Close(new { echo = message });
16 }
17 }
Added XFEExtension.NetCore.ServerInteractive.TServer/Services/GreetCoreService.cs +23 -0
@@ -0,0 +1,23 @@
1 using System.Net;
2 using XFEExtension.NetCore.ServerInteractive.Attributes;
3 using XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
4
5 namespace XFEExtension.NetCore.ServerInteractive.TServer.Services;
6
7 /// <summary>
8 /// 问候服务:根据名称返回问候语,支持错误处理
9 /// </summary>
10 public partial class GreetCoreService : ServerCoreStandardServiceBase
11 {
12 [EntryPoint("greet")]
13 public async Task GreetEntryPoint()
14 {
15 var name = Json?["name"]?.GetValue<string>();
16 if (string.IsNullOrWhiteSpace(name))
17 {
18 await CloseWithError("名称不能为空", HttpStatusCode.BadRequest);
19 return;
20 }
21 await Close(new { greeting = $"你好, {name}!" });
22 }
23 }
Added XFEExtension.NetCore.ServerInteractive.TServer/Services/MathCoreService.cs +27 -0
@@ -0,0 +1,27 @@
1 using System.Net;
2 using XFEExtension.NetCore.ServerInteractive.Attributes;
3 using XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
4
5 namespace XFEExtension.NetCore.ServerInteractive.TServer.Services;
6
7 /// <summary>
8 /// 数学运算服务:支持加法和乘法运算
9 /// </summary>
10 public partial class MathCoreService : ServerCoreStandardServiceBase
11 {
12 [EntryPoint("math/add")]
13 public async Task AddEntryPoint()
14 {
15 var a = Json?["a"]?.GetValue<double>() ?? 0;
16 var b = Json?["b"]?.GetValue<double>() ?? 0;
17 await Close(new { result = a + b });
18 }
19
20 [EntryPoint("math/multiply")]
21 public async Task MultiplyEntryPoint()
22 {
23 var a = Json?["a"]?.GetValue<double>() ?? 0;
24 var b = Json?["b"]?.GetValue<double>() ?? 0;
25 await Close(new { result = a * b });
26 }
27 }
Added XFEExtension.NetCore.ServerInteractive.TServer/Services/StatusCoreService.cs +21 -0
@@ -0,0 +1,21 @@
1 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 using XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
3
4 namespace XFEExtension.NetCore.ServerInteractive.TServer.Services;
5
6 /// <summary>
7 /// 状态服务:返回服务器状态信息
8 /// </summary>
9 public partial class StatusCoreService : ServerCoreStandardServiceBase
10 {
11 [EntryPoint("status")]
12 public async Task StatusEntryPoint()
13 {
14 await Close(new
15 {
16 status = "running",
17 timestamp = DateTime.UtcNow,
18 version = "1.0.0"
19 });
20 }
21 }
Added XFEExtension.NetCore.ServerInteractive.TServer/Services/TimeCoreService.cs +16 -0
@@ -0,0 +1,16 @@
1 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 using XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
3
4 namespace XFEExtension.NetCore.ServerInteractive.TServer.Services;
5
6 /// <summary>
7 /// 时间服务:返回服务器当前UTC时间
8 /// </summary>
9 public partial class TimeCoreService : ServerCoreStandardServiceBase
10 {
11 [EntryPoint("time")]
12 public async Task TimeEntryPoint()
13 {
14 await Close(new { utcNow = DateTime.UtcNow.ToString("o") });
15 }
16 }
Modified XFEExtension.NetCore.ServerInteractive.Test/Program.cs +364 -36
@@ -1,4 +1,5 @@
1 using System.Text.Json;
1 using System.Net;
2 using System.Text.Json;
2 3 using XFEExtension.NetCore.ServerInteractive.Models;
3 4 using XFEExtension.NetCore.ServerInteractive.Models.RequesterModels;
4 5 using XFEExtension.NetCore.ServerInteractive.TServer.Models;
@@ -16,6 +17,40 @@ internal class Program
16 17 {
17 18 execute = "test"
18 19 }, response => response)
20 .AddRequest("echo", (_, _, parameters) => new
21 {
22 execute = "echo",
23 message = parameters.Length > 0 ? parameters[0] : string.Empty
24 }, response => response)
25 .AddRequest("math/add", (_, _, parameters) => new
26 {
27 execute = "math/add",
28 a = parameters.Length > 0 ? parameters[0] : 0,
29 b = parameters.Length > 1 ? parameters[1] : 0
30 }, response => response)
31 .AddRequest("math/multiply", (_, _, parameters) => new
32 {
33 execute = "math/multiply",
34 a = parameters.Length > 0 ? parameters[0] : 0,
35 b = parameters.Length > 1 ? parameters[1] : 0
36 }, response => response)
37 .AddRequest("status", (_, _, _) => new
38 {
39 execute = "status"
40 }, response => response)
41 .AddRequest("greet", (_, _, parameters) => new
42 {
43 execute = "greet",
44 name = parameters.Length > 0 ? parameters[0] : string.Empty
45 }, response => response)
46 .AddRequest("greet_empty", (_, _, _) => new
47 {
48 execute = "greet"
49 }, response => response)
50 .AddRequest("time", (_, _, _) => new
51 {
52 execute = "time"
53 }, response => response)
19 54 .Build(options =>
20 55 {
21 56 options.RequestAddress = "http://localhost:3305/api";
@@ -30,20 +65,18 @@ internal class Program
30 65 Console.WriteLine($"请求完成:{e.StatusCode}\t{e.Message}");
31 66 }
32 67
33 //[SMTest("Admin", "123456")]
34 public static async Task Login(string account, string password)
68 #region 基础测试
69
70 /// <summary>
71 /// 测试基本的test端点请求
72 /// </summary>
73 [SMTest]
74 public static async Task Test()
35 75 {
36 var result = await s_xFEClientRequester.Request<UserLoginResult<UserFaceInfo>>("login", account, password);
37 if (result.StatusCode == System.Net.HttpStatusCode.OK)
76 var result = await s_xFEClientRequester.Request<string>("test");
77 if (result.StatusCode == HttpStatusCode.OK)
38 78 {
39 Console.WriteLine(result.Result.Session);
40 Console.WriteLine(result.Result.ExpireDate);
41 Console.WriteLine(result.Result.UserInfo.Id);
42 Console.WriteLine(result.Result.UserInfo.NickName);
43 Console.WriteLine(result.Result.UserInfo.PermissionLevel);
44 TableRequester.RequestAddress = s_xFEClientRequester.RequestAddress;
45 TableRequester.Session = result.Result.Session;
46 TableRequester.DeviceInfo = s_xFEClientRequester.DeviceInfo;
79 Console.WriteLine(result.Result);
47 80 }
48 81 else
49 82 {
@@ -52,13 +85,16 @@ internal class Program
52 85 }
53 86 }
54 87
55 //[SMTest]
56 public static async Task TestBench()
88 /// <summary>
89 /// 测试连接检查端点
90 /// </summary>
91 [SMTest]
92 public static async Task Check()
57 93 {
58 await Parallel.ForEachAsync(Enumerable.Range(0, 100000), async (_, _) =>
94 try
59 95 {
60 var result = await s_xFEClientRequester.Request<string>("test");
61 if (result.StatusCode == System.Net.HttpStatusCode.OK)
96 var result = await s_xFEClientRequester.Request<DateTime>("check_connect");
97 if (result.StatusCode == HttpStatusCode.OK)
62 98 {
63 99 Console.WriteLine(result.Result);
64 100 }
@@ -67,16 +103,34 @@ internal class Program
67 103 Console.WriteLine(result.StatusCode);
68 104 Console.WriteLine(result.Message);
69 105 }
70 });
106 }
107 catch (Exception ex)
108 {
109 Console.WriteLine(ex);
110 }
71 111 }
72 112
73 [SMTest]
74 public static async Task Test()
113 #endregion
114
115 #region 用户登录测试
116
117 /// <summary>
118 /// 测试用户登录功能
119 /// </summary>
120 [SMTest("Admin", "123456")]
121 public static async Task Login(string account, string password)
75 122 {
76 var result = await s_xFEClientRequester.Request<string>("test");
77 if (result.StatusCode == System.Net.HttpStatusCode.OK)
123 var result = await s_xFEClientRequester.Request<UserLoginResult<UserFaceInfo>>("login", account, password);
124 if (result.StatusCode == HttpStatusCode.OK)
78 125 {
79 Console.WriteLine(result.Result);
126 Console.WriteLine(result.Result.Session);
127 Console.WriteLine(result.Result.ExpireDate);
128 Console.WriteLine(result.Result.UserInfo.Id);
129 Console.WriteLine(result.Result.UserInfo.NickName);
130 Console.WriteLine(result.Result.UserInfo.PermissionLevel);
131 TableRequester.RequestAddress = s_xFEClientRequester.RequestAddress;
132 TableRequester.Session = result.Result.Session;
133 TableRequester.DeviceInfo = s_xFEClientRequester.DeviceInfo;
80 134 }
81 135 else
82 136 {
@@ -85,11 +139,36 @@ internal class Program
85 139 }
86 140 }
87 141
142 /// <summary>
143 /// 测试无效用户登录(错误密码)
144 /// </summary>
145 [SMTest("Admin", "wrong_password")]
146 public static async Task LoginWithWrongPassword(string account, string password)
147 {
148 var result = await s_xFEClientRequester.Request<UserLoginResult<UserFaceInfo>>("login", account, password);
149 Console.WriteLine($"状态码:{result.StatusCode}");
150 Console.WriteLine($"消息:{result.Message}");
151 }
152
153 /// <summary>
154 /// 测试无效用户登录(不存在的用户)
155 /// </summary>
156 [SMTest("NonExistentUser", "123456")]
157 public static async Task LoginWithNonExistentUser(string account, string password)
158 {
159 var result = await s_xFEClientRequester.Request<UserLoginResult<UserFaceInfo>>("login", account, password);
160 Console.WriteLine($"状态码:{result.StatusCode}");
161 Console.WriteLine($"消息:{result.Message}");
162 }
163
164 /// <summary>
165 /// 测试重新登录功能
166 /// </summary>
88 167 //[SMTest]
89 168 public static async Task ReLogin()
90 169 {
91 170 var result = await s_xFEClientRequester.Request<UserFaceInfo>("relogin");
92 if (result.StatusCode == System.Net.HttpStatusCode.OK)
171 if (result.StatusCode == HttpStatusCode.OK)
93 172 {
94 173 Console.WriteLine(result.Result.Id);
95 174 Console.WriteLine(result.Result.NickName);
@@ -102,13 +181,238 @@ internal class Program
102 181 }
103 182 }
104 183
184 #endregion
185
186 #region Echo服务测试
187
188 /// <summary>
189 /// 测试Echo服务:发送消息并验证原样返回
190 /// </summary>
105 191 [SMTest]
106 public static async Task Check()
192 public static async Task EchoTest()
107 193 {
108 try
194 var result = await s_xFEClientRequester.Request<string>("echo", "Hello, XFE!");
195 if (result.StatusCode == HttpStatusCode.OK)
109 196 {
110 var result = await s_xFEClientRequester.Request<DateTime>("check_connect");
111 if (result.StatusCode == System.Net.HttpStatusCode.OK)
197 Console.WriteLine($"Echo响应:{result.Result}");
198 }
199 else
200 {
201 Console.WriteLine($"Echo失败:{result.StatusCode} {result.Message}");
202 }
203 }
204
205 /// <summary>
206 /// 测试Echo服务:发送空消息
207 /// </summary>
208 [SMTest]
209 public static async Task EchoEmptyTest()
210 {
211 var result = await s_xFEClientRequester.Request<string>("echo", string.Empty);
212 if (result.StatusCode == HttpStatusCode.OK)
213 {
214 Console.WriteLine($"空Echo响应:{result.Result}");
215 }
216 else
217 {
218 Console.WriteLine($"空Echo失败:{result.StatusCode} {result.Message}");
219 }
220 }
221
222 /// <summary>
223 /// 测试Echo服务:发送中文消息
224 /// </summary>
225 [SMTest]
226 public static async Task EchoChineseTest()
227 {
228 var result = await s_xFEClientRequester.Request<string>("echo", "你好,世界!");
229 if (result.StatusCode == HttpStatusCode.OK)
230 {
231 Console.WriteLine($"中文Echo响应:{result.Result}");
232 }
233 else
234 {
235 Console.WriteLine($"中文Echo失败:{result.StatusCode} {result.Message}");
236 }
237 }
238
239 #endregion
240
241 #region 数学运算服务测试
242
243 /// <summary>
244 /// 测试加法运算
245 /// </summary>
246 [SMTest]
247 public static async Task MathAddTest()
248 {
249 var result = await s_xFEClientRequester.Request<string>("math/add", 3.0, 5.0);
250 if (result.StatusCode == HttpStatusCode.OK)
251 {
252 Console.WriteLine($"3 + 5 = {result.Result}");
253 }
254 else
255 {
256 Console.WriteLine($"加法失败:{result.StatusCode} {result.Message}");
257 }
258 }
259
260 /// <summary>
261 /// 测试乘法运算
262 /// </summary>
263 [SMTest]
264 public static async Task MathMultiplyTest()
265 {
266 var result = await s_xFEClientRequester.Request<string>("math/multiply", 4.0, 7.0);
267 if (result.StatusCode == HttpStatusCode.OK)
268 {
269 Console.WriteLine($"4 × 7 = {result.Result}");
270 }
271 else
272 {
273 Console.WriteLine($"乘法失败:{result.StatusCode} {result.Message}");
274 }
275 }
276
277 /// <summary>
278 /// 测试加法运算:负数
279 /// </summary>
280 [SMTest]
281 public static async Task MathAddNegativeTest()
282 {
283 var result = await s_xFEClientRequester.Request<string>("math/add", -10.0, 3.0);
284 if (result.StatusCode == HttpStatusCode.OK)
285 {
286 Console.WriteLine($"-10 + 3 = {result.Result}");
287 }
288 else
289 {
290 Console.WriteLine($"负数加法失败:{result.StatusCode} {result.Message}");
291 }
292 }
293
294 /// <summary>
295 /// 测试乘法运算:零
296 /// </summary>
297 [SMTest]
298 public static async Task MathMultiplyZeroTest()
299 {
300 var result = await s_xFEClientRequester.Request<string>("math/multiply", 42.0, 0.0);
301 if (result.StatusCode == HttpStatusCode.OK)
302 {
303 Console.WriteLine($"42 × 0 = {result.Result}");
304 }
305 else
306 {
307 Console.WriteLine($"零乘法失败:{result.StatusCode} {result.Message}");
308 }
309 }
310
311 /// <summary>
312 /// 测试加法运算:小数
313 /// </summary>
314 [SMTest]
315 public static async Task MathAddDecimalTest()
316 {
317 var result = await s_xFEClientRequester.Request<string>("math/add", 1.5, 2.3);
318 if (result.StatusCode == HttpStatusCode.OK)
319 {
320 Console.WriteLine($"1.5 + 2.3 = {result.Result}");
321 }
322 else
323 {
324 Console.WriteLine($"小数加法失败:{result.StatusCode} {result.Message}");
325 }
326 }
327
328 #endregion
329
330 #region 状态服务测试
331
332 /// <summary>
333 /// 测试服务器状态端点
334 /// </summary>
335 [SMTest]
336 public static async Task StatusTest()
337 {
338 var result = await s_xFEClientRequester.Request<string>("status");
339 if (result.StatusCode == HttpStatusCode.OK)
340 {
341 Console.WriteLine($"服务器状态:{result.Result}");
342 }
343 else
344 {
345 Console.WriteLine($"状态查询失败:{result.StatusCode} {result.Message}");
346 }
347 }
348
349 #endregion
350
351 #region 问候服务测试
352
353 /// <summary>
354 /// 测试问候服务:正常名称
355 /// </summary>
356 [SMTest]
357 public static async Task GreetTest()
358 {
359 var result = await s_xFEClientRequester.Request<string>("greet", "XFEstudio");
360 if (result.StatusCode == HttpStatusCode.OK)
361 {
362 Console.WriteLine($"问候响应:{result.Result}");
363 }
364 else
365 {
366 Console.WriteLine($"问候失败:{result.StatusCode} {result.Message}");
367 }
368 }
369
370 /// <summary>
371 /// 测试问候服务:空名称(应返回错误)
372 /// </summary>
373 [SMTest]
374 public static async Task GreetEmptyNameTest()
375 {
376 var result = await s_xFEClientRequester.Request<string>("greet_empty");
377 Console.WriteLine($"空名称状态码:{result.StatusCode}");
378 Console.WriteLine($"空名称消息:{result.Message}");
379 }
380
381 #endregion
382
383 #region 时间服务测试
384
385 /// <summary>
386 /// 测试服务器时间端点
387 /// </summary>
388 [SMTest]
389 public static async Task TimeTest()
390 {
391 var result = await s_xFEClientRequester.Request<string>("time");
392 if (result.StatusCode == HttpStatusCode.OK)
393 {
394 Console.WriteLine($"服务器时间:{result.Result}");
395 }
396 else
397 {
398 Console.WriteLine($"时间查询失败:{result.StatusCode} {result.Message}");
399 }
400 }
401
402 #endregion
403
404 #region 压力测试
405
406 /// <summary>
407 /// 压力测试:并行发送100000个请求
408 /// </summary>
409 //[SMTest]
410 public static async Task TestBench()
411 {
412 await Parallel.ForEachAsync(Enumerable.Range(0, 100000), async (_, _) =>
413 {
414 var result = await s_xFEClientRequester.Request<string>("test");
415 if (result.StatusCode == HttpStatusCode.OK)
112 416 {
113 417 Console.WriteLine(result.Result);
114 418 }
@@ -117,18 +421,21 @@ internal class Program
117 421 Console.WriteLine(result.StatusCode);
118 422 Console.WriteLine(result.Message);
119 423 }
120 }
121 catch (Exception ex)
122 {
123 Console.WriteLine(ex);
124 }
424 });
125 425 }
126 426
427 #endregion
428
429 #region 日志服务测试
430
431 /// <summary>
432 /// 测试获取服务器日志
433 /// </summary>
127 434 //[SMTest]
128 435 public static async Task GetLog()
129 436 {
130 437 var result = await s_xFEClientRequester.Request<string>("get_log", DateTime.MinValue, DateTime.MaxValue);
131 if (result.StatusCode == System.Net.HttpStatusCode.OK)
438 if (result.StatusCode == HttpStatusCode.OK)
132 439 {
133 440 Console.WriteLine(result.Result);
134 441 }
@@ -139,7 +446,14 @@ internal class Program
139 446 }
140 447 }
141 448
142 //[SMTest]
449 #endregion
450
451 #region 数据表操作测试
452
453 /// <summary>
454 /// 测试Order模型序列化
455 /// </summary>
456 [SMTest]
143 457 public static string ConvertOrder()
144 458 {
145 459 var order = new Order
@@ -150,6 +464,9 @@ internal class Program
150 464 return JsonSerializer.Serialize(order);
151 465 }
152 466
467 /// <summary>
468 /// 测试添加订单
469 /// </summary>
153 470 //[SMTest]
154 471 public static async Task<bool> AddOrder() => await TableRequester.Add<Order>(new()
155 472 {
@@ -157,6 +474,9 @@ internal class Program
157 474 Name = "测试订单"
158 475 });
159 476
477 /// <summary>
478 /// 测试获取订单列表
479 /// </summary>
160 480 //[SMTest]
161 481 public static async Task GetOrder()
162 482 {
@@ -167,6 +487,9 @@ internal class Program
167 487 }
168 488 }
169 489
490 /// <summary>
491 /// 测试修改订单
492 /// </summary>
170 493 //[SMTest]
171 494 public static async Task ChangeOrder()
172 495 {
@@ -178,6 +501,9 @@ internal class Program
178 501 await TableRequester.Change(order);
179 502 }
180 503
504 /// <summary>
505 /// 测试获取修改后的订单列表
506 /// </summary>
181 507 //[SMTest]
182 508 public static async Task GetOrder2()
183 509 {
@@ -188,4 +514,6 @@ internal class Program
188 514 }
189 515 Console.ReadLine();
190 516 }
517
518 #endregion
191 519 }