-
Notifications
You must be signed in to change notification settings - Fork 1
/
mw_test.go
171 lines (154 loc) · 4.36 KB
/
mw_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ginfmt
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/sleagon/ginfmt/errfmt"
"github.com/stretchr/testify/assert"
)
type Resp1 struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
}
func TestData(t *testing.T) {
r := gin.Default()
r.Use(MW())
r.GET("/ginfmt", func(c *gin.Context) {
Data(c, "foo")
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ginfmt", nil)
r.ServeHTTP(w, req)
resp := new(Resp1)
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), resp))
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, 0, resp.Code)
assert.Equal(t, "ok", resp.Message)
assert.Equal(t, "foo", resp.Data)
}
type Resp2 struct {
Code int `json:"code"`
Message string `json:"message"`
Data int `json:"data"`
}
func TestError(t *testing.T) {
FooError := errfmt.Register(http.StatusNotFound, 10010, "foo message")
r := gin.Default()
r.Use(MW())
r.GET("/ginfmt", func(c *gin.Context) {
Error(c, FooError.Gen())
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ginfmt", nil)
r.ServeHTTP(w, req)
resp := new(Resp2)
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), resp))
assert.Equal(t, resp.Code, FooError.Gen().Code())
assert.Equal(t, resp.Message, FooError.Gen().Message(context.TODO(), ""))
assert.Equal(t, 0, resp.Data)
}
func TestWrappedError(t *testing.T) {
FooError := errfmt.Register(http.StatusNotFound, 10010, "foo message")
r := gin.Default()
r.Use(MW())
r.GET("/ginfmt", func(c *gin.Context) {
err := fmt.Errorf("%w, extra info: test info", FooError.Gen())
Error(c, err)
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ginfmt", nil)
r.ServeHTTP(w, req)
resp := new(Resp2)
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), resp))
assert.Equal(t, resp.Code, FooError.Gen().Code())
assert.Equal(t, resp.Message, FooError.Gen().Message(context.TODO(), ""))
assert.Equal(t, 0, resp.Data)
}
type Resp3 struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
}
func TestDataError(t *testing.T) {
FooError := errfmt.Register(http.StatusNotFound, 10010, "foo message")
r := gin.Default()
r.Use(MW())
r.GET("/ginfmt", func(c *gin.Context) {
DataError(c, "foo", FooError.Gen())
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ginfmt", nil)
r.ServeHTTP(w, req)
resp := new(Resp3)
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), resp))
assert.Equal(t, resp.Code, FooError.Gen().Code())
assert.Equal(t, resp.Message, FooError.Gen().Message(context.TODO(), ""))
assert.Equal(t, "foo", resp.Data)
}
func DemoTrans(ctx context.Context, locale string, key string) string {
demoMap := map[string]map[string]string{
"zh": {
"foo": "这是一个foo信息",
},
"en-US": {
"foo": "This is foo message",
},
}
return demoMap[locale][key]
}
func TestI18n(t *testing.T) {
Init(nil, DemoTrans)
FooError := errfmt.Register(http.StatusNotFound, 10010, "foo")
r := gin.Default()
r.Use(func(c *gin.Context) {
c.Request.Header.Set("locale", "zh")
})
r.Use(MW())
r.GET("/ginfmt", func(c *gin.Context) {
DataError(c, "bar", FooError.Gen())
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ginfmt", nil)
r.ServeHTTP(w, req)
resp := new(Resp3)
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), resp))
assert.Equal(t, resp.Code, FooError.Gen().Code())
assert.Equal(t, resp.Message, FooError.Gen().Message(context.TODO(), "zh"))
assert.Equal(t, "bar", resp.Data)
}
type Resp4 struct {
Code int
Message string
Data Data4
}
type Data4 struct {
Name string
Age int
}
func TestHandler(t *testing.T) {
Init(nil, DemoTrans)
FooError := errfmt.Register(http.StatusNotFound, 10010, "foo")
myhandler := func(c *gin.Context) (interface{}, error) {
return Data4{"foo", 12}, FooError.Gen()
}
r := gin.Default()
r.Use(func(c *gin.Context) {
c.Request.Header.Set("locale", "zh")
})
r.Use(MW())
r.GET("/ginfmt", NewHandlerFunc(myhandler))
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ginfmt", nil)
r.ServeHTTP(w, req)
resp := new(Resp4)
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), resp))
assert.Equal(t, resp.Code, FooError.Gen().Code())
assert.Equal(t, resp.Message, FooError.Gen().Message(context.TODO(), "zh"))
assert.Equal(t, "foo", resp.Data.Name)
assert.Equal(t, 12, resp.Data.Age)
}