-
Notifications
You must be signed in to change notification settings - Fork 101
/
utils_test.go
121 lines (103 loc) · 3.16 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
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
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 TestExtractUrlAndPorts(t *testing.T) {
var rawurl, ports string
rawurl, ports = extractURLAndPorts("40000-40002")
if rawurl != "http://localhost" || ports != "40000-40002" {
t.Fatalf("extract url and ports failed: %v, %v", rawurl, ports)
}
rawurl, ports = extractURLAndPorts("https://example.com:1234")
if rawurl != "https://example.com" || ports != "1234" {
t.Fatalf("extract url and ports failed: %v, %v", rawurl, ports)
}
rawurl, ports = extractURLAndPorts("http://user:passwd@example.com:1234-1256")
if rawurl != "http://user:passwd@example.com" || ports != "1234-1256" {
t.Fatalf("extract url and ports failed: %v, %v", rawurl, ports)
}
rawurl, ports = extractURLAndPorts("https://example.com:1234-1256/_endpoint")
if rawurl != "https://example.com/_endpoint" || ports != "1234-1256" {
t.Fatalf("extract url and ports failed: %v, %v", rawurl, ports)
}
}
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")
}
// Test endpoints
arg = "localhost:2000,https://example.com:1234/_custom_expvars"
ports, err = ParsePorts(arg)
if err != nil {
t.Fatal(err)
}
if ports[0].Path != "/debug/vars" || ports[1].Path != "/_custom_expvars" {
t.Fatalf("ParsePorts returns wrong data: %v", ports)
}
}