Table of contents
1 Bruno 簡介
Bruno 同 Postman、Insomnia、HTTPie Desktop 之類既帶有 UI 既 HTTP API 測試軟件一樣,都可以幫我地測試 HTTP APIs,亦提供 pre-request、test 既功能。
唔同既係,佢有以下既特點:
- 只有離線模式,唔打算提供 cloud sync 功能,唔似 Postman 咁係咁迫我地註冊帳號。
- 完全適合 version control。
- 一改完就已經保存左落本地檔案裡面,唔洗好似 Postman 咁要下下都 export collection。
- 每一個 request 都對應一個
.bru
檔案,格式係佢自己既 Bru markup language,係一隻好易理解既 markup language。
- 唔似得 Postman 既 JSON 格式咁改完好難用 Git diff。
- 冇 Postman 冇意義既 workspace 概念。
- 有 portable 版本,唔似得 Postman 咁一定要安裝程式。
2 安裝
我地可以下載 portable 版本。
下載網址:
3 使用
3.1 建立 collection
左上角 Collections > Create Collection。
我地需要揀選 local file system 既一個 folder,Bruno 會將我地之後建立既 HTTP requests 都保存喺呢個 folder 度。
3.2 建立 environment
建立完 collection 之後,我地就可以建立 environment。
Bruno 既 environments 係跟住個 collection 既。
右上角 No Environment > Configure > Create,之後可以加:
Key | Value |
---|
HOST | http://localhost:8080 |
3.3 建立 HTTP requests
因為我地已經有 HOST
既 environment variable,咁我地既 request URL 就可以咁寫:
{{HOST}}/api/greeting
3.3.1 Pre-request script
我地可以用 pre-request script 黎生成 request 數據。
例如 request body 裡面既 JSON fields 想用隨機數據既話,可以咁寫:
{
"a": "{{a}}",
"b": "{{b}}"
}
然後 Script > Pre Request 可以寫 JavaScript:
bru.setVar("a", require("uuid").v4());
bru.setVar("b", require("nanoid").nanoid());
3.3.2 Post-response script
我地可以用 post-response script 黎將 response 既數據儲存喺 environment 既 variables 再用喺其他 requests,例如喺需要用 OAuth 拎左 access token 再用 Bearer token 做 authorization 去 call 受保護既 HTTP APIs 既情況下就可以用呢個方法。
Script > Post Response 可以寫 JavaScript:
bru.setEnvVar("AUTH_TOKEN", res.body.access_token);
3.3.3 Assert
如果想簡單咁測試下 HTTP response body 既 JSON fields,我地可以用 assert。
Bruno 提供左 res
object,裡面有幾個有用既 fields:
res.status
res.headers
res.body
Assert 支援唔同既 operators,包括 equals
、length
、matches
、isString
等等。
Assert 既 values 亦可以填 variables。
3.3.4 Tests
如果 assert 功能唔夠用,咁其實 Bruno 同 Postman 一樣,都可以用內置既 Chai.js 黎寫 test cases。
測試 HTTP response status code:
test("should be 200", function () {
expect(res.status).to.equal(200);
});
測試 HTTP response body 既 fields:
test("fields should be strings", function () {
expect(res.body.a).to.eq(bru.getVar("a"));
expect(res.body.b).to.eq(bru.getVar("b"));
});
我地可以用返 pre-request script 度 declare 既 variables 黎測試 response body。
3.4 保存/匯出 collection
因為當我地建立新 requests 既時候,Bruno 已經會幫我地保存落檔案裡面,所以我地唔需要任何匯出操作。
Folder structure 會係咁:
.bru
檔案格式大概係咁:
1meta {
2 name: Get Data
3 type: http
4 seq: 2
5}
6
7get {
8 url: {{HOST}}/test
9 body: none
10 auth: none
11}
12
13assert {
14 res.status: eq 200
15 res.body.a: eq {{a}}
16 res.body.b: eq {{b}}
17}
18
19tests {
20 test("fields should match variables", function () {
21 expect(res.body.a).to.eq(bru.getVar("a"));
22 expect(res.body.b).to.eq(bru.getVar("b"));
23 });
24}
3.5 打開/匯入 collection
我地只需要去左上角 Collections > Open Collection。
3.6 Collection 整體功能
3.6.1 Collection tests
有啲 common 既 tests 可以喺個 collection 上面寫,咁就唔洗每個 request 都重複寫一次。
3.6.2 執行 collection 既所有 requests
Right click 個 collection > Settings > 右上角 Runner。
3.6.3 查看 variables
我地有時候可能需要睇下而家既 variable values 係乜野。
Right click 個 collection > Settings > 右上角 Variables。
4 參考資料