說到Webapi測試工具相信很多人想起Swagger,它可以非常方便地集成到項目中並進行項目Webapi接口測試。而BeetleX.FastHttpApi在新版本中也提供類似的插件,只需要引用這個插件就可以對BeetleX.FastHttpApi服務的接口進行測試。插件的工作方式和Swagger類似,但提供的輸入控件就相對豐富一些。
安裝插件
可能通過引用BeetleX.FastHttpApi.ApiDoc的最新版,Nunget地址如下:
https://www.nuget.org/packages/BeetleX.FastHttpApi.ApiDoc/
引用插件后可以通過HttpApiServer.Register方法把插件注冊到服務,代碼如下:
httpserver.Register(typeof(BeetleX.FastHttpApi.ApiDoc.DocController).Assembly);
注冊插件就可以通過服務的/__apidoc/就可以查看當前服務的所有Webapi信息和調用,界面如下:

左邊的菜單是Webapi列表,右邊則是測試界面。選擇一個Webapi的測試界面如下:

接下來介紹一下插件的使用和顯示擴展。
輸入控件
插件提供一些基礎的輸入控件用於提供測試時的值輸入,控件分別有:number,date,time,select,radio,checkbox,switch,remark和text;如果熟悉elementUI相信對這些控件不陌生,畢竟這個插件是使用Vue的elementUI擴展實現的。
簡單API測試
插件會自動根據接口的參數來生成輸入界面,而使用控件默認是依據參數的類型來生成(但也可以指寫后面一一介紹)。
public string Hello(string name) { return $"hello {name}"; }
以上是一個簡單的Hello接口,針對這接口插件的測試界面如下:

測試界面上部分是接口輸入部分,點擊測試則會自動調用相關Api返回的值。
多參數和標簽定義
插件支持多參數和標簽定義,通過標簽定義可以指定輸入UI的顯示和默認值的多樣性.
public bool Login([Input(Label = "用戶名")]string name, [Input(Label = "密碼")]string pwd, [Input(Label = "保存狀態", Value = true)]bool saveStatus) { return name == "admin"; }
以上是一個簡單的登陸接口,可以通過Input來指定參數的顯示名稱,默認值和使用什么類型控件顯示等。


數據提交
插件會自動分析參數對象的屬性,然后生成相關的輸入標簽
[Post] public Employee AddEmploye(Employee emp) { return emp; }
以上是一個簡單的雇員信息添加接口,插件為針對這個雇員類生成它對應的屬性列表,大致如下:

自定義標簽
為了讓參數的輸入更方便,插件提供標簽定義提供更方便的參數輸入。
public class GenderInput : InputAttribute { public GenderInput() { Type = "radio"; } public override object Data => new object[] { new { value = "男", label = "男" }, new { value = "女", label = "女" } }; }
以上是一個性別的單選標簽,輸入控件使用radio
public class HobbyInput : InputAttribute { public HobbyInput() { Type = "checkbox"; } public override object Data => new object[] { new { value = "打球", label = "打球" }, new { value = "游泳", label = "游泳" }, new { value = "爬山", label = "爬山" }, new { value = "游戲", label = "游戲" }}; }
以上是一個愛好輸入的標簽,支持多項選擇,輸入控件為checkbox;接下來把這些標簽用於注冊的數據結構中
public class RegisterDto { [Input(Label = "用戶名")] public string Name { get; set; } [Input(Label = "郵箱地址")] public string Email { get; set; } [GenderInput(Label = "性別")] public string Gender { get; set; } [CityInput(Label = "城市")] public string City { get; set; } [Input(Label = "密碼")] public string Password { get; set; } [HobbyInput(Label = "愛好")] public string[] Hobby { get; set; } } [Post] public RegisterDto Register(RegisterDto register) { return register; }
針對這個注冊的測試輸入界面如下:

動態數據標簽
在參數中很多控件存在多樣化的數據,如select,checkbox和radio等,這些控件都有存在着多數據的選擇,有很多時候這些數據都是從接口中獲取的,接下來要一下如何在標簽中定義這些控件的數據源。
public object Orders( [Input(Type = "select", DataUrl = "/EmployeeSelecter", Label = "雇員")] int id, [Input(Type = "select", DataUrl = "/CustomerSelecter", Label = "客戶")] string customerid, [Input(Label ="頁數")] int index, [SizeInput(Label ="顯示數量")] int size, IHttpContext context) { Func<Order, bool> exp = o => (id == 0 || o.EmployeeID == id) && (string.IsNullOrEmpty(customerid) || o.CustomerID == customerid); int count = DataHelper.Defalut.Orders.Count(exp); if (size == 0) size = 10; int pages = count / size; if (count % size > 0) pages++; var items = DataHelper.Defalut.Orders.Where(exp).Skip(index * size).Take(size); return items; }
以上是一個訂單查詢接口,接口中有雇員和客戶都是來源於服務數據,可能通過標簽DataUrl屬性來指定數據的來源Url(數據要求,必須包括value和label屬性)。

對於插件的使用可以查看相關示例 https://github.com/IKende/BeetleX-Samples/tree/master/Web.ApiDoc
