-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhost_test.go
126 lines (108 loc) · 3.17 KB
/
host_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
package echo_test
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/webx-top/echo"
"github.com/webx-top/echo/testing/test"
)
func TestHostParse(t *testing.T) {
h := NewHost(`<a:[0-9]+>.<b>.cc`)
h.Parse()
test.Eq(t, `%v.%v.cc`, h.Format())
test.Eq(t, `^([0-9]+)\.([^.]+)\.cc$`, h.RegExp().String())
r := h.RegExp().FindStringSubmatch(`1.baa.cc`)
test.Eq(t, []string{
`1.baa.cc`,
`1`,
`baa`,
}, r)
}
func mwOnHostFound(c Context) (bool, error) {
if c.HostParam(`name`) != `coscms` {
return true, nil
}
fmt.Println(`host param (uid):`, c.HostParam(`uid`))
switch c.HostParam(`uid`) {
case `123`:
return true, nil
case `0`:
return false, errors.New(`err`)
default:
return false, nil
}
}
func TestHostGroupHandler(t *testing.T) {
e := New()
//e.OnHostFound(mwOnHostFound)
e.Pre(func(h Handler) HandlerFunc {
return func(c Context) error {
c.OnHostFound(mwOnHostFound)
return h.Handle(c)
}
})
e.Get(`/`, func(c Context) error {
return c.String(`pong`)
})
// suffix
g := e.Host(".admpub.com")
g.Get("/host", func(c Context) error {
if c.Queryx(`route`).Bool() {
return c.JSON(c.Route())
}
return c.String(c.Host())
}).SetName(`host`)
// rule
g = e.Host("<uid:[0-9]+>.<name>.com").SetAlias(`user`)
g.Get("/host2", func(c Context) error {
if c.Queryx(`route`).Bool() {
return c.JSON(c.Route())
}
return c.String(c.HostParam(`uid`) + `.` + c.HostParam(`name`) + `.com`)
}).SetName(`host2`)
e.RebuildRouter()
c, b := request(GET, "/host", e, func(req *http.Request) {
req.Host = "test.admpub.com"
})
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "test.admpub.com", b)
c, b = request(GET, "/host", e, func(req *http.Request) {
req.Host = "test-b.admpub.com"
})
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "test-b.admpub.com", b)
c, b = request(GET, "/host?route=1", e, func(req *http.Request) {
req.Host = "test-b.admpub.com"
})
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, `{"Host":".admpub.com","Method":"GET","Path":"/host","Name":"host","Format":"/host","Params":[],"Prefix":"","Meta":{}}`, b)
c, b = request(GET, "/host2", e, func(req *http.Request) {
req.Host = "123.coscms.com"
})
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "123.coscms.com", b)
assert.Equal(t, "10000.admpub.com/host2", e.TypeHost(`user`, 10000, `admpub`).URI(`host2`))
assert.Equal(t, "10001.admpub.com/host2", e.TypeHost(`user`, H{`uid`: 10001, `name`: `admpub`}).URI(`host2`))
c, b = request(GET, "/", e, func(req *http.Request) {
req.Host = "999.coscms.com"
})
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, `pong`, b)
c, b = request(GET, "/host2", e, func(req *http.Request) {
req.Host = "0.coscms.com"
})
assert.Equal(t, http.StatusInternalServerError, c)
assert.Equal(t, http.StatusText(http.StatusInternalServerError), b)
c, b = request(GET, "/host2", e, func(req *http.Request) {
req.Host = "3.coscms.com"
})
assert.Equal(t, http.StatusNotFound, c)
assert.Equal(t, http.StatusText(http.StatusNotFound), b)
c, b = request(GET, "/", e, func(req *http.Request) {
req.Host = "999.coscms.com"
})
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, `pong`, b)
}