-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrespose_test.go
88 lines (80 loc) · 2.35 KB
/
respose_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package httptest
import (
"net/http"
"reflect"
"testing"
"github.com/gavv/httpexpect/v2"
"github.com/gin-gonic/gin"
)
func TestIdKeys(t *testing.T) {
want := Responses{
{Key: "id", Value: 0, Type: "ge"},
}
t.Run("Test id keys", func(t *testing.T) {
idKeys := IdKeys()
if !reflect.DeepEqual(want, idKeys) {
t.Errorf("IdKeys want %+v but get %+v", want, idKeys)
}
})
}
func TestHttpTest(t *testing.T) {
engine := gin.New()
// Add /example route via handler function to the gin instance
handler := GinHandler(engine)
// Create httpexpect instance
e := httpexpect.WithConfig(httpexpect.Config{
Client: &http.Client{
Transport: httpexpect.NewBinder(handler),
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
Printers: []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
},
})
pageKeys := Responses{{Key: "message", Value: "OK"}, {Key: "status", Value: 200}, {Key: "data", Value: Response{Key: "message", Value: "pong"}}}
value := e.GET("/example").Expect().Status(http.StatusOK).JSON()
Test(value, pageKeys)
}
func TestHttpTestArray(t *testing.T) {
engine := gin.New()
// Add /example route via handler function to the gin instance
handler := GinHandler(engine)
// Create httpexpect instance
e := httpexpect.WithConfig(httpexpect.Config{
Client: &http.Client{
Transport: httpexpect.NewBinder(handler),
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
Printers: []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
},
})
pageKeys := []string{"1", "2"}
value := e.GET("/array").Expect().Status(http.StatusOK).JSON()
Test(value, pageKeys)
}
func TestHttpScan(t *testing.T) {
engine := gin.New()
// Add /example route via handler function to the gin instance
handler := GinHandler(engine)
// Create httpexpect instance
e := httpexpect.WithConfig(httpexpect.Config{
Client: &http.Client{
Transport: httpexpect.NewBinder(handler),
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
Printers: []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
},
})
pageKeys := Responses{{Key: "message", Value: ""}}
obj := e.GET("/example").Expect().Status(http.StatusOK).JSON().Object()
Scan(obj, pageKeys)
x := pageKeys.GetString("data.message")
if x != "pong" {
t.Errorf("Scan want get pong but get %s", x)
}
}