-
Notifications
You must be signed in to change notification settings - Fork 16
/
extends_test.go
257 lines (186 loc) · 5.77 KB
/
extends_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package rux
import (
"errors"
"html/template"
"io"
"net/url"
"reflect"
"strings"
"testing"
"github.com/gookit/goutil/testutil/assert"
)
func TestBuildRequestUrl_Params(t *testing.T) {
is := assert.New(t)
b := NewBuildRequestURL()
b.Path(`/news/{category_id}/{new_id}/detail`)
b.Params(M{"{category_id}": "100", "{new_id}": "20"})
is.Eq(b.Build().String(), `/news/100/20/detail`)
}
func TestBuildRequestUrl_Host(t *testing.T) {
is := assert.New(t)
b := NewBuildRequestURL()
b.Scheme("https")
b.Host("127.0.0.1")
b.Path(`/news`)
is.Eq(b.Build().String(), `https://127.0.0.1/news`)
}
func TestBuildRequestURL_User(t *testing.T) {
is := assert.New(t)
b := NewBuildRequestURL()
b.Scheme("https")
b.User("tom", "123")
b.Host("127.0.0.1")
b.Path(`/news`)
is.Eq(b.Build().String(), `https://tom:[email protected]/news`)
}
func TestBuildRequestUrl_Queries(t *testing.T) {
is := assert.New(t)
var u = make(url.Values)
u.Add("username", "admin")
u.Add("password", "12345")
b := NewBuildRequestURL()
b.Queries(u)
b.Path(`/news`)
is.Eq(b.Build().String(), `/news?password=12345&username=admin`)
}
func TestBuildRequestUrl_Build(t *testing.T) {
is := assert.New(t)
r := New()
homepage := NewNamedRoute("homepage", `/build-test/{name}/{id:\d+}`, emptyHandler, GET)
homepageFiexdPath := NewNamedRoute("homepage_fiexd_path", `/build-test/fiexd/path`, emptyHandler, GET)
r.AddRoute(homepage)
r.AddRoute(homepageFiexdPath)
b := NewBuildRequestURL()
b.Params(M{"{name}": "test", "{id}": "20"})
is.Eq(r.BuildURL("homepage", b).String(), `/build-test/test/20`)
is.Eq(r.BuildRequestURL("homepage_fiexd_path").String(), `/build-test/fiexd/path`)
}
func TestBuildRequestUrl_With(t *testing.T) {
r := New()
is := assert.New(t)
homepage := NewNamedRoute("homepage", `/build-test/{name}/{id:\d+}`, emptyHandler, GET)
r.AddRoute(homepage)
is.Eq(r.BuildRequestURL("homepage", M{
"{name}": "test",
"{id}": 20,
"username": "demo",
}).String(), `/build-test/test/20?username=demo`)
}
func TestBuildRequestUrl_WithCustom(t *testing.T) {
is := assert.New(t)
b := NewBuildRequestURL()
b.Path("/build-test/test/{id}")
is.Eq(b.Build(M{
"{id}": 20,
"username": "demo",
}).String(), `/build-test/test/20?username=demo`)
}
func TestBuildRequestUrl_WithMutilArgs(t *testing.T) {
r := New()
is := assert.New(t)
homepage := NewNamedRoute("homepage", `/build-test/{name}/{id:\d+}`, emptyHandler, GET)
r.AddRoute(homepage)
str := r.BuildRequestURL("homepage", "{name}", "test", "{id}", 20, "username", "demo").String()
is.Eq(`/build-test/test/20?username=demo`, str)
}
func TestBuildRequestUrl_WithMutilArgs2(t *testing.T) {
r := New()
is := assert.New(t)
homepage := NewNamedRoute("homepage", `/build-test`, emptyHandler, GET)
r.AddRoute(homepage)
str := r.BuildRequestURL("homepage", "{name}", "test", "{id}", 20, "username", "demo").String()
is.Eq(`/build-test?username=demo`, str)
str = r.BuildURL("homepage", "{name}", "test", "{id}", 20).String()
is.Eq(`/build-test`, str)
}
func TestBuildRequestUrl_WithMutilArgs3(t *testing.T) {
r := New()
is := assert.New(t)
homepage := NewNamedRoute("homepage", `/build-test/{id}`, emptyHandler, GET)
r.AddRoute(homepage)
str := r.BuildRequestURL("homepage", "{name}", "test", "{id}", 20, "username", "demo").String()
is.Eq(`/build-test/20?username=demo`, str)
str = r.BuildURL("homepage", "{name}", "test", "{id}", 23).String()
is.Eq(`/build-test/23`, str)
}
func TestBuildRequestUrl_EmptyRoute(t *testing.T) {
r := New()
is := assert.New(t)
homepage := NewNamedRoute("homepage", `/build-test/{name}/{id:\d+}`, emptyHandler, GET)
r.AddRoute(homepage)
is.PanicsMsg(func() {
r.BuildRequestURL("homepage-empty", "{name}", "test", "{id}", "20", "username", "demo")
}, "BuildRequestURL get route is nil(name: homepage-empty)")
}
func TestBuildRequestUrl_ErrorArgs(t *testing.T) {
r := New()
is := assert.New(t)
homepage := NamedRoute("homepage", `/build-test/{name}/{id:\d+}`, emptyHandler, GET)
r.AddRoute(homepage)
is.PanicsMsg(func() {
r.BuildRequestURL("homepage", "one")
}, "buildArgs odd argument count")
is.PanicsMsg(func() {
r.BuildRequestURL("homepage", "{name}", "test", "{id}")
}, "buildArgs odd argument count")
}
type MyValidator string
func (mv *MyValidator) Validate(v any) error {
var rt = reflect.TypeOf(v)
var rv = reflect.ValueOf(v)
var field = rt.Elem().Field(0)
var value = rv.Elem().Field(0)
var rules = field.Tag.Get("valid")
for _, rule := range strings.Split(rules, "|") {
if rule == "required" && value.Interface() == "" {
return errors.New("must required")
}
if rule == "email" && value.Interface() != "[email protected]" {
return errors.New("email error")
}
}
return nil
}
func TestContext_Validator(t *testing.T) {
ris := assert.New(t)
r := New()
// r.Validator = new(MyValidator)
r.Any("/validator", func(c *Context) {
var form = new(struct {
Username string `valid:"required|email"`
})
form.Username = "[email protected]"
if err := c.Validate(form); err != nil {
c.Text(200, err.Error())
return
}
c.Text(200, "passed")
})
w := mockRequest(r, GET, "/validator", nil)
ris.Eq(200, w.Code)
ris.Eq(w.Body.String(), `passed`)
}
type MyRenderer string
func (mr *MyRenderer) Render(w io.Writer, name string, data any, _ *Context) error {
tpl, err := template.New(name).Funcs(template.FuncMap{
"Upper": strings.ToUpper,
}).Parse("{{.Name|Upper}}, ID is {{ .ID}}")
if err != nil {
return err
}
return tpl.Execute(w, data)
}
func TestContext_Renderer(t *testing.T) {
is := assert.New(t)
r := New()
r.Renderer = new(MyRenderer)
r.Any("/renderer", func(c *Context) {
_ = c.Render(200, "index", M{
"ID": 100,
"Name": "admin",
})
})
w := mockRequest(r, GET, "/renderer", nil)
is.Eq(200, w.Code)
is.Eq(`ADMIN, ID is 100`, w.Body.String())
}