单元测试:增加OnlineUsers功能单元测试,中间件增加RemoteIpAddress地址保护
This commit is contained in:
parent
a27d8bf05f
commit
5d718d1840
|
@ -10,31 +10,17 @@ namespace Bootstrap.Admin
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class OnlineUser
|
public class OnlineUser
|
||||||
{
|
{
|
||||||
private ConcurrentQueue<KeyValuePair<DateTime, string>> _requestUrls;
|
private ConcurrentQueue<KeyValuePair<DateTime, string>> _requestUrls = new ConcurrentQueue<KeyValuePair<DateTime, string>>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ip"></param>
|
public string UserName { get; set; }
|
||||||
/// <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>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string UserName { get; }
|
public DateTime FirstAccessTime { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
public DateTime FirstAccessTime { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
|
|
@ -22,16 +22,21 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
await Task.Run(() =>
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
var onlineUsers = context.RequestServices.GetService<IOnlineUsers>();
|
var onlineUsers = context.RequestServices.GetService<IOnlineUsers>();
|
||||||
var clientIp = context.Connection.RemoteIpAddress.ToString();
|
var clientIp = context.Connection.RemoteIpAddress?.ToString() ?? "::1";
|
||||||
onlineUsers.AddOrUpdate(clientIp, key =>
|
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.Method = context.Request.Method;
|
||||||
ou.RequestUrl = context.Request.Path;
|
ou.RequestUrl = context.Request.Path;
|
||||||
ou.AddRequestUrl(context.Request.Path);
|
ou.AddRequestUrl(context.Request.Path);
|
||||||
return ou;
|
return ou;
|
||||||
}, (key, v) =>
|
}, (key, v) =>
|
||||||
{
|
{
|
||||||
|
v.UserName = context.User.Identity.Name;
|
||||||
v.LastAccessTime = DateTime.Now;
|
v.LastAccessTime = DateTime.Now;
|
||||||
v.Method = context.Request.Method;
|
v.Method = context.Request.Method;
|
||||||
v.RequestUrl = context.Request.Path;
|
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("Profiles", "个人中心")]
|
||||||
[InlineData("Exceptions", "程序异常")]
|
[InlineData("Exceptions", "程序异常")]
|
||||||
[InlineData("Messages", "站内消息")]
|
[InlineData("Messages", "站内消息")]
|
||||||
|
[InlineData("Online", "在线用户")]
|
||||||
[InlineData("Tasks", "任务管理")]
|
[InlineData("Tasks", "任务管理")]
|
||||||
[InlineData("Mobile", "客户端测试")]
|
[InlineData("Mobile", "客户端测试")]
|
||||||
public async void View_Ok(string view, string text)
|
public async void View_Ok(string view, string text)
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue