Golang httptest包測試使用教程
當(dāng)前首次學(xué)習(xí)到Golang httptest包時,著實打動了我。其他語言測試HTTP服務(wù)需要做很多工作或引用第三方工具,讓人不可思議的是,Golang標(biāo)準(zhǔn)庫就提供了非常容易理解的測試包。本文介紹httptest包的使用,為你Go http服務(wù)構(gòu)建更好的端到端的測試。
httptest包的理念是,非常容易模擬http服務(wù),也就是說模擬響應(yīng)寫(response writer),提供給http處理器(handle),讓我們測試http服務(wù)端和客戶端很容易。
本文主要介紹兩個使用httptest的特定場景: 測試http server處理器,測試http客戶端。
測試http服務(wù)端處理器
下面通過示例介紹http server的測試。首先看http服務(wù)程序,把請求字符串轉(zhuǎn)為大寫:
package main import ( "fmt" "log" "net/http" "net/url" "strings" ) // Req: http://localhost:1234/upper?word=abc // Res: ABC func upperCaseHandler(w http.ResponseWriter, r *http.Request) { query, err := url.ParseQuery(r.URL.RawQuery) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "invalid request") return } word := query.Get("word") if len(word) == 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "missing word") return } w.WriteHeader(http.StatusOK) fmt.Fprintf(w, strings.ToUpper(word)) } func main() { http.HandleFunc("/upper", upperCaseHandler) log.Fatal(http.ListenAndServe(":1234", nil)) }
現(xiàn)在想測試http server使用的upperCaseHandler邏輯,我們需要準(zhǔn)備兩方面:
- 使用httptest.NewRequest暴露的函數(shù)創(chuàng)建http.Request對象,NewRequest返回Request, 可以傳給http.Handler進(jìn)行測試.
- 使用httptest.NewRecorder函數(shù)創(chuàng)建http.ResponseWriter,返回httptest.ResponseRecorder。ResponseRecorder是
http.ResponseWriter 的實現(xiàn),它記錄變化為了后面測試檢查.
httptest.ResponseRecorder
httptest.ResponseRecorder是 http.ResponseWriter 的實現(xiàn),可以傳給http server handle,記錄所有處理并寫回響應(yīng)的數(shù)據(jù),下面測試程序可以看到其如何實現(xiàn):
package main import ( "io/ioutil" "net/http" "net/http/httptest" "testing" ) func TestUpperCaseHandler(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/upper?word=abc", nil) w := httptest.NewRecorder() upperCaseHandler(w, req) res := w.Result() defer res.Body.Close() data, err := ioutil.ReadAll(res.Body) if err != nil { t.Errorf("expected error to be nil got %v", err) } if string(data) != "ABC" { t.Errorf("expected ABC got %v", string(data)) } }
上面示例中首先定義請求和響應(yīng),然后傳入處理器進(jìn)行測試。然后檢查ResponseRecorder的Result方法輸出:
func (rw *ResponseRecorder) Result() *http.Response
Result返回處理器生成的響應(yīng)。返回相應(yīng)至少有StatusCode, Header, Body, 以及可選其他內(nèi)容,未來可能會填充更多字段,所以調(diào)用者在測試中不應(yīng)該深度比較相等。
測試HTTP客戶端
測試服務(wù)端處理器相對容易,特別當(dāng)測試處理器邏輯時,僅需要在測試中模擬http.ResponseWriter 和 http.Request對象。對于HTTP客戶端測試,情況稍晚有點復(fù)雜。原因是有時不容易模擬或復(fù)制整個HTTP Server,請看下面示例:
package main import ( "io/ioutil" "net/http" "github.com/pkg/errors" ) type Client struct { url string } func NewClient(url string) Client { return Client{url} } func (c Client) UpperCase(word string) (string, error) { res, err := http.Get(c.url + "/upper?word=" + word) if err != nil { return "", errors.Wrap(err, "unable to complete Get request") } defer res.Body.Close() out, err := ioutil.ReadAll(res.Body) if err != nil { return "", errors.Wrap(err, "unable to read response data") } return string(out), nil }
client需要url,表示遠(yuǎn)程服務(wù)端基地址。然后調(diào)用/upper
,帶上輸入單詞,最后返回結(jié)果字符串給調(diào)用者,如果調(diào)用不成功還返回錯誤對象。為了測試這段代碼,需要模擬整個http服務(wù)端邏輯,或至少是響應(yīng)請求路徑:/upper。使用httptest包可以模擬整個http 服務(wù),通過初始化本地服務(wù),監(jiān)聽回環(huán)地址并返回你想要的任何內(nèi)容。
使用 httptest.Server
通過調(diào)用httptest.NewServer函數(shù)生成我們想要的 httptest.Server。表示http服務(wù),監(jiān)聽回環(huán)地址及可選的端口號,用于實現(xiàn)端到端HTTP測試。
func NewServer(handler http.Handler) *Server
NewServer 啟動并返回新的HTTP服務(wù),調(diào)用者使用完成后應(yīng)該調(diào)用Close方法結(jié)束服務(wù)。下面通過示例進(jìn)行解釋:
package main import ( "fmt" "net/http" "net/http/httptest" "strings" "testing" ) func TestClientUpperCase(t *testing.T) { expected := "dummy data" svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, expected) })) defer svr.Close() c := NewClient(svr.URL) res, err := c.UpperCase("anything") if err != nil { t.Errorf("expected err to be nil got %v", err) } // res: expected\r\n // due to the http protocol cleanup response res = strings.TrimSpace(res) if res != expected { t.Errorf("expected res to be %s got %s", expected, res) } }
上面示例中使用httptest.NewServer函數(shù)創(chuàng)建了模擬http服務(wù)器,給它傳入自定義模擬處理器,總是返回相同的數(shù)據(jù)。并使用服務(wù)端url作為客戶端請求url,從而模擬并讓服務(wù)端返回任何我們想測試的內(nèi)容。
當(dāng)然我們可以修改處理器,讓其返回我們期望的邏輯:
func TestClientUpperCase(t *testing.T) { svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { query, err := url.ParseQuery(r.URL.RawQuery) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "invalid request") return } word := query.Get("word") if len(word) > 0 { fmt.Fprintf(w, strings.ToUpper(word)) } else { fmt.Fprintf(w, "no input") } })) defer svr.Close() expected := "ANYTHING" c := NewClient(svr.URL) res, err := c.UpperCase("anything") if err != nil { t.Errorf("expected err to be nil got %v", err) } // res: expected\r\n // due to the http protocol cleanup response res = strings.TrimSpace(res) if res != expected { t.Errorf("expected res to be %s got %s", expected, res) } }
總結(jié)
本文介紹httptest包,可以很方便測試http服務(wù)端處理邏輯,以及模擬http服務(wù)端測試客戶端請求邏輯。由于很方面模擬,從而可以把一組參數(shù)和期望值進(jìn)行組合,循環(huán)進(jìn)行測試并對比結(jié)果,可以極大地提升測試效率。
到此這篇關(guān)于Golang httptest包測試使用教程的文章就介紹到這了,更多相關(guān)Go httptest內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go并發(fā)讀寫文件、分片寫、分片下載文件的實現(xiàn)示例
讀寫文件在很多項目中都可以用到,本文主要介紹了Go并發(fā)讀寫文件、分片寫、分片下載文件的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-01-01Golang設(shè)計模式工廠模式實戰(zhàn)寫法示例詳解
這篇文章主要為大家介紹了Golang 工廠模式實戰(zhàn)寫法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Go語言編程中判斷文件是否存在是創(chuàng)建目錄的方法
這篇文章主要介紹了Go語言編程中判斷文件是否存在是創(chuàng)建目錄的方法,示例都是使用os包下的函數(shù),需要的朋友可以參考下2015-10-10