/
helper_test.go
84 lines (73 loc) 路 1.42 KB
/
helper_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
package api
import (
"encoding/json"
"fmt"
"sort"
"strings"
"testing"
)
func TestPathOrdering(t *testing.T) {
hosts := []*HTTPHost{
{
Host: "*.*.abc.com",
Paths: []*HTTPPath{
{
Path: "/Ta",
},
},
},
{
Host: "abc.com",
Paths: []*HTTPPath{
{
Path: "/",
},
{
Path: "/T",
},
},
},
{
Host: "",
Paths: []*HTTPPath{
{
Path: "/t",
},
{
Path: "/TA",
},
},
},
}
sort.Slice(hosts, func(i, j int) bool {
host_i := hostName(hosts[i].Host)
host_rank_i := hostRank(hosts[i].Host)
host_j := hostName(hosts[j].Host)
host_rank_j := hostRank(hosts[j].Host)
if host_rank_i == host_rank_j {
return host_i > host_j
}
return host_rank_i > host_rank_j
})
for y := range hosts {
host := hosts[y]
for z := range host.Paths {
if host.Paths[z].Backend != nil {
host.Paths[z].Backend.canonicalize(false, host.Host, "80", host.Paths[z].Path)
}
}
sort.Slice(host.Paths, func(i, j int) bool {
path_i := strings.ToLower(strings.Trim(host.Paths[i].Path, "/"))
path_comp_i := len(strings.Split(path_i, "/"))
path_j := strings.ToLower(strings.Trim(host.Paths[j].Path, "/"))
path_comp_j := len(strings.Split(path_j, "/"))
if path_comp_i == path_comp_j {
return path_i > path_j
}
return path_comp_i > path_comp_j
})
hosts[y] = host
}
b, _ := json.MarshalIndent(hosts, "", " ")
fmt.Println(string(b))
}