Quantcast
Channel: 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY
Viewing all articles
Browse latest Browse all 325

輕量級 Gofight 支援 Echo 框架測試

$
0
0
Go-brown-side.sh Gofight 是一套用 Golang 撰寫的輕量級測試工具,專門測試 Golang Web Framework API,可以參考之前一篇教學: 用 gofight 來測試 golang web API handler,在 Echo 框架發布 v3.0.0 之前,Echo 不支援 golang 標準的 http.Requesthttp.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 ./... 安裝方式就沒有問題,之後再找時間解決此問題。

Viewing all articles
Browse latest Browse all 325

Trending Articles


Girasoles para colorear


mayabang Quotes, Torpe Quotes, tanga Quotes


Tagalog Quotes About Crush – Tagalog Love Quotes


OFW quotes : Pinoy Tagalog Quotes


Long Distance Relationship Tagalog Love Quotes


Tagalog Quotes To Move on and More Love Love Love Quotes


5 Tagalog Relationship Rules


Best Crush Tagalog Quotes And Sayings 2017


Re:Mutton Pies (lleechef)


FORECLOSURE OF REAL ESTATE MORTGAGE


Sapos para colorear


tagalog love Quotes – Tiwala Quotes


Break up Quotes Tagalog Love Quote – Broken Hearted Quotes Tagalog


Patama Quotes : Tagalog Inspirational Quotes


Pamatay na Banat and Mga Patama Love Quotes


Tagalog Long Distance Relationship Love Quotes


BARKADA TAGALOG QUOTES


“BAHAY KUBO HUGOT”


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 10.7.1 by Vimeo.com, Inc.