-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadowproxy_test.go
71 lines (66 loc) · 1.94 KB
/
shadowproxy_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
package shadowproxy
import (
"testing"
)
func TestTemplate(t *testing.T) {
urlPattern, urlKeys := parseURLPattern("/v1/{user}/foo/{bar}/{ukey}")
expected := "/v1/{{.User}}/foo/{{.Bar}}/{{.Ukey}}"
if urlPattern != expected {
t.Fatalf("expected '%s', got '%s'", expected, urlPattern)
}
if urlKeys[0] != "User" { // nolint
t.Fatalf("expected 'User', got '%s'", urlKeys[0])
}
if urlKeys[1] != "Bar" {
t.Fatalf("expected 'Bar', got '%s'", urlKeys[1])
}
if urlKeys[2] != "Ukey" {
t.Fatalf("expected 'Ukey', got '%s'", urlKeys[2])
}
if len(urlKeys) != 3 {
t.Fatalf("expected len '3', got '%d'", len(urlKeys))
}
}
func TestTemplateNoPathVars(t *testing.T) {
urlPattern := "/v1/user/ukey"
parsed, urlKeys := parseURLPattern(urlPattern)
if parsed != urlPattern {
t.Fatalf("expected '%s', got '%s'", urlPattern, parsed)
}
if len(urlKeys) != 0 {
t.Fatalf("expected len '0', got '%d'", len(urlKeys))
}
}
func TestTemplatePathVarsWithUpperCase(t *testing.T) {
urlPattern, urlKeys := parseURLPattern("/v1/{User}/foo/{BAR}/{uKey}")
expected := "/v1/{{.User}}/foo/{{.BAR}}/{{.UKey}}"
if urlPattern != expected {
t.Fatalf("expected '%s', got '%s'", expected, urlPattern)
}
if urlKeys[0] != "User" {
t.Fatalf("expected 'User', got '%s'", urlKeys[0])
}
if urlKeys[1] != "BAR" {
t.Fatalf("expected 'BAR', got '%s'", urlKeys[1])
}
if urlKeys[2] != "UKey" {
t.Fatalf("expected 'UKey', got '%s'", urlKeys[2])
}
if len(urlKeys) != 3 {
t.Fatalf("expected len '3', got '%d'", len(urlKeys))
}
}
func TestTemplateSameNameForVarAndFixedPath(t *testing.T) {
// make sure '{key}' is replaced, but not 'key'
urlPattern, urlKeys := parseURLPattern("/v1/key/{key}")
expected := "/v1/key/{{.Key}}"
if urlPattern != expected {
t.Fatalf("expected '%s', got '%s'", expected, urlPattern)
}
if urlKeys[0] != "Key" {
t.Fatalf("expected 'Ukey', got '%s'", urlKeys[0])
}
if len(urlKeys) != 1 {
t.Fatalf("expected len '1', got '%d'", len(urlKeys))
}
}