单元测试:增加OnlineUsers功能单元测试,中间件增加RemoteIpAddress地址保护
This commit is contained in:
parent
a27d8bf05f
commit
5d718d1840
|
@ -10,31 +10,17 @@ namespace Bootstrap.Admin
|
|||
/// </summary>
|
||||
public class OnlineUser
|
||||
{
|
||||
private ConcurrentQueue<KeyValuePair<DateTime, string>> _requestUrls;
|
||||
private ConcurrentQueue<KeyValuePair<DateTime, string>> _requestUrls = new ConcurrentQueue<KeyValuePair<DateTime, string>>();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="userName"></param>
|
||||
public OnlineUser(string ip, string userName)
|
||||
{
|
||||
Ip = ip;
|
||||
UserName = userName;
|
||||
FirstAccessTime = DateTime.Now;
|
||||
LastAccessTime = DateTime.Now;
|
||||
_requestUrls = new ConcurrentQueue<KeyValuePair<DateTime, string>>();
|
||||
}
|
||||
public string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string UserName { get; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public DateTime FirstAccessTime { get; }
|
||||
public DateTime FirstAccessTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
|
|
@ -22,16 +22,21 @@ namespace Microsoft.AspNetCore.Builder
|
|||
await Task.Run(() =>
|
||||
{
|
||||
var onlineUsers = context.RequestServices.GetService<IOnlineUsers>();
|
||||
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;
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
using Xunit;
|
||||
|
||||
namespace Bootstrap.Admin.Api.MySql
|
||||
{
|
||||
[Collection("MySqlContext")]
|
||||
public class OnlineTest : Api.OnlineTest
|
||||
{
|
||||
public OnlineTest(MySqlBAWebHost factory) : base(factory) { }
|
||||
}
|
||||
}
|
|
@ -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, IEnumerable<OnlineUser>>(string.Empty);
|
||||
Assert.Single(usres);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Get_Ok()
|
||||
{
|
||||
var urls = await Client.GetAsJsonAsync<IEnumerable<KeyValuePair<DateTime, string>>>("::1");
|
||||
Assert.NotEmpty(urls);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using Xunit;
|
||||
|
||||
namespace Bootstrap.Admin.Api.SQLite
|
||||
{
|
||||
[Collection("SQLiteContext")]
|
||||
public class OnlineTest : Api.OnlineTest
|
||||
{
|
||||
public OnlineTest(SQLiteBAWebHost factory) : base(factory) { }
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue