Debian Codegraph @

uritemplates/utils_test.go

raw
1 package uritemplates
2
3 import (
4 "testing"
5 )
6
7 type ExpandTest struct {
8 in string
9 expansions map[string]string
10 want string
11 }
12
13 var expandTests = []ExpandTest{
14 // #0: no expansions
15 {
16 "http://www.golang.org/",
17 map[string]string{},
18 "http://www.golang.org/",
19 },
20 // #1: one expansion, no escaping
21 {
22 "http://www.golang.org/{bucket}/delete",
23 map[string]string{
24 "bucket": "red",
25 },
26 "http://www.golang.org/red/delete",
27 },
28 // #2: one expansion, with hex escapes
29 {
30 "http://www.golang.org/{bucket}/delete",
31 map[string]string{
32 "bucket": "red/blue",
33 },
34 "http://www.golang.org/red%2Fblue/delete",
35 },
36 // #3: one expansion, with space
37 {
38 "http://www.golang.org/{bucket}/delete",
39 map[string]string{
40 "bucket": "red or blue",
41 },
42 "http://www.golang.org/red%20or%20blue/delete",
43 },
44 // #4: expansion not found
45 {
46 "http://www.golang.org/{object}/delete",
47 map[string]string{
48 "bucket": "red or blue",
49 },
50 "http://www.golang.org//delete",
51 },
52 // #5: multiple expansions
53 {
54 "http://www.golang.org/{one}/{two}/{three}/get",
55 map[string]string{
56 "one": "ONE",
57 "two": "TWO",
58 "three": "THREE",
59 },
60 "http://www.golang.org/ONE/TWO/THREE/get",
61 },
62 // #6: utf-8 characters
63 {
64 "http://www.golang.org/{bucket}/get",
65 map[string]string{
66 "bucket": "£100",
67 },
68 "http://www.golang.org/%C2%A3100/get",
69 },
70 // #7: punctuations
71 {
72 "http://www.golang.org/{bucket}/get",
73 map[string]string{
74 "bucket": `/\@:,.*~`,
75 },
76 "http://www.golang.org/%2F%5C%40%3A%2C.%2A~/get",
77 },
78 // #8: mis-matched brackets
79 {
80 "http://www.golang.org/{bucket/get",
81 map[string]string{
82 "bucket": "red",
83 },
84 "",
85 },
86 // #9: "+" prefix for suppressing escape
87 // See also: http://tools.ietf.org/html/rfc6570#section-3.2.3
88 {
89 "http://www.golang.org/{+topic}",
90 map[string]string{
91 "topic": "/topics/myproject/mytopic",
92 },
93 // The double slashes here look weird, but it's intentional
94 "http://www.golang.org//topics/myproject/mytopic",
95 },
96 }
97
98 func TestExpand(t *testing.T) {
99 for i, test := range expandTests {
100 got, _ := Expand(test.in, test.expansions)
101 if got != test.want {
102 t.Errorf("got %q expected %q in test %d", got, test.want, i)
103 }
104 }
105 }