-
Notifications
You must be signed in to change notification settings - Fork 5
/
gin_test.go
57 lines (47 loc) · 1.13 KB
/
gin_test.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package _gin_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/morvanzhou/unittest-demo/4gin"
"github.com/stretchr/testify/assert"
)
func performRequest(r http.Handler, method, path string, body io.Reader) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, body)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestHelloWorld(t *testing.T) {
tt := []struct {
name string
in string
expected string
}{
{"test1", "human", "human"},
{"test2", "dog", "dog"},
}
// Grab our router
router := _gin.SetupRouter()
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T){
// request
reqStr := fmt.Sprintf(`{"Param": "%s"}`, tc.in)
r := strings.NewReader(reqStr)
// get response
w := performRequest(router, "POST", "/post_test", r)
assert.Equal(t, http.StatusOK, w.Code)
// verify return
var response map[string]string
err := json.Unmarshal([]byte(w.Body.String()), &response)
assert.Nil(t, err)
value, exists := response["Return"]
assert.True(t, exists)
assert.Equal(t, tc.expected, value)
})
}
}