trpc.group/trpc-go/trpc-go@v1.0.3/restful/populate_util_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 restful_test 15 16 import ( 17 "errors" 18 "reflect" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "github.com/stretchr/testify/require" 23 "google.golang.org/protobuf/proto" 24 "google.golang.org/protobuf/testing/protocmp" 25 "google.golang.org/protobuf/types/known/fieldmaskpb" 26 27 "trpc.group/trpc-go/trpc-go/restful" 28 "trpc.group/trpc-go/trpc-go/testdata/restful/helloworld" 29 ) 30 31 func TestPopulateMessage(t *testing.T) { 32 for _, test := range []struct { 33 msg proto.Message 34 fieldPath []string 35 values []string 36 want proto.Message 37 wantErr bool 38 desc string 39 }{ 40 { 41 msg: &helloworld.HelloRequest{}, 42 fieldPath: nil, 43 values: nil, 44 want: &helloworld.HelloRequest{}, 45 wantErr: true, 46 desc: "populating message test empty fieldpath or fieldvalues", 47 }, 48 { 49 msg: &helloworld.HelloRequest{}, 50 fieldPath: []string{"foobar"}, 51 values: []string{"anything"}, 52 want: &helloworld.HelloRequest{}, 53 wantErr: true, 54 desc: "populating message test field name not found", 55 }, 56 { 57 msg: &helloworld.HelloRequest{}, 58 fieldPath: []string{"repeatedEnumValue", "x"}, 59 values: []string{"anything"}, 60 want: &helloworld.HelloRequest{}, 61 wantErr: true, 62 desc: "populating message test wrong fieldpath", 63 }, 64 { 65 msg: &helloworld.HelloRequest{}, 66 fieldPath: []string{"name"}, 67 values: []string{"name1", "name2"}, 68 want: &helloworld.HelloRequest{}, 69 wantErr: true, 70 desc: "populating message test populate field with multi values", 71 }, 72 { 73 msg: &helloworld.HelloRequest{}, 74 fieldPath: []string{"mapped_string_value"}, 75 values: []string{"key", "value", "anything"}, 76 want: &helloworld.HelloRequest{}, 77 wantErr: true, 78 desc: "populating message test populate map field with wrong len of values", 79 }, 80 { 81 msg: &helloworld.HelloRequest{}, 82 fieldPath: []string{"mapped_enum_value"}, 83 values: []string{"key", "value"}, 84 want: &helloworld.HelloRequest{}, 85 wantErr: true, 86 desc: "populating message test populate map field with invalid values", 87 }, 88 { 89 msg: &helloworld.HelloRequest{}, 90 fieldPath: []string{"enum_value"}, 91 values: []string{"11"}, 92 want: &helloworld.HelloRequest{}, 93 wantErr: true, 94 desc: "populating message test parse enum field with invalid values", 95 }, 96 { 97 msg: &helloworld.HelloRequest{}, 98 fieldPath: []string{"enum_value"}, 99 values: []string{"foo"}, 100 want: &helloworld.HelloRequest{}, 101 wantErr: true, 102 desc: "populating message test parse enum field with non numeric values", 103 }, 104 { 105 msg: &helloworld.HelloRequest{}, 106 fieldPath: []string{"primitive_bool_value"}, 107 values: []string{"foo"}, 108 want: &helloworld.HelloRequest{}, 109 wantErr: true, 110 desc: "populating message test parse bool type field with invalid values", 111 }, 112 { 113 msg: &helloworld.HelloRequest{}, 114 fieldPath: []string{"primitive_int32_value"}, 115 values: []string{"foo"}, 116 want: &helloworld.HelloRequest{}, 117 wantErr: true, 118 desc: "populating message test parse int32 type field with invalid values", 119 }, 120 { 121 msg: &helloworld.HelloRequest{}, 122 fieldPath: []string{"primitive_int64_value"}, 123 values: []string{"foo"}, 124 want: &helloworld.HelloRequest{}, 125 wantErr: true, 126 desc: "populating message test parse int64 type field with invalid values", 127 }, 128 { 129 msg: &helloworld.HelloRequest{}, 130 fieldPath: []string{"primitive_uint32_value"}, 131 values: []string{"foo"}, 132 want: &helloworld.HelloRequest{}, 133 wantErr: true, 134 desc: "populating message test parse uint32 type field with invalid values", 135 }, 136 { 137 msg: &helloworld.HelloRequest{}, 138 fieldPath: []string{"primitive_uint64_value"}, 139 values: []string{"foo"}, 140 want: &helloworld.HelloRequest{}, 141 wantErr: true, 142 desc: "populating message test parse uint64 type field with invalid values", 143 }, 144 { 145 msg: &helloworld.HelloRequest{}, 146 fieldPath: []string{"primitive_float_value"}, 147 values: []string{"foo"}, 148 want: &helloworld.HelloRequest{}, 149 wantErr: true, 150 desc: "populating message test parse float type field with invalid values", 151 }, 152 { 153 msg: &helloworld.HelloRequest{}, 154 fieldPath: []string{"primitive_double_value"}, 155 values: []string{"foo"}, 156 want: &helloworld.HelloRequest{}, 157 wantErr: true, 158 desc: "populating message test parse double type field with invalid values", 159 }, 160 { 161 msg: &helloworld.HelloRequest{}, 162 fieldPath: []string{"primitive_bytes_value"}, 163 values: []string{"foo"}, 164 want: &helloworld.HelloRequest{}, 165 wantErr: true, 166 desc: "populating message test parse bytes type field with invalid values", 167 }, 168 { 169 msg: &helloworld.HelloRequest{}, 170 fieldPath: []string{"mask_value"}, 171 values: []string{"a.b.c,d.e.f"}, 172 want: &helloworld.HelloRequest{ 173 MaskValue: &fieldmaskpb.FieldMask{Paths: []string{"a.b.c", "d.e.f"}}, 174 }, 175 wantErr: false, 176 desc: "populating message test parse fieldmask type field", 177 }, 178 } { 179 err := restful.PopulateMessage(test.msg, test.fieldPath, test.values) 180 if test.wantErr { 181 require.NotNil(t, err, test.desc) 182 } else { 183 require.Nil(t, err, test.desc) 184 require.Equal(t, "", cmp.Diff(test.want, test.msg, protocmp.Transform()), test.desc) 185 } 186 } 187 } 188 189 func TestPopulateMessageWrappedField(t *testing.T) { 190 for _, test := range []struct { 191 msg proto.Message 192 fieldPath []string 193 values []string 194 want proto.Message 195 wantErr bool 196 desc string 197 }{ 198 { 199 msg: &helloworld.HelloRequest{}, 200 fieldPath: []string{"wrapped_bool_value"}, 201 values: []string{"foo"}, 202 want: &helloworld.HelloRequest{}, 203 wantErr: true, 204 desc: "populating message test parse wrapped bool type field with invalid values", 205 }, 206 { 207 msg: &helloworld.HelloRequest{}, 208 fieldPath: []string{"wrapped_float_value"}, 209 values: []string{"foo"}, 210 want: &helloworld.HelloRequest{}, 211 wantErr: true, 212 desc: "populating message test parse wrapped float type field with invalid values", 213 }, 214 { 215 msg: &helloworld.HelloRequest{}, 216 fieldPath: []string{"wrapped_double_value"}, 217 values: []string{"foo"}, 218 want: &helloworld.HelloRequest{}, 219 wantErr: true, 220 desc: "populating message test parse wrapped double type field with invalid values", 221 }, 222 { 223 msg: &helloworld.HelloRequest{}, 224 fieldPath: []string{"wrapped_int32_value"}, 225 values: []string{"foo"}, 226 want: &helloworld.HelloRequest{}, 227 wantErr: true, 228 desc: "populating message test parse wrapped int32 type field with invalid values", 229 }, 230 { 231 msg: &helloworld.HelloRequest{}, 232 fieldPath: []string{"wrapped_int64_value"}, 233 values: []string{"foo"}, 234 want: &helloworld.HelloRequest{}, 235 wantErr: true, 236 desc: "populating message test parse wrapped int64 type field with invalid values", 237 }, 238 { 239 msg: &helloworld.HelloRequest{}, 240 fieldPath: []string{"wrapped_uint32_value"}, 241 values: []string{"foo"}, 242 want: &helloworld.HelloRequest{}, 243 wantErr: true, 244 desc: "populating message test parse wrapped uint32 type field with invalid values", 245 }, 246 { 247 msg: &helloworld.HelloRequest{}, 248 fieldPath: []string{"wrapped_uint64_value"}, 249 values: []string{"foo"}, 250 want: &helloworld.HelloRequest{}, 251 wantErr: true, 252 desc: "populating message test parse wrapped uint64 type field with invalid values", 253 }, 254 { 255 msg: &helloworld.HelloRequest{}, 256 fieldPath: []string{"wrapped_bytes_value"}, 257 values: []string{"foo"}, 258 want: &helloworld.HelloRequest{}, 259 wantErr: true, 260 desc: "populating message test parse wrapped bytes type field with invalid values", 261 }, 262 } { 263 err := restful.PopulateMessage(test.msg, test.fieldPath, test.values) 264 if test.wantErr { 265 require.NotNil(t, err, test.desc) 266 } else { 267 require.Nil(t, err, test.desc) 268 require.True(t, reflect.DeepEqual(test.want, test.msg), test.desc) 269 } 270 } 271 } 272 273 func TestPopulateMessageTraverseNotFound(t *testing.T) { 274 msg := &helloworld.HelloRequest{} 275 fieldPath := []string{"foobar"} 276 values := []string{"anything"} 277 err := restful.PopulateMessage(msg, fieldPath, values) 278 require.True(t, errors.Is(err, restful.ErrTraverseNotFound)) 279 }