forked from divan/expvarmon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
88 lines (75 loc) · 2.02 KB
/
utils_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
package main
import "testing"
func TestUtils(t *testing.T) {
str := "memstats.Alloc,memstats.Sys"
vars, err := ParseVars(str)
if err != nil {
t.Fatalf("Err not nil: %v", err)
}
if len(vars) != 2 {
t.Fatalf("vars should contain 2 elements, but has %d", len(vars))
}
str = "memstats.Alloc,memstats.Sys,goroutines,Counter.A"
vars, err = ParseVars(str)
if err != nil {
t.Fatalf("Err not nil: %v", err)
}
if len(vars) != 4 {
t.Fatalf("vars should contain 4 elements, but has %d", len(vars))
}
}
func TestPorts(t *testing.T) {
arg := "1234,1235"
ports, err := ParsePorts(arg)
if err != nil {
t.Fatal(err)
}
if len(ports) != 2 || ports[0].Host != "localhost:1234" {
t.Fatalf("ParsePorts returns wrong data: %v", ports)
}
arg = "1234-1237,2000"
ports, err = ParsePorts(arg)
if err != nil {
t.Fatal(err)
}
if len(ports) != 5 || ports[0].Host != "localhost:1234" || ports[4].Host != "localhost:2000" {
t.Fatalf("ParsePorts returns wrong data: %v", ports)
}
arg = "40000-40002,localhost:2000-2002,remote:1234-1235,https://example.com:1234-1236"
ports, err = ParsePorts(arg)
if err != nil {
t.Fatal(err)
}
if len(ports) != 11 ||
ports[0].Host != "localhost:40000" ||
ports[3].Host != "localhost:2000" ||
ports[7].Host != "remote:1235" ||
ports[7].Path != "/debug/vars" ||
ports[10].Host != "example.com:1236" ||
ports[10].Scheme != "https" {
t.Fatalf("ParsePorts returns wrong data: %v", ports)
}
// Test Auth
arg = "http://user:pass@localhost:2000-2002"
ports, err = ParsePorts(arg)
if err != nil {
t.Fatal(err)
}
pass, isSet := ports[0].User.Password()
if len(ports) != 3 ||
ports[0].User.Username() != "user" ||
pass != "pass" || !isSet ||
ports[0].Scheme != "http" {
t.Fatalf("ParsePorts returns wrong data: %v", ports)
}
arg = "localhost:2000-2002,remote:1234-1235,some:weird:1234-123input"
_, err = ParsePorts(arg)
if err == nil {
t.Fatalf("err shouldn't be nil")
}
arg = "string,sdasd"
_, err = ParsePorts(arg)
if err == nil {
t.Fatalf("err shouldn't be nil")
}
}