forked from cccoven/mini-gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
58 lines (47 loc) · 1.3 KB
/
router_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
package mini_gin
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestParsePattern(t *testing.T) {
ok := reflect.DeepEqual(parsePattern("/p/:name"), []string{"p", ":name"})
ok = ok && reflect.DeepEqual(parsePattern("/p/*"), []string{"p", "*"})
ok = ok && reflect.DeepEqual(parsePattern("/p/*name/*"), []string{"p", "*name"})
if !ok {
t.Fatal("test parsePattern failed")
}
}
func TestRouter(t *testing.T) {
r := newRouter()
// r.addRoute("GET", "/", nil)
r.addRoute("GET", "/hello/:name", nil)
r.addRoute("GET", "/hello/b/c", nil)
r.addRoute("GET", "/hi/:name", nil)
r.addRoute("GET", "/assets/*filepath", nil)
r.addRoute("GET", "/", nil)
n, params := r.getRoute("GET", "/")
n, params = r.getRoute("GET", "/hello/mini-gin")
n, params = r.getRoute("GET", "/assets/public/xx")
fmt.Println(n, params)
}
func TestParam(t *testing.T) {
r := New()
r.GET("/a/b", func(c *Context) {
c.String(http.StatusOK, "ok")
})
r.GET("/a/b", func(c *Context) {
c.String(http.StatusOK, "okok")
})
r.GET("/hello/:name", func(c *Context) {
c.String(http.StatusOK, c.Param("name"))
})
r.GET("/hello/:name/aaa", func(c *Context) {
c.String(http.StatusOK, c.Param("name")+"aaa")
})
r.GET("/assets/*filepath/test", func(c *Context) {
c.String(http.StatusOK, c.Param("filepath"))
})
r.Run(":8080")
}