-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
148 lines (123 loc) · 3.37 KB
/
config_test.go
File metadata and controls
148 lines (123 loc) · 3.37 KB
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
package config
import (
"testing"
)
type Config struct {
Version string `config:"version"`
// gzingress
Server struct {
Ports []int64 `config:"ports"`
Cleanup string `config:"cleanup"`
} `config:"server"`
Logger struct {
Level string `config:"level"`
Trace bool `config:"trace"`
} `config:"logger"`
Rules []struct {
Host string `config:"host"`
Backend struct {
ServiceName string `config:"service_name"`
ServicePort int64 `config:"service_port"`
} `config:"backend"`
} `config:"rules"`
Struc struct {
Field1 string `config:"field1"`
Field2 string `config:"field2"`
} `config:"struct"`
// gzfly
Relay string `config:"relay"`
Auth string `config:"auth"`
Crypto string `config:"crypto"`
//
Actions map[string]Action `config:"actions"`
}
type Action struct {
Target string `config:"target"`
Bind string `config:"bind"`
Socks5 string `config:"socks5"`
}
func TestConfig(t *testing.T) {
var cfg Config
if err := Load(&cfg); err != nil {
t.Fatal(err)
}
// j, _ := json.MarshalIndent(cfg, "", " ")
// fmt.Println(string(j))
if cfg.Version != "1.0.0" {
t.Fatal("version is not 1.0.0")
}
if len(cfg.Server.Ports) != 2 {
t.Fatal("ports is not 2")
}
if cfg.Server.Ports[0] != 8080 {
t.Fatal("ports[0] is not 8080")
}
if cfg.Server.Ports[1] != 9090 {
t.Fatal("ports[1] is not 9090")
}
if cfg.Server.Cleanup != "1h" {
t.Fatal("cleanup is not 1h")
}
if cfg.Logger.Level != "warn" {
t.Fatal("level is not warn")
}
if cfg.Logger.Trace != false {
t.Fatal("trace is not false")
}
if len(cfg.Rules) != 2 {
t.Fatal("rules is not 2")
}
if cfg.Rules[0].Host != "backend-a.example.com" {
t.Fatal("rules[0].host is not backend-a.example.com")
}
if cfg.Rules[0].Backend.ServiceName != "backend-a" {
t.Fatal("rules[0].backend.service_name is not backend-a")
}
if cfg.Rules[0].Backend.ServicePort != 80 {
t.Fatal("rules[0].backend.service_port is not 80")
}
if cfg.Rules[1].Host != "backend-b.example.com" {
t.Fatal("rules[1].host is not backend-b.example.com")
}
if cfg.Rules[1].Backend.ServiceName != "backend-b" {
t.Fatal("rules[1].backend.service_name is not backend-b")
}
if cfg.Rules[1].Backend.ServicePort != 8080 {
t.Fatal("rules[1].backend.service_port is not 8080")
}
// gzfly
if cfg.Actions["action1"].Target != "client_name:pk" {
t.Fatalf("actions.action1.target is not %s, but got %s", "client_name:pk", cfg.Actions["action1"].Target)
}
if cfg.Actions["action1"].Bind != "tcp:0.0.0.0:17890:192.168.1.2:17890" {
t.Fatalf("actions.action1.bind is not %s, but got %s", "tcp:0.0.0.0:17890:192.168.1.2:17890", cfg.Actions["action1"].Bind)
}
if cfg.Actions["action1"].Socks5 != "" {
t.Fatal("actions.action1.socks5 is not empty string")
}
}
func TestConfigFromURL(t *testing.T) {
type Config2 struct {
Title string `config:"title"`
TagLine string `config:"tagline"`
Author struct {
Name string `config:"name"`
Nickname string `config:"nickname"`
Email string `config:"email"`
GitHub string `config:"github"`
} `config:"author"`
}
var cfg Config2
err := Load(&cfg, &LoadOptions{
FilePath: "https://raw.githubusercontent.com/whatwewant/whatwewant.github.io/refs/heads/master/_config.yml",
})
if err != nil {
t.Fatal(err)
}
if cfg.Title != "Cole" {
t.Fatal("title is not Cole")
}
if cfg.TagLine != "Free as the wind." {
t.Fatal("tagline is not Free as the wind.")
}
}