trpc.group/trpc-go/trpc-go@v1.0.3/internal/httprule/parse_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package httprule 15 16 import ( 17 "errors" 18 "reflect" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestParseTemplate(t *testing.T) { 25 for _, test := range []struct { 26 input string 27 wantTpl *PathTemplate 28 wantErr error 29 desc string 30 }{ 31 { 32 input: "/foo/{bar}", 33 wantTpl: &PathTemplate{ 34 segments: []segment{ 35 literal("foo"), 36 variable{fp: []string{"bar"}, segments: []segment{wildcard{}}}, 37 }, 38 }, 39 wantErr: nil, 40 desc: "ParseTemplateTest01", 41 }, 42 { 43 input: "/foo/*:bar", 44 wantTpl: &PathTemplate{ 45 segments: []segment{ 46 literal("foo"), 47 wildcard{}, 48 }, 49 verb: "bar", 50 }, 51 wantErr: nil, 52 desc: "ParseTemplateTest02", 53 }, 54 { 55 input: "/foo/**:bar", 56 wantTpl: &PathTemplate{ 57 segments: []segment{ 58 literal("foo"), 59 deepWildcard{}, 60 }, 61 verb: "bar", 62 }, 63 wantErr: nil, 64 desc: "ParseTemplateTest03", 65 }, 66 { 67 input: "/foo/*/bar:baz", 68 wantTpl: &PathTemplate{ 69 segments: []segment{ 70 literal("foo"), 71 wildcard{}, 72 literal("bar"), 73 }, 74 verb: "baz", 75 }, 76 wantErr: nil, 77 desc: "ParseTemplateTest04", 78 }, 79 { 80 input: "/foobar/{foo.bar.baz}:qux", 81 wantTpl: &PathTemplate{ 82 segments: []segment{ 83 literal("foobar"), 84 variable{fp: []string{"foo", "bar", "baz"}, segments: []segment{wildcard{}}}, 85 }, 86 verb: "qux", 87 }, 88 wantErr: nil, 89 desc: "ParseTemplateTest05", 90 }, 91 { 92 input: "/foo/{bar=baz/**}", 93 wantTpl: &PathTemplate{ 94 segments: []segment{ 95 literal("foo"), 96 variable{fp: []string{"bar"}, segments: []segment{literal("baz"), deepWildcard{}}}, 97 }, 98 }, 99 wantErr: nil, 100 desc: "ParseTemplateTest06", 101 }, 102 { 103 input: "/foo/{bar}/**", 104 wantTpl: &PathTemplate{ 105 segments: []segment{ 106 literal("foo"), 107 variable{fp: []string{"bar"}, segments: []segment{wildcard{}}}, 108 deepWildcard{}, 109 }, 110 }, 111 desc: "ParseTemplateTest07", 112 }, 113 } { 114 gotTpl, gotErr := Parse(test.input) 115 require.Equal(t, true, gotErr == test.wantErr, test.desc) 116 require.Equal(t, true, reflect.DeepEqual(gotTpl, test.wantTpl), test.desc) 117 } 118 } 119 120 func TestFieldPaths(t *testing.T) { 121 for _, test := range []struct { 122 input string 123 want [][]string 124 desc string 125 }{ 126 { 127 input: "/foo/{bar}/baz/{qux}/*/**", 128 want: [][]string{{"bar"}, {"qux"}}, 129 desc: "GetFieldPathsTest01", 130 }, 131 { 132 input: "/foo/bar", 133 want: nil, 134 desc: "GetFieldPathsTest02", 135 }, 136 } { 137 tpl, err := Parse(test.input) 138 require.Nil(t, err, test.desc) 139 require.Equal(t, test.want, tpl.FieldPaths(), test.desc) 140 } 141 } 142 143 func TestVerb(t *testing.T) { 144 for _, test := range []struct { 145 input string 146 want string 147 desc string 148 }{ 149 { 150 input: "/a/b/c:d:e:f:g", 151 want: "g", 152 desc: "test verb 01", 153 }, 154 { 155 input: "/a/b/{c}:d:e:f:g", 156 want: "d:e:f:g", 157 desc: "test verb 02", 158 }, 159 { 160 input: "/a/b/*:d:e:f:g", 161 want: "d:e:f:g", 162 desc: "test verb 03", 163 }, 164 { 165 input: "/a/b/**:d:e:f:g", 166 want: "d:e:f:g", 167 desc: "test verb 04", 168 }, 169 { 170 input: "/a/b/*/**", 171 want: "", 172 desc: "test verb 05", 173 }, 174 } { 175 tpl, err := Parse(test.input) 176 require.Nil(t, err, test.desc) 177 require.Equal(t, tpl.verb, test.want, test.desc) 178 } 179 } 180 181 func TestValidateTemplate(t *testing.T) { 182 for _, test := range []struct { 183 input string 184 wantErr error 185 desc string 186 }{ 187 { 188 input: "/v1/{name=a/{nested}}", 189 wantErr: errNestedVar, 190 desc: "validate tpl test 01", 191 }, 192 { 193 input: "/v1/{name}/{name}", 194 wantErr: errDupFieldPath, 195 desc: "validate tpl test 02", 196 }, 197 { 198 input: "/v1/{name=**}/a/b/c", 199 wantErr: errDeepWildcard, 200 desc: "validate tpl test 03", 201 }, 202 { 203 input: "/v1/**/a/b/c", 204 wantErr: errDeepWildcard, 205 desc: "validate tpl test 04", 206 }, 207 { 208 input: "/v1/a/b/c/{name=**/b}", 209 wantErr: errDeepWildcard, 210 desc: "validate tpl test 05", 211 }, 212 { 213 input: "/v1/a:b:c/{name=**}", 214 wantErr: nil, 215 desc: "validate tpl test 06", 216 }, 217 } { 218 _, gotErr := Parse(test.input) 219 require.Equal(t, errors.Unwrap(gotErr), test.wantErr, test.desc) 220 } 221 }