dubbo.apache.org/dubbo-go/v3@v3.1.1/common/url_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package common 19 20 import ( 21 "encoding/base64" 22 "net/url" 23 "testing" 24 ) 25 26 import ( 27 "github.com/stretchr/testify/assert" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/common/constant" 32 ) 33 34 const ( 35 userName = "username" 36 password = "password" 37 loopbackAddress = "127.0.0.1" 38 ) 39 40 func TestNewURLWithOptions(t *testing.T) { 41 methods := []string{"Methodone,methodtwo"} 42 params := url.Values{} 43 params.Set("key", "value") 44 u := NewURLWithOptions(WithPath("com.test.Service"), 45 WithUsername(userName), 46 WithPassword(password), 47 WithProtocol("testprotocol"), 48 WithIp(loopbackAddress), 49 WithPort("8080"), 50 WithMethods(methods), 51 WithParams(params), 52 WithParamsValue("key2", "value2")) 53 assert.Equal(t, "/com.test.Service", u.Path) 54 assert.Equal(t, userName, u.Username) 55 assert.Equal(t, password, u.Password) 56 assert.Equal(t, "testprotocol", u.Protocol) 57 assert.Equal(t, loopbackAddress, u.Ip) 58 assert.Equal(t, "8080", u.Port) 59 assert.Equal(t, methods, u.Methods) 60 assert.Equal(t, 2, len(u.params)) 61 } 62 63 func TestURL(t *testing.T) { 64 u, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 65 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 66 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 67 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 68 "side=provider&timeout=3000×tamp=1556509797245") 69 assert.NoError(t, err) 70 71 urlInst := URL{} 72 urlInst.noCopy.Lock() 73 urlInst.SetParam("hello", "world") 74 urlInst.noCopy.Unlock() 75 76 assert.Equal(t, "/com.ikurento.user.UserProvider", u.Path) 77 assert.Equal(t, "127.0.0.1:20000", u.Location) 78 assert.Equal(t, "dubbo", u.Protocol) 79 assert.Equal(t, loopbackAddress, u.Ip) 80 assert.Equal(t, "20000", u.Port) 81 assert.Equal(t, urlInst.Methods, u.Methods) 82 assert.Equal(t, "", u.Username) 83 assert.Equal(t, "", u.Password) 84 assert.Equal(t, "anyhost=true&application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-"+ 85 "provider-golang-1.0.0&environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%"+ 86 "2C&module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&t"+ 87 "imestamp=1556509797245", u.params.Encode()) 88 89 assert.Equal(t, "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&application=BDTServi"+ 90 "ce&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&environment=dev&interface=com.ikure"+ 91 "nto.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&module=dubbogo+user-info+server&org=ikurento.com&owner="+ 92 "ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245", u.String()) 93 } 94 95 func TestURLWithoutSchema(t *testing.T) { 96 u, err := NewURL("127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+ 97 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ 98 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ 99 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+ 100 "side=provider&timeout=3000×tamp=1556509797245", WithProtocol("dubbo")) 101 assert.NoError(t, err) 102 103 assert.Equal(t, "/com.ikurento.user.UserProvider", u.Path) 104 assert.Equal(t, "127.0.0.1:20000", u.Location) 105 assert.Equal(t, "dubbo", u.Protocol) 106 assert.Equal(t, loopbackAddress, u.Ip) 107 assert.Equal(t, "20000", u.Port) 108 assert.Equal(t, URL{}.Methods, u.Methods) 109 assert.Equal(t, "", u.Username) 110 assert.Equal(t, "", u.Password) 111 assert.Equal(t, "anyhost=true&application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-"+ 112 "provider-golang-1.0.0&environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%"+ 113 "2C&module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&t"+ 114 "imestamp=1556509797245", u.params.Encode()) 115 116 assert.Equal(t, "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&application=BDTServi"+ 117 "ce&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&environment=dev&interface=com.ikure"+ 118 "nto.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&module=dubbogo+user-info+server&org=ikurento.com&owner="+ 119 "ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245", u.String()) 120 } 121 122 func TestURLEqual(t *testing.T) { 123 u1, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0") 124 assert.NoError(t, err) 125 u2, err := NewURL("dubbo://127.0.0.2:20001/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0") 126 assert.NoError(t, err) 127 assert.True(t, u1.URLEqual(u2)) 128 129 u3, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=gg&version=2.6.0") 130 assert.NoError(t, err) 131 assert.False(t, u1.URLEqual(u3)) 132 133 // urlGroupAnyValue's group is * 134 urlGroupAnyValue, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=*&version=2.6.0") 135 assert.NoError(t, err) 136 assert.True(t, u3.URLEqual(urlGroupAnyValue)) 137 138 // test for enabled 139 urlEnabledEmpty, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=*&version=2.6.0&enabled=") 140 assert.NoError(t, err) 141 assert.True(t, u3.URLEqual(urlEnabledEmpty)) 142 143 urlEnabledFalse, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=*&version=2.6.0&enabled=1") 144 assert.NoError(t, err) 145 assert.False(t, u3.URLEqual(urlEnabledFalse)) 146 147 urlEnabledTrue, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=*&version=2.6.0&enabled=true") 148 assert.NoError(t, err) 149 assert.True(t, u3.URLEqual(urlEnabledTrue)) 150 151 urlEnabledAny, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=*&version=2.6.0&enabled=*") 152 assert.NoError(t, err) 153 assert.True(t, u3.URLEqual(urlEnabledAny)) 154 155 // test for category 156 categoryAny, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=*&version=2.6.0&enabled=*&category=*") 157 assert.NoError(t, err) 158 assert.True(t, categoryAny.URLEqual(u3)) 159 } 160 161 func TestURLGetParam(t *testing.T) { 162 params := url.Values{} 163 params.Set("key", "value") 164 165 u := URL{} 166 u.SetParams(params) 167 168 v := u.GetParam("key", "default") 169 assert.Equal(t, "value", v) 170 171 u = URL{} 172 v = u.GetParam("key", "default") 173 assert.Equal(t, "default", v) 174 } 175 176 func TestURLGetParamInt(t *testing.T) { 177 params := url.Values{} 178 params.Set("key", "value") 179 180 u := URL{} 181 u.SetParams(params) 182 183 v := u.GetParamInt("key", 1) 184 assert.Equal(t, int64(1), v) 185 186 u = URL{} 187 v = u.GetParamInt("key", 1) 188 assert.Equal(t, int64(1), v) 189 } 190 191 func TestURLGetParamIntValue(t *testing.T) { 192 params := url.Values{} 193 params.Set("key", "0") 194 195 u := URL{} 196 u.SetParams(params) 197 198 v := u.GetParamInt("key", 1) 199 assert.Equal(t, int64(0), v) 200 201 u = URL{} 202 v = u.GetParamInt("key", 1) 203 assert.Equal(t, int64(1), v) 204 } 205 206 func TestURLGetParamBool(t *testing.T) { 207 params := url.Values{} 208 params.Set("force", "true") 209 210 u := URL{} 211 u.SetParams(params) 212 213 v := u.GetParamBool("force", false) 214 assert.Equal(t, true, v) 215 216 u = URL{} 217 v = u.GetParamBool("force", false) 218 assert.Equal(t, false, v) 219 } 220 221 func TestURLGetParamAndDecoded(t *testing.T) { 222 rule := "host = 2.2.2.2,1.1.1.1,3.3.3.3 & host !=1.1.1.1 => host = 1.2.3.4" 223 params := url.Values{} 224 params.Set("rule", base64.URLEncoding.EncodeToString([]byte(rule))) 225 226 u := URL{} 227 u.SetParams(params) 228 229 v, _ := u.GetParamAndDecoded("rule") 230 assert.Equal(t, rule, v) 231 } 232 233 func TestURLGetRawParam(t *testing.T) { 234 u, _ := NewURL("condition://0.0.0.0:8080/com.foo.BarService?serialization=fastjson") 235 u.Username = "test" 236 u.Password = "test" 237 assert.Equal(t, "condition", u.GetRawParam("protocol")) 238 assert.Equal(t, "0.0.0.0", u.GetRawParam("host")) 239 assert.Equal(t, "8080", u.GetRawParam("port")) 240 assert.Equal(t, "test", u.GetRawParam(userName)) 241 assert.Equal(t, "test", u.GetRawParam(password)) 242 assert.Equal(t, "/com.foo.BarService", u.GetRawParam("path")) 243 assert.Equal(t, "fastjson", u.GetRawParam("serialization")) 244 } 245 246 func TestURLToMap(t *testing.T) { 247 u, _ := NewURL("condition://0.0.0.0:8080/com.foo.BarService?serialization=fastjson") 248 u.Username = "test" 249 u.Password = "test" 250 251 m := u.ToMap() 252 assert.Equal(t, 7, len(m)) 253 assert.Equal(t, "condition", m["protocol"]) 254 assert.Equal(t, "0.0.0.0", m["host"]) 255 assert.Equal(t, "8080", m["port"]) 256 assert.Equal(t, "test", m[userName]) 257 assert.Equal(t, "test", m[password]) 258 assert.Equal(t, "/com.foo.BarService", m["path"]) 259 assert.Equal(t, "fastjson", m["serialization"]) 260 } 261 262 func TestURLGetMethodParamInt(t *testing.T) { 263 params := url.Values{} 264 params.Set("methods.GetValue.timeout", "3") 265 266 u := URL{} 267 u.SetParams(params) 268 269 v := u.GetMethodParamInt("GetValue", "timeout", 1) 270 assert.Equal(t, int64(3), v) 271 272 u = URL{} 273 v = u.GetMethodParamInt("GetValue", "timeout", 1) 274 assert.Equal(t, int64(1), v) 275 } 276 277 func TestURLGetMethodParam(t *testing.T) { 278 params := url.Values{} 279 params.Set("methods.GetValue.timeout", "3s") 280 281 u := URL{} 282 u.SetParams(params) 283 284 v := u.GetMethodParam("GetValue", "timeout", "1s") 285 assert.Equal(t, "3s", v) 286 287 u = URL{} 288 v = u.GetMethodParam("GetValue", "timeout", "1s") 289 assert.Equal(t, "1s", v) 290 } 291 292 func TestURLGetMethodParamBool(t *testing.T) { 293 params := url.Values{} 294 params.Set("methods.GetValue.async", "true") 295 296 u := URL{} 297 u.SetParams(params) 298 299 v := u.GetMethodParamBool("GetValue", "async", false) 300 assert.Equal(t, true, v) 301 302 u = URL{} 303 v = u.GetMethodParamBool("GetValue2", "async", false) 304 assert.Equal(t, false, v) 305 } 306 307 func TestMergeUrl(t *testing.T) { 308 referenceUrlParams := url.Values{} 309 referenceUrlParams.Set(constant.ClusterKey, "random") 310 referenceUrlParams.Set(constant.RetriesKey, "1") 311 referenceUrlParams.Set("test3", "1") 312 referenceUrlParams.Set("methods.testMethod."+constant.RetriesKey, "1") 313 serviceUrlParams := url.Values{} 314 serviceUrlParams.Set("test2", "1") 315 serviceUrlParams.Set(constant.ClusterKey, "roundrobin") 316 serviceUrlParams.Set(constant.RetriesKey, "2") 317 serviceUrlParams.Set(constant.MethodKeys+".testMethod."+constant.RetriesKey, "2") 318 referenceUrl, _ := NewURL("mock1://127.0.0.1:1111", WithParams(referenceUrlParams), WithMethods([]string{"testMethod"})) 319 serviceUrl, _ := NewURL("mock2://127.0.0.1:20000", WithParams(serviceUrlParams)) 320 321 mergedUrl := MergeURL(serviceUrl, referenceUrl) 322 assert.Equal(t, "random", mergedUrl.GetParam(constant.ClusterKey, "")) 323 assert.Equal(t, "1", mergedUrl.GetParam("test2", "")) 324 assert.Equal(t, "1", mergedUrl.GetParam("test3", "")) 325 assert.Equal(t, "1", mergedUrl.GetParam(constant.RetriesKey, "")) 326 assert.Equal(t, "1", mergedUrl.GetParam(constant.MethodKeys+".testMethod."+constant.RetriesKey, "")) 327 } 328 329 func TestURLSetParams(t *testing.T) { 330 u1, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&configVersion=1.0") 331 assert.NoError(t, err) 332 params := url.Values{} 333 params.Set("key", "3") 334 u1.SetParams(params) 335 assert.Equal(t, "3", u1.GetParam("key", "")) 336 assert.Equal(t, "2.6.0", u1.GetParam("version", "")) 337 } 338 339 func TestURLReplaceParams(t *testing.T) { 340 u1, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&configVersion=1.0") 341 assert.NoError(t, err) 342 params := url.Values{} 343 params.Set("key", "3") 344 u1.ReplaceParams(params) 345 assert.Equal(t, "3", u1.GetParam("key", "")) 346 assert.Equal(t, "", u1.GetParam("version", "")) 347 } 348 349 func TestClone(t *testing.T) { 350 u1, err := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&configVersion=1.0") 351 assert.NoError(t, err) 352 u2 := u1.Clone() 353 assert.Equal(t, u2.Protocol, "dubbo") 354 assert.Equal(t, "1.0", u2.GetParam("configVersion", "")) 355 u2.Protocol = "provider" 356 assert.Equal(t, u1.Protocol, "dubbo") 357 assert.Equal(t, u2.Protocol, "provider") 358 } 359 360 func TestColonSeparatedKey(t *testing.T) { 361 u1, _ := NewURL("dubbo://127.0.0.1:20000") 362 u1.AddParam(constant.InterfaceKey, "com.ikurento.user.UserProvider") 363 364 assert.Equal(t, u1.ColonSeparatedKey(), u1.GetParam(constant.InterfaceKey, "")+"::") 365 u1.AddParam(constant.VersionKey, "version1") 366 assert.Equal(t, u1.ColonSeparatedKey(), u1.GetParam(constant.InterfaceKey, "")+":version1:") 367 u1.AddParam(constant.GroupKey, "group1") 368 assert.Equal(t, u1.ColonSeparatedKey(), u1.GetParam(constant.InterfaceKey, "")+":version1:group1") 369 u1.SetParam(constant.VersionKey, "") 370 assert.Equal(t, u1.ColonSeparatedKey(), u1.GetParam(constant.InterfaceKey, "")+"::group1") 371 } 372 373 func TestCompareURLEqualFunc(t *testing.T) { 374 // test Default 375 url1, _ := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 376 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 377 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 378 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 379 "side=provider&timeout=3000×tamp=1556509797245") 380 url2, _ := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 381 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 382 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 383 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 384 "side=provider&timeout=3000×tamp=155650979798") 385 assert.False(t, GetCompareURLEqualFunc()(url1, url2)) 386 assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TimestampKey, constant.RemoteTimestampKey)) 387 388 // test custom 389 url1, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 390 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 391 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 392 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 393 "side=provider&timeout=3000×tamp=1556509797245") 394 url2, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 395 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 396 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 397 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 398 "side=provider&timeout=3000×tamp=155650979798") 399 assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TimestampKey, constant.RemoteTimestampKey)) 400 SetCompareURLEqualFunc(CustomCompareURLEqual) 401 assert.False(t, GetCompareURLEqualFunc()(url1, url2)) 402 assert.False(t, GetCompareURLEqualFunc()(url1, url2, constant.TimestampKey, constant.RemoteTimestampKey)) 403 404 url1, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 405 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 406 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 407 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 408 "side=provider&timeout=3000") 409 url2, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 410 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 411 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 412 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 413 "side=provider&timeout=3000") 414 assert.True(t, GetCompareURLEqualFunc()(url1, url2)) 415 assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TimestampKey, constant.RemoteTimestampKey)) 416 SetCompareURLEqualFunc(CustomCompareURLEqual) 417 assert.True(t, GetCompareURLEqualFunc()(url1, url2)) 418 assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TimestampKey, constant.RemoteTimestampKey)) 419 } 420 421 func CustomCompareURLEqual(l *URL, r *URL, execludeParam ...string) bool { 422 return l.PrimitiveURL == r.PrimitiveURL 423 } 424 425 func TestParseServiceKey(t *testing.T) { 426 type args struct { 427 serviceKey string 428 } 429 tests := []struct { 430 name string 431 args args 432 want string 433 want1 string 434 want2 string 435 }{ 436 { 437 name: "test1", 438 args: args{ 439 serviceKey: "group/interface:version", 440 }, 441 want: "interface", 442 want1: "group", 443 want2: "version", 444 }, 445 { 446 name: "test2", 447 args: args{ 448 serviceKey: "*/*:*", 449 }, 450 want: "*", 451 want1: "*", 452 want2: "*", 453 }, 454 { 455 name: "test3", 456 args: args{ 457 serviceKey: "group/org.apache.dubbo.mock.api.MockService", 458 }, 459 want: "org.apache.dubbo.mock.api.MockService", 460 want1: "group", 461 want2: "", 462 }, 463 { 464 name: "test4", 465 args: args{ 466 serviceKey: "org.apache.dubbo.mock.api.MockService", 467 }, 468 want: "org.apache.dubbo.mock.api.MockService", 469 want1: "", 470 want2: "", 471 }, 472 { 473 name: "test5", 474 args: args{ 475 serviceKey: "group/", 476 }, 477 want: "", 478 want1: "group", 479 want2: "", 480 }, 481 } 482 for _, tt := range tests { 483 t.Run(tt.name, func(t *testing.T) { 484 got, got1, got2 := ParseServiceKey(tt.args.serviceKey) 485 assert.Equalf(t, tt.want, got, "ParseServiceKey(%v)", tt.args.serviceKey) 486 assert.Equalf(t, tt.want1, got1, "ParseServiceKey(%v)", tt.args.serviceKey) 487 assert.Equalf(t, tt.want2, got2, "ParseServiceKey(%v)", tt.args.serviceKey) 488 }) 489 } 490 } 491 492 func TestIsAnyCondition(t *testing.T) { 493 type args struct { 494 intf string 495 group string 496 version string 497 serviceURL *URL 498 } 499 serviceURL, _ := NewURL(GetLocalIp()+":0", WithProtocol("admin"), WithParams(url.Values{ 500 constant.GroupKey: {"group"}, 501 constant.VersionKey: {"version"}, 502 })) 503 tests := []struct { 504 name string 505 args args 506 want bool 507 }{ 508 { 509 name: "test1", 510 args: args{ 511 intf: constant.AnyValue, 512 group: constant.AnyValue, 513 version: constant.AnyValue, 514 serviceURL: serviceURL, 515 }, 516 want: true, 517 }, 518 { 519 name: "test2", 520 args: args{ 521 intf: constant.AnyValue, 522 group: "group", 523 version: "version", 524 serviceURL: serviceURL, 525 }, 526 want: true, 527 }, 528 { 529 name: "test3", 530 args: args{ 531 intf: "intf", 532 group: constant.AnyValue, 533 version: constant.AnyValue, 534 serviceURL: serviceURL, 535 }, 536 want: false, 537 }, 538 { 539 name: "test4", 540 args: args{ 541 intf: constant.AnyValue, 542 group: "group1", 543 version: constant.AnyValue, 544 serviceURL: serviceURL, 545 }, 546 want: false, 547 }, 548 } 549 for _, tt := range tests { 550 t.Run(tt.name, func(t *testing.T) { 551 assert.Equalf(t, tt.want, IsAnyCondition(tt.args.intf, tt.args.group, tt.args.version, tt.args.serviceURL), "IsAnyCondition(%v, %v, %v, %v)", tt.args.intf, tt.args.group, tt.args.version, tt.args.serviceURL) 552 }) 553 } 554 }