-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathpath_replace_test.go
More file actions
94 lines (86 loc) · 2.36 KB
/
Copy pathpath_replace_test.go
File metadata and controls
94 lines (86 loc) · 2.36 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
package httpbinding
import (
"bytes"
"testing"
)
func TestPathReplace(t *testing.T) {
cases := []struct {
Orig, ExpPath, ExpRawPath []byte
Key, Val string
ExpectErr bool
}{
{
Orig: []byte("/{bucket}/{key+}"),
ExpPath: []byte("/123/{key+}"),
ExpRawPath: []byte("/123/{key+}"),
Key: "bucket", Val: "123",
},
{
Orig: []byte("/{bucket}/{key+}"),
ExpPath: []byte("/{bucket}/abc"),
ExpRawPath: []byte("/{bucket}/abc"),
Key: "key", Val: "abc",
},
{
Orig: []byte("/{bucket}/{key+}"),
ExpPath: []byte("/{bucket}/a/b/c"),
ExpRawPath: []byte("/{bucket}/a/b/c"),
Key: "key", Val: "a/b/c",
},
{
Orig: []byte("/{bucket}/{key+}"),
ExpPath: []byte("/1/2/3/{key+}"),
ExpRawPath: []byte("/1%2F2%2F3/{key+}"),
Key: "bucket", Val: "1/2/3",
},
{
Orig: []byte("/{bucket}/{key+}"),
ExpPath: []byte("/reallylongvaluegoesheregrowingarray/{key+}"),
ExpRawPath: []byte("/reallylongvaluegoesheregrowingarray/{key+}"),
Key: "bucket", Val: "reallylongvaluegoesheregrowingarray",
},
{
Orig: []byte("/{namespace}/{name}"),
ExpPath: []byte("/{namespace}/value"),
ExpRawPath: []byte("/{namespace}/value"),
Key: "name", Val: "value",
},
{
Orig: []byte("/{name}/{namespace}"),
ExpPath: []byte("/value/{namespace}"),
ExpRawPath: []byte("/value/{namespace}"),
Key: "name", Val: "value",
},
{
Orig: []byte("/{namespace}/{name+}"),
Key: "nam", Val: "value",
ExpectErr: true,
},
}
var buffer [64]byte
for i, c := range cases {
origRaw := make([]byte, len(c.Orig))
copy(origRaw, c.Orig)
path, _, err := replacePathElement(c.Orig, buffer[:0], c.Key, c.Val, false)
if err != nil {
if !c.ExpectErr {
t.Fatalf("expected no error, got %v", err)
}
} else if c.ExpectErr {
t.Fatalf("expect error, got none")
}
if c.ExpectErr {
return
}
rawPath, _, err := replacePathElement(origRaw, buffer[:0], c.Key, c.Val, true)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if e, a := c.ExpPath, path; bytes.Compare(e, a) != 0 {
t.Errorf("%d, expect uri path to be %q got %q", i, e, a)
}
if e, a := c.ExpRawPath, rawPath; bytes.Compare(e, a) != 0 {
t.Errorf("%d, expect uri raw path to be %q got %q", i, e, a)
}
}
}