File size: 2,511 Bytes
530729e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package context

import (
	"fmt"
	"testing"

	"github.com/magiconair/properties/assert"
)

func TestSlash(t *testing.T) {
	assert.Equal(t, "/abc", slash("/abc"))
	assert.Equal(t, "/", slash(""))
	assert.Equal(t, "/abc", slash("abc/"))
	assert.Equal(t, "/", slash("/"))
	assert.Equal(t, "/abc", slash("/abc/"))
	assert.Equal(t, "/", slash("//"))
}

func TestJoin(t *testing.T) {
	assert.Equal(t, "/abc/abc", join(slash("/abc"), slash("/abc")))
	assert.Equal(t, "/", join(slash("/"), slash("/")))
	assert.Equal(t, "/abc", join(slash("/"), slash("/abc")))
	assert.Equal(t, "/abc", join(slash("abc/"), slash("/")))
	assert.Equal(t, "/abc", join(slash("/abc/"), slash("/")))
}

func TestTree(t *testing.T) {
	tree := tree()
	tree.addPath(stringToArr("/adm"), "GET", []Handler{func(ctx *Context) { fmt.Println(1) }})
	tree.addPath(stringToArr("/admi"), "GET", []Handler{func(ctx *Context) { fmt.Println(1) }})
	tree.addPath(stringToArr("/admin"), "GET", []Handler{func(ctx *Context) { fmt.Println(1) }})
	tree.addPath(stringToArr("/admin/menu/new"), "POST", []Handler{func(ctx *Context) { fmt.Println(1) }})
	tree.addPath(stringToArr("/admin/menu/new"), "GET", []Handler{func(ctx *Context) { fmt.Println(1) }})
	tree.addPath(stringToArr("/admin/info/:__prefix"), "GET", []Handler{
		func(ctx *Context) { fmt.Println("auth") },
		func(ctx *Context) { fmt.Println("init") },
		func(ctx *Context) { fmt.Println("info") },
	})
	tree.addPath(stringToArr("/admin/info/:__prefix/detail"), "GET", []Handler{
		func(ctx *Context) { fmt.Println("auth") },
		func(ctx *Context) { fmt.Println("detail") },
	})

	fmt.Println("/admin/menu/new", "GET")
	h := tree.findPath(stringToArr("/admin/menu/new"), "GET")
	assert.Equal(t, h != nil, true)
	printHandler(h)
	fmt.Println("/admin/menu/new", "POST")
	h = tree.findPath(stringToArr("/admin/menu/new"), "POST")
	assert.Equal(t, h != nil, true)
	printHandler(h)
	fmt.Println("/admin/me/new", "POST")
	h = tree.findPath(stringToArr("/admin/me/new"), "POST")
	assert.Equal(t, h == nil, true)
	printHandler(h)
	fmt.Println("/admin/info/user", "GET")
	h = tree.findPath(stringToArr("/admin/info/user"), "GET")
	assert.Equal(t, h != nil, true)
	printHandler(h)
	fmt.Println("/admin/info/user/detail", "GET")
	h = tree.findPath(stringToArr("/admin/info/user/detail"), "GET")
	assert.Equal(t, h != nil, true)
	printHandler(h)
	fmt.Println("=========== printChildren ===========")
	tree.printChildren()
}

func printHandler(h []Handler) {
	for _, value := range h {
		value(&Context{})
	}
}