diff --git a/Bootstrap.Admin/OnlineUsers/OnlineUser.cs b/Bootstrap.Admin/OnlineUsers/OnlineUser.cs index 5e783877..690810ba 100644 --- a/Bootstrap.Admin/OnlineUsers/OnlineUser.cs +++ b/Bootstrap.Admin/OnlineUsers/OnlineUser.cs @@ -10,31 +10,17 @@ namespace Bootstrap.Admin /// public class OnlineUser { - private ConcurrentQueue> _requestUrls; + private ConcurrentQueue> _requestUrls = new ConcurrentQueue>(); /// /// /// - /// - /// - public OnlineUser(string ip, string userName) - { - Ip = ip; - UserName = userName; - FirstAccessTime = DateTime.Now; - LastAccessTime = DateTime.Now; - _requestUrls = new ConcurrentQueue>(); - } + public string UserName { get; set; } /// /// /// - public string UserName { get; } - - /// - /// - /// - public DateTime FirstAccessTime { get; } + public DateTime FirstAccessTime { get; set; } /// /// diff --git a/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs b/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs index c1783293..102c8f83 100644 --- a/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs +++ b/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs @@ -22,16 +22,21 @@ namespace Microsoft.AspNetCore.Builder await Task.Run(() => { var onlineUsers = context.RequestServices.GetService(); - var clientIp = context.Connection.RemoteIpAddress.ToString(); + var clientIp = context.Connection.RemoteIpAddress?.ToString() ?? "::1"; onlineUsers.AddOrUpdate(clientIp, key => { - var ou = new OnlineUser(key, context.User.Identity.Name); + var ou = new OnlineUser(); + ou.Ip = clientIp; + ou.UserName = context.User.Identity.Name; + ou.FirstAccessTime = DateTime.Now; + ou.LastAccessTime = DateTime.Now; ou.Method = context.Request.Method; ou.RequestUrl = context.Request.Path; ou.AddRequestUrl(context.Request.Path); return ou; }, (key, v) => { + v.UserName = context.User.Identity.Name; v.LastAccessTime = DateTime.Now; v.Method = context.Request.Method; v.RequestUrl = context.Request.Path; diff --git a/UnitTest/Bootstrap.Admin/Api/MySql/OnlineTest.cs b/UnitTest/Bootstrap.Admin/Api/MySql/OnlineTest.cs new file mode 100644 index 00000000..d79729fd --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Api/MySql/OnlineTest.cs @@ -0,0 +1,10 @@ +using Xunit; + +namespace Bootstrap.Admin.Api.MySql +{ + [Collection("MySqlContext")] + public class OnlineTest : Api.OnlineTest + { + public OnlineTest(MySqlBAWebHost factory) : base(factory) { } + } +} diff --git a/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs b/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs new file mode 100644 index 00000000..7e77fd8f --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using Xunit; + +namespace Bootstrap.Admin.Api +{ + public class OnlineTest : ControllerTest + { + public OnlineTest(BAWebHost factory) : base(factory, "api/OnlineUsers") { } + + [Fact] + public async void Post_Ok() + { + var usres = await Client.PostAsJsonAsync>(string.Empty); + Assert.Single(usres); + } + + [Fact] + public async void Get_Ok() + { + var urls = await Client.GetAsJsonAsync>>("::1"); + Assert.NotEmpty(urls); + } + } +} diff --git a/UnitTest/Bootstrap.Admin/Api/SQLite/OnlineTest.cs b/UnitTest/Bootstrap.Admin/Api/SQLite/OnlineTest.cs new file mode 100644 index 00000000..1b5733e0 --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Api/SQLite/OnlineTest.cs @@ -0,0 +1,10 @@ +using Xunit; + +namespace Bootstrap.Admin.Api.SQLite +{ + [Collection("SQLiteContext")] + public class OnlineTest : Api.OnlineTest + { + public OnlineTest(SQLiteBAWebHost factory) : base(factory) { } + } +} diff --git a/UnitTest/Bootstrap.Admin/Controllers/AdminTest.cs b/UnitTest/Bootstrap.Admin/Controllers/AdminTest.cs index eaca1494..80acdc5b 100644 --- a/UnitTest/Bootstrap.Admin/Controllers/AdminTest.cs +++ b/UnitTest/Bootstrap.Admin/Controllers/AdminTest.cs @@ -22,6 +22,7 @@ namespace Bootstrap.Admin.Controllers [InlineData("Profiles", "个人中心")] [InlineData("Exceptions", "程序异常")] [InlineData("Messages", "站内消息")] + [InlineData("Online", "在线用户")] [InlineData("Tasks", "任务管理")] [InlineData("Mobile", "客户端测试")] public async void View_Ok(string view, string text) diff --git a/UnitTest/DB/UnitTest.db b/UnitTest/DB/UnitTest.db index 1fae2f5f..75955d9b 100644 Binary files a/UnitTest/DB/UnitTest.db and b/UnitTest/DB/UnitTest.db differ