单元测试:增加OnlineUsers功能单元测试,中间件增加RemoteIpAddress地址保护

This commit is contained in:
Argo-MacBookPro 2019-03-01 13:48:25 +08:00
parent a27d8bf05f
commit 5d718d1840
7 changed files with 56 additions and 19 deletions

View File

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

View File

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

View File

@ -0,0 +1,10 @@
using Xunit;
namespace Bootstrap.Admin.Api.MySql
{
[Collection("MySqlContext")]
public class OnlineTest : Api.OnlineTest
{
public OnlineTest(MySqlBAWebHost factory) : base(factory) { }
}
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,10 @@
using Xunit;
namespace Bootstrap.Admin.Api.SQLite
{
[Collection("SQLiteContext")]
public class OnlineTest : Api.OnlineTest
{
public OnlineTest(SQLiteBAWebHost factory) : base(factory) { }
}
}

View File

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