Gofight 是一套用
Golang 撰寫的輕量級測試工具,專門測試 Golang Web Framework API,可以參考之前一篇教學:
用 gofight 來測試 golang web API handler,在
Echo 框架發布
v3.0.0 之前,Echo 不支援 golang 標準的
http.Request
及
http.ResponseWriter
,反倒是支援
fasthttp,所以我發了
Issue 希望作者可以支援原生的 http 標準,最後沒有得到回應。就在前幾天 Echo 在 v3.0.0 版本把
fasthttp
拿掉,這樣 Gofight 就可以移除特定函示,改用原生 http。
gofight v1 寫法 (舊式)
舊版
gofight v1 針對 Echo 框架測試寫法
func TestEchoPostFormData(t *testing.T) {
r := New()
r.POST("/form").
SetBody("a=1&b=2").
RunEcho(framework.EchoEngine(), func(r EchoHTTPResponse, rq EchoHTTPRequest) {
data := []byte(r.Body.String())
a, _ := jsonparser.GetString(data, "a")
b, _ := jsonparser.GetString(data, "b")
assert.Equal(t, "1", a)
assert.Equal(t, "2", b)
assert.Equal(t, http.StatusOK, r.Status())
})
}
gofight v2 寫法 (新式)
新版
gofight v2 針對 Echo 框架測試寫法
func TestEchoPut(t *testing.T) {
r := New()
r.PUT("/update").
SetBody("c=1&d=2").
Run(framework.EchoEngine(), func(r HTTPResponse, rq HTTPRequest) {
data := []byte(r.Body.String())
c, _ := jsonparser.GetString(data, "c")
d, _ := jsonparser.GetString(data, "d")
assert.Equal(t, "1", c)
assert.Equal(t, "2", d)
assert.Equal(t, http.StatusOK, r.Code)
})
}
可以看到只要取代底下 func 就可以無痛轉換
RunEcho
=> Run
EchoHTTPResponse
=> HTTPResponse
EchoHTTPRequest
=> HTTPRequest
載入新版方式
下載 Gofight v2
$ go get gopkg.in/appleboy/gofight.v2
載入 Gofight v2
import "gopkg.in/appleboy/gofight.v2"
後記
Gofight 有用
glide 做 vendor 套件控管,但是支援 Echo 框架後,發現測試突然壞了,產生了
另外 Issue,如果是使用
go get -d -t ./...
安裝方式就沒有問題,之後再找時間解決此問題。