httptest
在开发中Web API接口开发完成后,若需要进行http传参与响应返回的测试,一般的做法是启动一个HTTPServer,但在这样启动慢而且不方便,那有没有什么方式不需要启动一个HTTPServer呢?Golang提供的net/http/httptest包可以实现。
httptest.NewRequest
httptest.NewRequest模拟了一个Request。和http.NewRequest相比,httptest.NewRequest默认设置了req.RemoteAddr和req.Host,并且它不会返回error。
1
2
3
4
5
req.RemoteAddr = "192.0.2.1:1234"
if req.Host == "" {
req.Host = "example.com"
}
httptest.NewRecorder
httptest.NewRecorder 方法用于创建一个http的响应记录体 *httptestResponseRecorder,它包含了接口的返回信息。它模拟了一个Response,相当于http.ResponseWriter。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// NewRecorder returns an initialized ResponseRecorder.
func NewRecorder() *ResponseRecorder {
return &ResponseRecorder{
HeaderMap: make(http.Header),
Body: new(bytes.Buffer),
Code: 200,
}
}
type ResponseRecorder struct {
Code int //HTTP response code
HeaderMap http.Header // Response.Header map
Body *bytes.Buffer //Handler's Write
Flushed bool //is whether the Handler called Flush.
result *http.Response // cache of Result's return value
snapHeader http.Header // snapshot of HeaderMap at first Write
wroteHeader bool
}
示例1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
)
func main() {
handler := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><body>Hello World!</body></html>")
}
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header.Get("Content-Type"))
fmt.Println(string(body))
}
示例2
测试gin的路由:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package demo
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/gin-gonic/gin"
)
func Router() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
return r
}
func TestHealthCheck(t *testing.T) {
router := Router()
w := httptest.NewRecorder() //记录Response
req := httptest.NewRequest(http.MethodGet, "/ping", nil) //创建一个Request
router.ServeHTTP(w, req)
got := w.Body.String()
want := "pong"
if !reflect.DeepEqual(got, want) {
t.Errorf("ping = %v, want %v", got, want)
}
}