截取自:Spring Security OAuth2.0 認證協議【4】
測試工具:IntelliJ IDEA HTTP Client
參考
- 【⭐️⭐️】文章:官方文檔
- 文章:IntelliJ IDEA的這個接口調試工具真是太好用了!
- 文章: 學會IDEA REST Client后就可以丟掉postman了
- 視頻:IntelliJ IDEA HTTP Client 工具使用教程,以后不用 Postman 也可以
下面需要用到工具訪問寫好的接口
這里選擇用 idea 提供的 http client
【推薦:⭐️⭐️⭐️】
(不用idea的,可以用chrome的http client或者postman代替)
下面基本展示一下 idea 的 httpclient 工具的使用
發送請求
添加httpclient文件(.http后綴即可)
寫入請求內容
# 請求路徑 GET http://localhost:8080/user # 請求頭 Content-Type: application/json { # 請求體 } # 三個井號分割開兩個請求 ###
idea2019.3以后的,可以安裝插件:https://plugins.jetbrains.com/plugin/13121-http-client
(2019.3.3 版自帶)
發送請求
點擊運行鍵即可發送請求,並且在控制台看到響應
環境變量
官方文檔:https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html#example-working-with-environment-files
可能會用一些常用的變量,如:域名、用戶名、密碼、令牌等
並且這些變量會隨環境的變化而變化,如生產環境的域名和開發的域名肯定不同。
這時,我們可以把這些變量抽出放到一個文件中
創建文件:http-client.env.json
在項目路徑創建 http-client.env.json
文件,可以在里面設置環境變量
設置環境變量
這里設置
- 開發環境 “development” 的host
- 用戶測試環境 uat 的 host
- 生產環境 production 的 host
{ "development" : { "host" : "localhost:8080" }, "uat" : { "host" : "uat.vshop.cn" }, "production" : { "host" : "vshop.cn" } }
發送請求
回到 users.http
把域名該成 {{host}}
GET http://{{host}}/user Content-Type: application/json ###
選擇生產環境運行,也是可以成功獲得響應的
另外,如果有一些變量不想公開,如生產環境的賬號密碼。
可以創建文件http-client.private.env.json
,把不想公開的變量放進去,變量也會起作用。
(並且,http-client.private.env.json
的優先級大於http-client.env.json
,當變量沖突,前者的變量會覆蓋后者。)最后,把文件名放入
.gitignore
中進行排除,即可避免變量公開。
響應處理腳本(Response handler script)
官方文檔:https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html#using-response-handler-scripts
- 我們很多時候不會一個
會話
只發送一個請求
,而是在一個會話中發送多個請求。 - 並且,會根據不同響應,發送不同的請求或者請求體。
這就需要響應腳本進行處理。
剛好 idea 的 http client 提供了 響應處理腳本
的功能
腳本使用方法兩個(下圖)
- 在
.http
文件中直接寫腳本,用>{% ... %}
包裹- 直接導入
js腳本
, 用> 文件url
的方式
(這種方式,需要引入 JavaScript Library | HTTP Response Handler.)
腳本的編寫
腳本可以 javascript(ECMAScript 5.1)寫。
主要涉及到兩個類:
- client:存儲了會話(session)元數據(metadata)。
- response:存儲響應信息(content type、status、response body 等等)
【這里】API
-
client.global.set 和
{{...}}
用client.global.set("variable_name", variable )
存的數據,可以在下面的 HTTP requests 請求中用{{variable_name}}
取出來
(上面的圖片就是例子) -
client.test 和 client.assert
可以寫測試方法,並對結果進行校驗。這和我們上面寫的測試類類似
開啟測試:client.test(testName, function)
校驗結果:client.assert(condition, message)
GET https://httpbin.org/status/200 > {% client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); %}
-
其他 API
可以(ctrl+左鍵)點擊client進入源碼去看(下面是去注解簡化版)var client = new HttpClient(); var response = new HttpResponse(); function HttpClient() { this.global = new Variables(); this.test = function (testName, func) {}; this.assert = function (condition, message) {}; this.log = function (text) {}; } function Variables() { this.set = function (varName, varValue) {}; this.get = function (varName) {return varValue}; this.isEmpty = function () {return true}; /** * Removes variable 'varName'. */ this.clear = function (varName) { }; /** * Removes all variables. */ this.clearAll = function () { }; } function HttpResponse() { this.body = " "; this.headers = new ResponseHeaders(); this.status = 200; this.contentType = new ContentType } function ResponseHeaders() { this.valueOf = function (headerName) {return headerValue}; this.valuesOf = function (headerName) {return headerValue}; } function ContentType() { this.mimeType = "application/json"; this.charset = "utf-8"; }
到這里,其實你也發現了,idea的httpclient完全可以替代我們前面寫的測試代碼。
從寫代碼測試代碼的初衷來看:- 快速測試接口
- 重構時,能快速找出變化了,有問題的接口
.
是的。蘿卜青菜,各有所愛
到這里,你會發現,IntelliJ IDEA HTTP Client 的優點有
- 在同一窗口實現開發和測試
- 測試腳本可以實現串聯的接口調用,提高測試效率
- 可上傳的測試腳本,在多人協同合作的環境下,共享接口請求代碼變得非常簡單
.
人生苦短,及時行樂。