diff --git a/test/UnitTest/Bootstrap.Admin/Api/AnalyseTest.cs b/test/UnitTest/Bootstrap.Admin/Api/AnalyseTest.cs index d21be81d..f2fc5940 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/AnalyseTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/AnalyseTest.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Net.Http.Json; using Xunit; using static Bootstrap.Admin.Controllers.Api.AnalyseController; @@ -11,11 +12,11 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var cates = await Client.GetAsJsonAsync("?logType=LoginUsers"); + var cates = await Client.GetFromJsonAsync("?logType=LoginUsers"); Assert.NotNull(cates); - cates = await Client.GetAsJsonAsync("?logType=log"); + cates = await Client.GetFromJsonAsync("?logType=log"); Assert.NotNull(cates); - cates = await Client.GetAsJsonAsync("?logType=trace"); + cates = await Client.GetFromJsonAsync("?logType=trace"); Assert.NotNull(cates); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/AppsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/AppsTest.cs index c3ceb240..7c9ee4df 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/AppsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/AppsTest.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -14,7 +15,7 @@ namespace Bootstrap.Admin.Api public async void Get_Ok() { var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - var cates = await Client.GetAsJsonAsync>(rid); + var cates = await Client.GetFromJsonAsync>(rid); Assert.NotEmpty(cates); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/CategoryTest.cs b/test/UnitTest/Bootstrap.Admin/Api/CategoryTest.cs index 7a1f5306..c577f90a 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/CategoryTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/CategoryTest.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -13,21 +14,21 @@ namespace Bootstrap.Admin.Api [Fact] public async void DictCategorys_Ok() { - var cates = await Client.GetAsJsonAsync>("RetrieveDictCategorys"); + var cates = await Client.GetFromJsonAsync>("RetrieveDictCategorys"); Assert.NotEmpty(cates); } [Fact] public async void Menus_Ok() { - var cates = await Client.GetAsJsonAsync>("RetrieveMenus"); + var cates = await Client.GetFromJsonAsync>("RetrieveMenus"); Assert.NotEmpty(cates); } [Fact] public async void ParentMenus_Ok() { - var cates = await Client.GetAsJsonAsync>("RetrieveParentMenus"); + var cates = await Client.GetFromJsonAsync>("RetrieveParentMenus"); Assert.NotEmpty(cates); } @@ -35,11 +36,11 @@ namespace Bootstrap.Admin.Api public async void ValidateMenuBySubMenu_Ok() { var id = MenuHelper.RetrieveAllMenus("Admin").First(m => m.Name == "个人中心").Id; - var cates = await Client.GetAsJsonAsync($"ValidateMenuBySubMenu/{id}"); + var cates = await Client.GetFromJsonAsync($"ValidateMenuBySubMenu/{id}"); Assert.False(cates); id = MenuHelper.RetrieveAllMenus("Admin").First(m => m.Name == "后台管理").Id; - cates = await Client.GetAsJsonAsync($"ValidateMenuBySubMenu/{id}"); + cates = await Client.GetFromJsonAsync($"ValidateMenuBySubMenu/{id}"); Assert.True(cates); } @@ -47,11 +48,11 @@ namespace Bootstrap.Admin.Api public async void ValidateParentMenuById_Ok() { var id = MenuHelper.RetrieveAllMenus("Admin").First(m => m.Name == "个人中心").Id; - var cates = await Client.GetAsJsonAsync($"ValidateParentMenuById/{id}"); + var cates = await Client.GetFromJsonAsync($"ValidateParentMenuById/{id}"); Assert.True(cates); var subId = MenuHelper.RetrieveAllMenus("Admin").First(m => m.ParentId == id).Id; - cates = await Client.GetAsJsonAsync($"ValidateParentMenuById/{subId}"); + cates = await Client.GetFromJsonAsync($"ValidateParentMenuById/{subId}"); Assert.False(cates); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/DictTest.cs b/test/UnitTest/Bootstrap.Admin/Api/DictTest.cs index 7f1fdc75..2874a929 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/DictTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/DictTest.cs @@ -3,7 +3,7 @@ using Bootstrap.Security; using Longbow.Web.Mvc; using System.Collections.Generic; using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -25,7 +25,7 @@ namespace Bootstrap.Admin.Api public async void Get_Ok(string query, string order) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?sort={query}&order={order}&offset=0&limit=20&category=%E8%8F%9C%E5%8D%95&name=%E7%B3%BB%E7%BB%9F%E8%8F%9C%E5%8D%95&define=0&_=1547608210979"); + var qd = await Client.GetFromJsonAsync>($"?sort={query}&order={order}&offset=0&limit=20&category=%E8%8F%9C%E5%8D%95&name=%E7%B3%BB%E7%BB%9F%E8%8F%9C%E5%8D%95&define=0&_=1547608210979"); Assert.Single(qd.rows); } @@ -36,18 +36,20 @@ namespace Bootstrap.Admin.Api public async void Search_Ok(string search) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); + var qd = await Client.GetFromJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); Assert.NotEmpty(qd.rows); } [Fact] public async void PostAndDelete_Ok() { - var ret = await Client.PostAsJsonAsync("", new BootstrapDict() { Name = "UnitTest-Dict", Category = "UnitTest-Category", Code = "0", Define = 0 }); - Assert.True(ret); + var ret = await Client.PostAsJsonAsync("", new BootstrapDict() { Name = "UnitTest-Dict", Category = "UnitTest-Category", Code = "0", Define = 0 }); + Assert.True(ret.IsSuccessStatusCode); var ids = DictHelper.RetrieveDicts().Where(d => d.Name == "UnitTest-Dict").Select(d => d.Id); - Assert.True(await Client.DeleteAsJsonAsync, bool>("", ids)); + var resp = await Client.DeleteAsJsonAsync>("", ids); + var ret1 = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret1); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/ExceptionsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/ExceptionsTest.cs index a2135b6c..f9b04f7d 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/ExceptionsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/ExceptionsTest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; using Xunit; using static Bootstrap.Admin.Controllers.Api.ExceptionsController; @@ -22,7 +23,7 @@ namespace Bootstrap.Admin.Api // 菜单 系统菜单 系统使用条件 var query = "?sort=LogTime&order=desc&offset=0&limit=20&StartTime=&EndTime=&_=1547610349796"; - var qd = await Client.GetAsJsonAsync>(query); + var qd = await Client.GetFromJsonAsync>(query); Assert.NotEmpty(qd.rows); // clean @@ -32,13 +33,15 @@ namespace Bootstrap.Admin.Api [Fact] public async void Post_Ok() { - var files = await Client.PostAsJsonAsync>(string.Empty, ""); + var ret = await Client.PostAsJsonAsync(string.Empty, ""); + var files = await ret.Content.ReadFromJsonAsync>(); Assert.NotNull(files); var fileName = files.FirstOrDefault(); if (!string.IsNullOrEmpty(fileName)) { - var resp = await Client.PutAsJsonAsync("", new ExceptionFileQuery() { FileName = fileName }); + ret = await Client.PutAsJsonAsync("", new ExceptionFileQuery() { FileName = fileName }); + var resp = await ret.Content.ReadAsStringAsync(); Assert.NotNull(resp); } diff --git a/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs b/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs index c7cdc3e1..ffacca05 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs @@ -1,6 +1,7 @@ using Bootstrap.Admin.Controllers.Api; using System; using System.Net.Http; +using System.Net.Http.Json; using System.Reflection; using System.Threading.Tasks; using Xunit; @@ -14,28 +15,28 @@ namespace Bootstrap.Admin.Api [Fact] public async void Issues_Ok() { - var cates = await Client.GetAsJsonAsync("Issues"); + var cates = await Client.GetFromJsonAsync("Issues"); Assert.NotNull(cates); } [Fact] public async void Pulls_Ok() { - var cates = await Client.GetAsJsonAsync("Pulls"); + var cates = await Client.GetFromJsonAsync("Pulls"); Assert.NotNull(cates); } [Fact] public async void Releases_Ok() { - var cates = await Client.GetAsJsonAsync("Releases"); + var cates = await Client.GetFromJsonAsync("Releases"); Assert.NotNull(cates); } [Fact] public async void Builds_Ok() { - var cates = await Client.GetAsJsonAsync("Builds"); + var cates = await Client.GetFromJsonAsync("Builds"); Assert.NotNull(cates); } diff --git a/test/UnitTest/Bootstrap.Admin/Api/GroupsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/GroupsTest.cs index 453cdb52..7846d48b 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/GroupsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/GroupsTest.cs @@ -2,7 +2,7 @@ using Longbow.Web.Mvc; using System.Collections.Generic; using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -16,9 +16,9 @@ namespace Bootstrap.Admin.Api { // 菜单 系统菜单 系统使用条件 var query = "?sort=GroupName&order=asc&offset=0&limit=20&groupName=Admin&description=%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E7%BB%84&_=1547614230481"; - var qd = await Client.GetAsJsonAsync>(query); + await Client.GetFromJsonAsync>(query); query = "?sort=GroupName&order=desc&offset=0&limit=20&groupName=Admin&description=%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E7%BB%84&_=1547614230481"; - qd = await Client.GetAsJsonAsync>(query); + var qd = await Client.GetFromJsonAsync>(query); Assert.Single(qd.rows); } @@ -27,30 +27,34 @@ namespace Bootstrap.Admin.Api [InlineData("系统默认")] public async void Search_Ok(string search) { - var qd = await Client.GetAsJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); + var qd = await Client.GetFromJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); Assert.NotEmpty(qd.rows); } [Fact] public async void PostAndDelete_Ok() { - var ret = await Client.PostAsJsonAsync("", new Group() { GroupCode = "002", GroupName = "UnitTest-Group", Description = "UnitTest-Desc" }); - Assert.True(ret); + var ret = await Client.PostAsJsonAsync("", new Group() { GroupCode = "002", GroupName = "UnitTest-Group", Description = "UnitTest-Desc" }); + Assert.True(ret.IsSuccessStatusCode); var ids = GroupHelper.Retrieves().Where(d => d.GroupName == "UnitTest-Group").Select(d => d.Id); - Assert.True(await Client.DeleteAsJsonAsync, bool>("", ids)); + var resp = await Client.DeleteAsJsonAsync>("", ids); + var ret1 = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret1); } [Fact] public async void PostById_Ok() { var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id; - var ret = await Client.PostAsJsonAsync>($"{uid}?type=user", string.Empty); - Assert.NotEmpty(ret); + var ret = await Client.PostAsJsonAsync($"{uid}?type=user", string.Empty); + var groups = await ret.Content.ReadFromJsonAsync>(); + Assert.NotEmpty(groups); var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - ret = await Client.PostAsJsonAsync>($"{rid}?type=role", string.Empty); - Assert.NotEmpty(ret); + ret = await Client.PostAsJsonAsync($"{rid}?type=role", string.Empty); + groups = await ret.Content.ReadFromJsonAsync>(); + Assert.NotEmpty(groups); } [Fact] @@ -58,12 +62,12 @@ namespace Bootstrap.Admin.Api { var ids = GroupHelper.Retrieves().Select(g => g.Id); var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id; - var ret = await Client.PutAsJsonAsync, bool>($"{uid}?type=user", ids); - Assert.True(ret); + var ret = await Client.PutAsJsonAsync>($"{uid}?type=user", ids); + Assert.True(ret.IsSuccessStatusCode); var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - ret = await Client.PutAsJsonAsync, bool>($"{rid}?type=role", ids); - Assert.True(ret); + ret = await Client.PutAsJsonAsync>($"{rid}?type=role", ids); + Assert.True(ret.IsSuccessStatusCode); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs b/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs index 81aeb876..25064c30 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs @@ -9,15 +9,15 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using System.Net.Http; +using System.Net.Http.Json; using System.Text.Json; -using UnitTest; using Xunit; namespace Bootstrap.Admin.Api { public class HealthCheckTest : IClassFixture> { - private HttpClient client; + private readonly HttpClient client; public HealthCheckTest(HealthCheckWebHost factory) { @@ -27,7 +27,7 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var cates = await client.GetAsJsonAsync("/Healths"); + var cates = await client.GetFromJsonAsync("/Healths"); Assert.NotNull(cates); } diff --git a/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs b/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs index 88f7b95a..8cfe2211 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs @@ -1,6 +1,7 @@ using Bootstrap.Security; using System.Collections.Generic; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -12,42 +13,48 @@ namespace Bootstrap.Admin.Api [Fact] public async void RetrieveDicts_Ok() { - var ret = await Client.PostAsJsonAsync>("RetrieveDicts", ""); + var req = await Client.PostAsJsonAsync("RetrieveDicts", ""); + var ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); } [Fact] public async void RetrieveRolesByUrl_Ok() { - var ret = await Client.PostAsJsonAsync>("RetrieveRolesByUrl", "~/Admin/Index"); + var req = await Client.PostAsJsonAsync("RetrieveRolesByUrl", "~/Admin/Index"); + var ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); } [Fact] public async void RetrieveRolesByUserName_Ok() { - var ret = await Client.PostAsJsonAsync>("RetrieveRolesByUserName", "Admin"); + var req = await Client.PostAsJsonAsync("RetrieveRolesByUserName", "Admin"); + var ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); } [Fact] public async void RetrieveUserByUserName_Ok() { - var ret = await Client.PostAsJsonAsync("RetrieveUserByUserName", "Admin"); + var req = await Client.PostAsJsonAsync("RetrieveUserByUserName", "Admin"); + var ret = await req.Content.ReadFromJsonAsync(); Assert.Equal("Admin", ret.UserName); } [Fact] public async void RetrieveAppMenus_Ok() { - var ret = await Client.PostAsJsonAsync>("RetrieveAppMenus", new AppMenuOption() { AppId = "Demo", UserName = "Admin", Url = "~/Admin/Index" }); + var req = await Client.PostAsJsonAsync("RetrieveAppMenus", new AppMenuOption() { AppId = "Demo", UserName = "Admin", Url = "~/Admin/Index" }); + var ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); } [Fact] public async void Healths_Ok() { - var ret = await Client.PostAsJsonAsync("Healths", "UnitTest"); + var req = await Client.PostAsJsonAsync("Healths", "UnitTest"); + var ret = await req.Content.ReadFromJsonAsync(); Assert.False(ret); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/LoginTest.cs b/test/UnitTest/Bootstrap.Admin/Api/LoginTest.cs index 7f43f74b..f3b3a953 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/LoginTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/LoginTest.cs @@ -2,6 +2,7 @@ using Longbow.Web.Mvc; using Longbow.Web.SMS; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -13,7 +14,7 @@ namespace Bootstrap.Admin.Api [Fact] public async void Login_Get() { - var users = await Client.GetAsJsonAsync>("?sort=LoginTime&order=&offset=0&limit=20&startTime=&endTime=&loginIp=&_=1560933256621"); + var users = await Client.GetFromJsonAsync>("?sort=LoginTime&order=&offset=0&limit=20&startTime=&endTime=&loginIp=&_=1560933256621"); Assert.NotEmpty(users.rows); } @@ -51,7 +52,7 @@ namespace Bootstrap.Admin.Api public async void Option_Ok() { var req = new HttpRequestMessage(HttpMethod.Options, ""); - var resp = await Client.SendAsync(req); + await Client.SendAsync(req); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/LogsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/LogsTest.cs index 9753e788..9a93992c 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/LogsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/LogsTest.cs @@ -2,6 +2,7 @@ using Longbow.Web.Mvc; using System; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -18,7 +19,7 @@ namespace Bootstrap.Admin.Api // 菜单 系统菜单 系统使用条件 var query = "?sort=LogTime&order=desc&offset=0&limit=20&operateType=&OperateTimeStart=&OperateTimeEnd=&_=1547617573596"; - var qd = await Client.GetAsJsonAsync>(query); + var qd = await Client.GetFromJsonAsync>(query); Assert.NotEmpty(qd.rows); // clean @@ -29,8 +30,9 @@ namespace Bootstrap.Admin.Api public async void Post_Ok() { Client.DefaultRequestHeaders.Add("user-agent", "UnitTest"); - var resp = await Client.PostAsJsonAsync("", new Log() { CRUD = "UnitTest", RequestUrl = "~/UnitTest" }); - Assert.True(resp); + var resp = await Client.PostAsJsonAsync("", new Log() { CRUD = "UnitTest", RequestUrl = "~/UnitTest" }); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // clean DbManager.Create().Execute("delete from Logs where CRUD = @0", "UnitTest"); diff --git a/test/UnitTest/Bootstrap.Admin/Api/MenusTest.cs b/test/UnitTest/Bootstrap.Admin/Api/MenusTest.cs index 4e3ec848..74bd0065 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/MenusTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/MenusTest.cs @@ -4,6 +4,7 @@ using Longbow.Web.Mvc; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -29,7 +30,7 @@ namespace Bootstrap.Admin.Api [InlineData("Application", "desc")] public async void Get_Ok(string query, string order) { - var qd = await Client.GetAsJsonAsync>($"?sort={query}&order={order}&offset=0&limit=100&parentName=%E6%B5%8B%E8%AF%95%E9%A1%B5%E9%9D%A2&name=%E5%85%B3%E4%BA%8E&category=1&isresource=0&appId=Demo&_=1558235377255"); + var qd = await Client.GetFromJsonAsync>($"?sort={query}&order={order}&offset=0&limit=100&parentName=%E6%B5%8B%E8%AF%95%E9%A1%B5%E9%9D%A2&name=%E5%85%B3%E4%BA%8E&category=1&isresource=0&appId=Demo&_=1558235377255"); Assert.Single(qd.rows); } @@ -39,18 +40,21 @@ namespace Bootstrap.Admin.Api public async void Search_Ok(string search) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); + var qd = await Client.GetFromJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); Assert.NotEmpty(qd.rows); } [Fact] public async void PostAndDelete_Ok() { - var ret = await Client.PostAsJsonAsync("", new BootstrapMenu() { Name = "UnitTest-Menu", Application = "0", Category = "0", ParentId = "0", Url = "#", Target = "_self", IsResource = 0 }); + var req = await Client.PostAsJsonAsync("", new BootstrapMenu() { Name = "UnitTest-Menu", Application = "0", Category = "0", ParentId = "0", Url = "#", Target = "_self", IsResource = 0 }); + var ret = await req.Content.ReadFromJsonAsync(); Assert.True(ret); var ids = MenuHelper.RetrieveAllMenus("Admin").Where(d => d.Name == "UnitTest-Menu").Select(d => d.Id); - Assert.True(await Client.DeleteAsJsonAsync, bool>("", ids)); + var resp = await Client.DeleteAsJsonAsync>("", ids); + var ret1 = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret1); } @@ -58,11 +62,13 @@ namespace Bootstrap.Admin.Api public async void PostById_Ok() { var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id; - var ret = await Client.PostAsJsonAsync>($"{uid}?type=user", string.Empty); + var req = await Client.PostAsJsonAsync($"{uid}?type=user", string.Empty); + var ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - ret = await Client.PostAsJsonAsync>($"{rid}?type=role", string.Empty); + req = await Client.PostAsJsonAsync($"{rid}?type=role", string.Empty); + ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); } @@ -71,7 +77,8 @@ namespace Bootstrap.Admin.Api { var ids = MenuHelper.RetrieveAllMenus("Admin").Select(g => g.Id); var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - var ret = await Client.PutAsJsonAsync, bool>($"{rid}", ids); + var req = await Client.PutAsJsonAsync>($"{rid}", ids); + var ret = await req.Content.ReadFromJsonAsync(); Assert.True(ret); } diff --git a/test/UnitTest/Bootstrap.Admin/Api/MessagesTest.cs b/test/UnitTest/Bootstrap.Admin/Api/MessagesTest.cs index 782de3fd..11b79e32 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/MessagesTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/MessagesTest.cs @@ -2,6 +2,7 @@ using Bootstrap.DataAccess; using System.Collections.Generic; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -17,14 +18,14 @@ namespace Bootstrap.Admin.Api [InlineData("trash")] public async void Get_Ok(string action) { - var resp = await Client.GetAsJsonAsync>(action); + var resp = await Client.GetFromJsonAsync>(action); Assert.NotNull(resp); } [Fact] public async void GetCount_Ok() { - var resp = await Client.GetAsJsonAsync(); + var resp = await Client.GetFromJsonAsync(""); Assert.NotNull(resp); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/NewTest.cs b/test/UnitTest/Bootstrap.Admin/Api/NewTest.cs index af7066a9..5eb384c5 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/NewTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/NewTest.cs @@ -1,7 +1,7 @@ using Bootstrap.DataAccess; using System.Collections.Generic; using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -13,9 +13,9 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var nusr = InsertNewUser(); + InsertNewUser(); - var resp = await Client.GetAsJsonAsync>(); + var resp = await Client.GetFromJsonAsync>(""); Assert.NotEmpty(resp); // 删除新用户 @@ -30,8 +30,9 @@ namespace Bootstrap.Admin.Api // Approve nusr.UserStatus = UserStates.ApproveUser; - var resp = await Client.PutAsJsonAsync("", nusr); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", nusr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // 删除新用户 UserHelper.Delete(new string[] { nusr.Id }); @@ -39,14 +40,15 @@ namespace Bootstrap.Admin.Api // Reject nusr = InsertNewUser(); nusr.UserStatus = UserStates.RejectUser; - resp = await Client.PutAsJsonAsync("", nusr); - Assert.True(resp); + resp = await Client.PutAsJsonAsync("", nusr); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // 删除新用户 DeleteUnitTestUser(); } - private User InsertNewUser() + private static User InsertNewUser() { // 插入新用户 var nusr = new User() { UserName = "UnitTest_New", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" }; @@ -54,7 +56,7 @@ namespace Bootstrap.Admin.Api return nusr; } - private void DeleteUnitTestUser() + private static void DeleteUnitTestUser() { var ids = UserHelper.RetrieveNewUsers().Where(u => u.UserName == "UnitTest_New").Select(u => u.Id); UserHelper.Delete(ids); diff --git a/test/UnitTest/Bootstrap.Admin/Api/NotificationsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/NotificationsTest.cs index 314c89d6..81060aaa 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/NotificationsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/NotificationsTest.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -10,7 +11,7 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var resp = await Client.GetAsJsonAsync(); + var resp = await Client.GetFromJsonAsync(""); Assert.NotNull(resp); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs b/test/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs index efdff871..d3d49033 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/OnlineTest.cs @@ -1,7 +1,7 @@ using Longbow.Web; using System; using System.Collections.Generic; -using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -13,14 +13,14 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var users = await Client.GetAsJsonAsync>(); + var users = await Client.GetFromJsonAsync>(""); Assert.Single(users); } [Fact] public async void GetById_Ok() { - var urls = await Client.GetAsJsonAsync>>("UnitTest"); + var urls = await Client.GetFromJsonAsync>>("UnitTest"); Assert.Empty(urls); } @@ -28,11 +28,12 @@ namespace Bootstrap.Admin.Api public async void Put_Ok() { // 三次 Put 请求后返回真 - var ret = await Client.PutAsJsonAsync("", ""); - ret = await Client.PutAsJsonAsync("", ""); - ret = await Client.PutAsJsonAsync("", ""); - ret = await Client.PutAsJsonAsync("", ""); - Assert.True(ret); + await Client.PutAsJsonAsync("", ""); + await Client.PutAsJsonAsync("", ""); + await Client.PutAsJsonAsync("", ""); + var ret = await Client.PutAsJsonAsync("", ""); + var req = await ret.Content.ReadFromJsonAsync(); + Assert.True(req); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/ProfilesTest.cs b/test/UnitTest/Bootstrap.Admin/Api/ProfilesTest.cs index 67861071..fa621a63 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/ProfilesTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/ProfilesTest.cs @@ -3,7 +3,7 @@ using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; -using UnitTest; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -19,8 +19,9 @@ namespace Bootstrap.Admin.Api // change theme usr.Css = "blue.css"; usr.UserStatus = UserStates.ChangeTheme; - var resp = await Client.PutAsJsonAsync("", usr); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", usr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] @@ -31,8 +32,9 @@ namespace Bootstrap.Admin.Api usr.UserStatus = UserStates.ChangePassword; usr.NewPassword = "123789"; usr.Password = "123789"; - var resp = await Client.PutAsJsonAsync("", usr); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", usr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] @@ -42,8 +44,9 @@ namespace Bootstrap.Admin.Api // change displayname usr.UserStatus = UserStates.ChangeDisplayName; usr.DisplayName = "Administrator"; - var resp = await Client.PutAsJsonAsync("", usr); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", usr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] @@ -53,8 +56,9 @@ namespace Bootstrap.Admin.Api // change app usr.App = "UnitTest"; usr.UserStatus = UserStates.SaveApp; - var resp = await Client.PutAsJsonAsync("", usr); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", usr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] @@ -63,8 +67,8 @@ namespace Bootstrap.Admin.Api var iconFile = TestHelper.RetrievePath(string.Format("..{0}src{0}admin{0}Bootstrap.Admin{0}wwwroot{0}images{0}logo.jpg", Path.DirectorySeparatorChar)); var adminFile = TestHelper.RetrievePath(string.Format("..{0}src{0}admin{0}Bootstrap.Admin{0}wwwroot{0}images{0}uploader{0}Admin.jpg", Path.DirectorySeparatorChar)); var fi = new FileInfo(iconFile); - string fileName = fi.Name; - byte[] fileContents = File.ReadAllBytes(fi.FullName); + var fileName = fi.Name; + var fileContents = File.ReadAllBytes(fi.FullName); var loginContent = new MultipartFormDataContent(); var byteArrayContent = new ByteArrayContent(fileContents); @@ -78,8 +82,8 @@ namespace Bootstrap.Admin.Api // delete file var delContent = new StringContent("key=Admin.jpg"); delContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); - req = await Client.PostAsync("del", delContent); - req = await Client.PostAsync("Delete", delContent); + await Client.PostAsync("del", delContent); + await Client.PostAsync("Delete", delContent); Assert.False(File.Exists(adminFile)); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/RegisterTest.cs b/test/UnitTest/Bootstrap.Admin/Api/RegisterTest.cs index c596cb8c..f703aa36 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/RegisterTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/RegisterTest.cs @@ -1,6 +1,6 @@ using Bootstrap.DataAccess; using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -12,9 +12,9 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var resp = await Client.GetAsJsonAsync("?userName=Admin"); + var resp = await Client.GetFromJsonAsync("?userName=Admin"); Assert.False(resp); - resp = await Client.GetAsJsonAsync("?userName=Admin1"); + resp = await Client.GetFromJsonAsync("?userName=Admin1"); Assert.True(resp); } @@ -23,10 +23,12 @@ namespace Bootstrap.Admin.Api { // register new user var nusr = new User() { UserName = "U_Register", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" }; - var resp = await Client.PostAsJsonAsync("", nusr); - Assert.True(resp); - resp = await Client.GetAsJsonAsync($"?userName={nusr.UserName}"); - Assert.False(resp); + var resp = await Client.PostAsJsonAsync("", nusr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); + + ret = await Client.GetFromJsonAsync($"?userName={nusr.UserName}"); + Assert.False(ret); UserHelper.Delete(nusr.RetrieveNewUsers().Where(u => u.UserName == nusr.UserName).Select(u => u.Id)); } @@ -34,21 +36,24 @@ namespace Bootstrap.Admin.Api public async void Put_Ok() { var user = new ResetUser() { DisplayName = "UnitTest", UserName = "UnitTest", Reason = "UnitTest" }; - var resp = await Client.PutAsJsonAsync("", user); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", user); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] public async void Put_UserName() { var user = new User() { Password = "1" }; - var resp = await Client.PutAsJsonAsync("UnitTest", user); - Assert.False(resp); + var resp = await Client.PutAsJsonAsync("UnitTest", user); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.False(ret); // 重置Admin密码 - await Client.PutAsJsonAsync("", new ResetUser { UserName = "Admin", DisplayName = "Administrator", Reason = "UnitTest" }); - resp = await Client.PutAsJsonAsync("Admin", new User() { Password = "123789" }); - Assert.True(resp); + await Client.PutAsJsonAsync("", new ResetUser { UserName = "Admin", DisplayName = "Administrator", Reason = "UnitTest" }); + resp = await Client.PutAsJsonAsync("Admin", new User() { Password = "123789" }); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/RolesTest.cs b/test/UnitTest/Bootstrap.Admin/Api/RolesTest.cs index ed798c20..d830c4e2 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/RolesTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/RolesTest.cs @@ -3,6 +3,7 @@ using Longbow.Web.Mvc; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -17,7 +18,7 @@ namespace Bootstrap.Admin.Api public async void Get_Ok(string query, string order) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?sort={query}&order={order}&offset=0&limit=20&roleName=Administrators&description=%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%91%98&_=1547625202230"); + var qd = await Client.GetFromJsonAsync>($"?sort={query}&order={order}&offset=0&limit=20&roleName=Administrators&description=%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%91%98&_=1547625202230"); Assert.Single(qd.rows); } @@ -27,18 +28,21 @@ namespace Bootstrap.Admin.Api public async void Search_Ok(string search) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); + var qd = await Client.GetFromJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); Assert.NotEmpty(qd.rows); } [Fact] public async void PostAndDelete_Ok() { - var ret = await Client.PostAsJsonAsync("", new Role() { RoleName = "UnitTest-Role", Description = "UnitTest-Desc" }); + var resp = await Client.PostAsJsonAsync("", new Role() { RoleName = "UnitTest-Role", Description = "UnitTest-Desc" }); + var ret = await resp.Content.ReadFromJsonAsync(); Assert.True(ret); var ids = RoleHelper.Retrieves().Where(d => d.RoleName == "UnitTest-Role").Select(d => d.Id); - Assert.True(await Client.DeleteAsJsonAsync, bool>("", ids)); + resp = await Client.DeleteAsJsonAsync>("", ids); + var ret1 = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret1); } [Fact] @@ -48,14 +52,15 @@ namespace Bootstrap.Admin.Api var gid = GroupHelper.Retrieves().Where(g => g.GroupName == "Admin").First().Id; var mid = MenuHelper.RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id; - var ret = await Client.PostAsJsonAsync>($"{uid}?type=user", string.Empty); + var req = await Client.PostAsJsonAsync($"{uid}?type=user", string.Empty); + var ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); - ret = await Client.PostAsJsonAsync>($"{gid}?type=group", string.Empty); - Assert.NotEmpty(ret); + req = await Client.PostAsJsonAsync($"{gid}?type=group", string.Empty); + ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); - ret = await Client.PostAsJsonAsync>($"{mid}?type=menu", string.Empty); - Assert.NotEmpty(ret); + req = await Client.PostAsJsonAsync($"{mid}?type=menu", string.Empty); + ret = await req.Content.ReadFromJsonAsync>(); Assert.NotEmpty(ret); } [Fact] @@ -66,17 +71,18 @@ namespace Bootstrap.Admin.Api var mid = MenuHelper.RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id; var ids = RoleHelper.Retrieves().Select(r => r.Id); - var ret = await Client.PutAsJsonAsync, bool>($"{uid}?type=user", ids); + var req = await Client.PutAsJsonAsync>($"{uid}?type=user", ids); + var ret = await req.Content.ReadFromJsonAsync(); Assert.True(ret); - ret = await Client.PutAsJsonAsync, bool>($"{gid}?type=group", ids); - Assert.True(ret); + req = await Client.PutAsJsonAsync>($"{gid}?type=group", ids); + ret = await req.Content.ReadFromJsonAsync(); Assert.True(ret); - ret = await Client.PutAsJsonAsync, bool>($"{mid}?type=menu", ids); - Assert.True(ret); + req = await Client.PutAsJsonAsync>($"{mid}?type=menu", ids); + ret = await req.Content.ReadFromJsonAsync(); Assert.True(ret); - ret = await Client.PutAsJsonAsync, bool>($"{mid}?type=app", ids); - Assert.True(ret); + req = await Client.PutAsJsonAsync>($"{mid}?type=app", ids); + ret = await req.Content.ReadFromJsonAsync(); Assert.True(ret); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/SQLTest.cs b/test/UnitTest/Bootstrap.Admin/Api/SQLTest.cs index adba2c9c..afb5c99d 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/SQLTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/SQLTest.cs @@ -1,6 +1,7 @@ using Bootstrap.DataAccess; using Longbow.Web.Mvc; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -12,7 +13,7 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var resp = await Client.GetAsJsonAsync>("?userName=Admin&OperateTimeStart=&OperateTimeEnd="); + var resp = await Client.GetFromJsonAsync>("?userName=Admin&OperateTimeStart=&OperateTimeEnd="); Assert.NotNull(resp); } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs b/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs index 5c73bfb8..a7d552d4 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/SettingsTest.cs @@ -5,6 +5,7 @@ using Longbow.Cache; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -16,14 +17,14 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var resp = await Client.GetAsJsonAsync>(); + var resp = await Client.GetFromJsonAsync>(""); Assert.NotNull(resp); } [Fact] public async void GetByKey_Ok() { - var resp = await Client.GetAsJsonAsync("Demo"); + var resp = await Client.GetFromJsonAsync("Demo"); Assert.NotNull(resp); } @@ -40,30 +41,33 @@ namespace Bootstrap.Admin.Api AppFooter = "网站页脚" }; - var resp = await Client.PutAsJsonAsync("", data); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("", data); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // Check - var op = await Client.GetAsJsonAsync(data.AppCode); + var op = await Client.GetFromJsonAsync(data.AppCode); Assert.Equal(data.AppTitle, op.AppTitle); // update data.AppId = "edit"; data.AppUrl = "http://UnitTest"; - resp = await Client.PutAsJsonAsync("", data); - Assert.True(resp); + resp = await Client.PutAsJsonAsync("", data); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); - op = await Client.GetAsJsonAsync(data.AppCode); + op = await Client.GetFromJsonAsync(data.AppCode); Assert.Equal(data.AppUrl, op.AppUrl); // 删除 - resp = await Client.DeleteAsJsonAsync("AppPath", new BootstrapDict() + resp = await Client.DeleteAsJsonAsync("AppPath", new BootstrapDict() { Category = data.AppName, Name = data.AppName, Code = data.AppCode }); - Assert.True(resp); + var ret1 = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret1); } [Fact] @@ -77,10 +81,11 @@ namespace Bootstrap.Admin.Api Assert.True(DictHelper.Save(new BootstrapDict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "0", Define = 0 })); // 调用 Settings webapi - var resp = await Client.PostAsJsonAsync, bool>("", new BootstrapDict[]{ + var resp = await Client.PostAsJsonAsync>("", new BootstrapDict[]{ new BootstrapDict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "UnitTest" } }); - Assert.True(resp); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // 由于 SaveUISettings 函数保护功能,上一步保存成功,但是未更改 Code 值 var code = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "UnitTest-Settings").Code; @@ -95,11 +100,13 @@ namespace Bootstrap.Admin.Api public async void Post_Id_Ok() { // Demo - var resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "UnitTest", Code = "0" }); - Assert.False(resp); + var resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "UnitTest", Code = "0" }); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.False(ret); - resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "123789", Code = "0" }); - Assert.True(resp); + resp = await Client.PostAsJsonAsync("Demo", new BootstrapDict() { Name = "123789", Code = "0" }); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } internal class CacheCorsItem : ICacheCorsItem diff --git a/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs b/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs index 2edde574..ce3ca526 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs @@ -1,6 +1,7 @@ using Bootstrap.DataAccess; using System.Collections.Generic; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -12,11 +13,11 @@ namespace Bootstrap.Admin.Api [Fact] public async void Get_Ok() { - var resp = await Client.GetAsJsonAsync>(); + var resp = await Client.GetFromJsonAsync>(""); Assert.NotNull(resp); // receive log - var recv = await Client.GetAsJsonAsync("/api/TasksLog?name=周期任务"); + var recv = await Client.GetFromJsonAsync("/api/TasksLog?name=周期任务"); Assert.True(recv); // for test SignalRManager.SendTaskLog @@ -26,12 +27,14 @@ namespace Bootstrap.Admin.Api [Fact] public async void Put_Ok() { - var resp = await Client.PutAsJsonAsync("/api/Tasks/SQL日志?operType=pause", ""); - Assert.True(resp); + var resp = await Client.PutAsJsonAsync("/api/Tasks/SQL日志?operType=pause", ""); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // receive log - var recv = await Client.PutAsJsonAsync("/api/Tasks/SQL日志?operType=run", ""); - Assert.True(recv); + var recv = await Client.PutAsJsonAsync("/api/Tasks/SQL日志?operType=run", ""); + ret = await recv.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] @@ -40,23 +43,27 @@ namespace Bootstrap.Admin.Api var widget = new TaskWidget(); // widget Cron 表达式为 ”“ - var resp = await Client.PostAsJsonAsync("/api/Tasks", widget); - Assert.False(resp); + var resp = await Client.PostAsJsonAsync("/api/Tasks", widget); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.False(ret); // task executor 不合法 widget.CronExpression = Longbow.Tasks.Cron.Secondly(5); widget.TaskExecutorName = "UnitTest-Widget"; - resp = await Client.PostAsJsonAsync("/api/Tasks", widget); - Assert.False(resp); + resp = await Client.PostAsJsonAsync("/api/Tasks", widget); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.False(ret); widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor"; widget.Name = "UnitTest-Task"; - resp = await Client.PostAsJsonAsync("/api/Tasks", widget); - Assert.True(resp); + resp = await Client.PostAsJsonAsync("/api/Tasks", widget); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); // Delete - resp = await Client.DeleteAsJsonAsync, bool>("/api/Tasks", new string[] { widget.Name }); - Assert.True(resp); + resp = await Client.DeleteAsJsonAsync>("/api/Tasks", new string[] { widget.Name }); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } [Fact] @@ -70,7 +77,7 @@ namespace Bootstrap.Admin.Api [Collection("SystemModel")] public class TasksSystemModelTest { - private HttpClient client; + private readonly HttpClient client; public TasksSystemModelTest(BASystemModelWebHost factory) { @@ -80,24 +87,30 @@ namespace Bootstrap.Admin.Api [Fact] public async void Post_Ok() { - var widget = new TaskWidget(); - widget.CronExpression = Longbow.Tasks.Cron.Secondly(5); - widget.Name = "单次任务"; - widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor"; + var widget = new TaskWidget + { + CronExpression = Longbow.Tasks.Cron.Secondly(5), + Name = "单次任务", + TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor" + }; // 演示模式下禁止移除系统内置任务 - var resp = await client.PostAsJsonAsync("/api/Tasks", widget); - Assert.False(resp); + var resp = await client.PostAsJsonAsync("/api/Tasks", widget); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.False(ret); - resp = await client.DeleteAsJsonAsync, bool>("/api/Tasks", new string[] { widget.Name }); - Assert.False(resp); + resp = await client.DeleteAsJsonAsync>("/api/Tasks", new string[] { widget.Name }); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.False(ret); widget.Name = "Test-Widget"; - resp = await client.PostAsJsonAsync("/api/Tasks", widget); - Assert.True(resp); + resp = await client.PostAsJsonAsync("/api/Tasks", widget); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); - resp = await client.DeleteAsJsonAsync, bool>("/api/Tasks", new string[] { widget.Name }); - Assert.True(resp); + resp = await client.DeleteAsJsonAsync>("/api/Tasks", new string[] { widget.Name }); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/TracesTest.cs b/test/UnitTest/Bootstrap.Admin/Api/TracesTest.cs index 4e23e697..07479c0f 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/TracesTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/TracesTest.cs @@ -3,6 +3,7 @@ using Longbow.Web; using Longbow.Web.Mvc; using System; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -19,7 +20,7 @@ namespace Bootstrap.Admin.Api // 菜单 系统菜单 系统使用条件 var query = "?sort=LogTime&order=desc&offset=0&limit=20&operateType=&OperateTimeStart=&OperateTimeEnd=&AccessIP=&_=1547617573596"; - var qd = await Client.GetAsJsonAsync>(query); + var qd = await Client.GetFromJsonAsync>(query); Assert.NotEmpty(qd.rows); // clean @@ -39,8 +40,9 @@ namespace Bootstrap.Admin.Api OS = "UniTest", UserAgent = "UniTest" }; - var result = await Client.PostAsJsonAsync("", onlineUser); - Assert.True(result); + var result = await Client.PostAsJsonAsync("", onlineUser); + var ret = await result.Content.ReadFromJsonAsync(); + Assert.True(ret); } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/UsersTest.cs b/test/UnitTest/Bootstrap.Admin/Api/UsersTest.cs index a1d9386a..9fa4b536 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/UsersTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/UsersTest.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Json; using Xunit; namespace Bootstrap.Admin.Api @@ -35,7 +36,7 @@ namespace Bootstrap.Admin.Api public async void Get_Ok(string query, string order) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?sort={query}&order={order}&offset=0&limit=20&name=Admin&displayName=Administrator&_=1547628247338"); + var qd = await Client.GetFromJsonAsync>($"?sort={query}&order={order}&offset=0&limit=20&name=Admin&displayName=Administrator&_=1547628247338"); Assert.Single(qd.rows); } @@ -46,7 +47,7 @@ namespace Bootstrap.Admin.Api public async void Search_Ok(string search) { // 菜单 系统菜单 系统使用条件 - var qd = await Client.GetAsJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); + var qd = await Client.GetFromJsonAsync>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979"); Assert.NotEmpty(qd.rows); } @@ -56,15 +57,19 @@ namespace Bootstrap.Admin.Api UserHelper.Delete(UserHelper.Retrieves().Where(usr => usr.UserName == "UnitTest_Delete").Select(usr => usr.Id)); var nusr = new User { UserName = "UnitTest_Delete", Password = "1", DisplayName = "DisplayName", ApprovedBy = "System", ApprovedTime = DateTime.Now, Description = "Desc", Icon = "default.jpg" }; - var resp = await Client.PostAsJsonAsync("", nusr); - Assert.True(resp); + var resp = await Client.PostAsJsonAsync("", nusr); + var ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); nusr.Id = UserHelper.Retrieves().First(u => u.UserName == nusr.UserName).Id; - resp = await Client.PostAsJsonAsync("", nusr); - Assert.True(resp); + resp = await Client.PostAsJsonAsync("", nusr); + ret = await resp.Content.ReadFromJsonAsync(); + Assert.True(ret); var ids = UserHelper.Retrieves().Where(d => d.UserName == nusr.UserName).Select(d => d.Id); - Assert.True(await Client.DeleteAsJsonAsync, bool>("", ids)); + var resp1 = await Client.DeleteAsJsonAsync>("", ids); + var ret1 = await resp1.Content.ReadFromJsonAsync(); + Assert.True(ret1); } [Fact] @@ -72,11 +77,13 @@ namespace Bootstrap.Admin.Api { var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - var ret = await Client.PostAsJsonAsync>($"{rid}?type=role", string.Empty); + var resp = await Client.PostAsJsonAsync($"{rid}?type=role", string.Empty); + var ret = await resp.Content.ReadFromJsonAsync>(); Assert.NotNull(ret); var gid = GroupHelper.Retrieves().Where(r => r.GroupName == "Admin").First().Id; - ret = await Client.PostAsJsonAsync>($"{gid}?type=group", string.Empty); + resp = await Client.PostAsJsonAsync($"{gid}?type=group", string.Empty); + ret = await resp.Content.ReadFromJsonAsync>(); Assert.NotNull(ret); // 创建用户 @@ -87,7 +94,8 @@ namespace Bootstrap.Admin.Api UserHelper.ForgotPassword(new ResetUser() { DisplayName = nusr.DisplayName, Reason = "UnitTest", ResetTime = DateTime.Now, UserName = nusr.UserName }); // 重置操作 - ret = await Client.PostAsJsonAsync>($"{nusr.UserName}?type=reset", string.Empty); + resp = await Client.PostAsJsonAsync($"{nusr.UserName}?type=reset", string.Empty); + ret = await resp.Content.ReadFromJsonAsync>(); Assert.NotNull(ret); UserHelper.Delete(UserHelper.Retrieves().Where(usr => usr.UserName == nusr.UserName).Select(usr => usr.Id)); @@ -98,11 +106,13 @@ namespace Bootstrap.Admin.Api { var ids = UserHelper.Retrieves().Where(u => u.UserName == "Admin").Select(u => u.Id); var gid = GroupHelper.Retrieves().Where(r => r.GroupName == "Admin").First().Id; - var ret = await Client.PutAsJsonAsync, bool>($"{gid}?type=group", ids); + var resp = await Client.PutAsJsonAsync>($"{gid}?type=group", ids); + var ret = await resp.Content.ReadFromJsonAsync(); Assert.True(ret); var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id; - ret = await Client.PutAsJsonAsync, bool>($"{rid}?type=role", ids); + resp = await Client.PutAsJsonAsync>($"{rid}?type=role", ids); + ret = await resp.Content.ReadFromJsonAsync(); Assert.True(ret); } } diff --git a/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs b/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs index 6bfaf769..3bcab90c 100644 --- a/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs +++ b/test/UnitTest/Bootstrap.Admin/HttpClientExtensions.cs @@ -1,4 +1,7 @@ -using System.Net.Http; +using Microsoft.Extensions.DependencyInjection; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text.Json; using System.Threading.Tasks; namespace Bootstrap.Admin @@ -34,5 +37,25 @@ namespace Bootstrap.Admin }; await client.PostAsync("/Account/Login", content); } + + /// + /// + /// + /// + /// + /// + /// + /// + public static async Task DeleteAsJsonAsync(this HttpClient client, string requestUri, TValue value) + { + var options = new JsonSerializerOptions().AddDefaultConverters(); + var req = new HttpRequestMessage(HttpMethod.Delete, requestUri) + { + Content = new StringContent(JsonSerializer.Serialize(value, options)) + }; + req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + + return await client.SendAsync(req); + } } } diff --git a/test/UnitTest/Bootstrap.DataAccess/DictsTest.cs b/test/UnitTest/Bootstrap.DataAccess/DictsTest.cs index 5ae125a9..52c03bf2 100644 --- a/test/UnitTest/Bootstrap.DataAccess/DictsTest.cs +++ b/test/UnitTest/Bootstrap.DataAccess/DictsTest.cs @@ -186,11 +186,11 @@ namespace Bootstrap.DataAccess using var client = new HttpClient(); // 日本东京 - var locator = await client.GetAsJsonAsync($"{ipUri}207.148.111.94"); + var locator = await client.GetFromJsonAsync($"{ipUri}207.148.111.94"); Assert.NotEqual(0, locator.Status); // 四川成都 - locator = await client.GetAsJsonAsync($"{ipUri}182.148.123.196"); + locator = await client.GetFromJsonAsync($"{ipUri}182.148.123.196"); Assert.Equal(0, locator.Status); } @@ -201,11 +201,11 @@ namespace Bootstrap.DataAccess // 日本东京 using var client = new HttpClient(); - var locator = await client.GetAsJsonAsync($"{ipUri}207.148.111.94"); + var locator = await client.GetFromJsonAsync($"{ipUri}207.148.111.94"); Assert.Contains(new int[] { 0, 10012 }, c => c == locator.Error_Code); // 四川成都 - locator = await client.GetAsJsonAsync($"{ipUri}182.148.123.196"); + locator = await client.GetFromJsonAsync($"{ipUri}182.148.123.196"); Assert.Contains(new int[] { 0, 10012 }, c => c == locator.Error_Code); } @@ -222,7 +222,7 @@ namespace Bootstrap.DataAccess Assert.Equal("0", locator.Status); // 四川成都 - locator = await client.GetAsJsonAsync($"{ipUri}182.148.123.196"); + locator = await client.GetFromJsonAsync($"{ipUri}182.148.123.196"); Assert.Equal("0", locator.Status); }