1 package httpbinding
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestPathReplace(t *testing.T) {
9 cases := []struct {
10 Orig, ExpPath, ExpRawPath []byte
11 Key, Val string
12 }{
13 {
14 Orig: []byte("/{bucket}/{key+}"),
15 ExpPath: []byte("/123/{key+}"),
16 ExpRawPath: []byte("/123/{key+}"),
17 Key: "bucket", Val: "123",
18 },
19 {
20 Orig: []byte("/{bucket}/{key+}"),
21 ExpPath: []byte("/{bucket}/abc"),
22 ExpRawPath: []byte("/{bucket}/abc"),
23 Key: "key", Val: "abc",
24 },
25 {
26 Orig: []byte("/{bucket}/{key+}"),
27 ExpPath: []byte("/{bucket}/a/b/c"),
28 ExpRawPath: []byte("/{bucket}/a/b/c"),
29 Key: "key", Val: "a/b/c",
30 },
31 {
32 Orig: []byte("/{bucket}/{key+}"),
33 ExpPath: []byte("/1/2/3/{key+}"),
34 ExpRawPath: []byte("/1%2F2%2F3/{key+}"),
35 Key: "bucket", Val: "1/2/3",
36 },
37 {
38 Orig: []byte("/{bucket}/{key+}"),
39 ExpPath: []byte("/reallylongvaluegoesheregrowingarray/{key+}"),
40 ExpRawPath: []byte("/reallylongvaluegoesheregrowingarray/{key+}"),
41 Key: "bucket", Val: "reallylongvaluegoesheregrowingarray",
42 },
43 }
44
45 var buffer [64]byte
46
47 for i, c := range cases {
48 origRaw := make([]byte, len(c.Orig))
49 copy(origRaw, c.Orig)
50
51 path, _, err := replacePathElement(c.Orig, buffer[:0], c.Key, c.Val, false)
52 if err != nil {
53 t.Fatalf("expected no error, got %v", err)
54 }
55 rawPath, _, err := replacePathElement(origRaw, buffer[:0], c.Key, c.Val, true)
56 if err != nil {
57 t.Fatalf("expected no error, got %v", err)
58 }
59
60 if e, a := c.ExpPath, path; bytes.Compare(e, a) != 0 {
61 t.Errorf("%d, expect uri path to be %q got %q", i, e, a)
62 }
63 if e, a := c.ExpRawPath, rawPath; bytes.Compare(e, a) != 0 {
64 t.Errorf("%d, expect uri raw path to be %q got %q", i, e, a)
65 }
66 }
67 }
68
View as plain text