dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/directory/base/directory_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 base 19 20 import ( 21 "encoding/base64" 22 "fmt" 23 "testing" 24 ) 25 26 import ( 27 "github.com/stretchr/testify/assert" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/cluster/router/chain" 32 "dubbo.apache.org/dubbo-go/v3/common" 33 "dubbo.apache.org/dubbo-go/v3/common/constant" 34 ) 35 36 var ( 37 url, _ = common.NewURL( 38 fmt.Sprintf("dubbo://%s:%d/com.ikurento.user.UserProvider", constant.LocalHostValue, constant.DefaultPort)) 39 anyURL, _ = common.NewURL(fmt.Sprintf("condition://%s/com.foo.BarService", constant.AnyHostValue)) 40 ) 41 42 func TestNewBaseDirectory(t *testing.T) { 43 dir := NewDirectory(url) 44 assert.Equal(t, url, dir.GetURL()) 45 assert.Equal(t, url, dir.GetDirectoryUrl()) 46 } 47 48 func TestBuildRouterChain(t *testing.T) { 49 regURL := url 50 regURL.AddParam(constant.InterfaceKey, "mock-app") 51 directory := NewDirectory(regURL) 52 var err error 53 directory.routerChain, err = chain.NewRouterChain() 54 assert.Error(t, err) 55 } 56 57 func getRouteURL(rule string, u *common.URL) *common.URL { 58 ru := u 59 ru.AddParam("rule", rule) 60 ru.AddParam("force", "true") 61 ru.AddParam(constant.RouterKey, "router") 62 return ru 63 } 64 65 func TestIsProperRouter(t *testing.T) { 66 regURL := url 67 regURL.AddParam(constant.ApplicationKey, "mock-app") 68 d := NewDirectory(regURL) 69 localIP := common.GetLocalIp() 70 rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP)) 71 routeURL := getRouteURL(rule, anyURL) 72 routeURL.AddParam(constant.ApplicationKey, "mock-app") 73 rst := d.isProperRouter(routeURL) 74 assert.True(t, rst) 75 76 regURL.AddParam(constant.ApplicationKey, "") 77 regURL.AddParam(constant.InterfaceKey, "com.foo.BarService") 78 d = NewDirectory(regURL) 79 routeURL = getRouteURL(rule, anyURL) 80 routeURL.AddParam(constant.InterfaceKey, "com.foo.BarService") 81 rst = d.isProperRouter(routeURL) 82 assert.True(t, rst) 83 84 regURL.AddParam(constant.ApplicationKey, "") 85 regURL.AddParam(constant.InterfaceKey, "") 86 d = NewDirectory(regURL) 87 routeURL = getRouteURL(rule, anyURL) 88 rst = d.isProperRouter(routeURL) 89 assert.True(t, rst) 90 91 regURL.SetParam(constant.ApplicationKey, "") 92 regURL.SetParam(constant.InterfaceKey, "") 93 d = NewDirectory(regURL) 94 routeURL = getRouteURL(rule, anyURL) 95 routeURL.AddParam(constant.ApplicationKey, "mock-service") 96 rst = d.isProperRouter(routeURL) 97 assert.False(t, rst) 98 99 regURL.SetParam(constant.ApplicationKey, "") 100 regURL.SetParam(constant.InterfaceKey, "") 101 d = NewDirectory(regURL) 102 routeURL = getRouteURL(rule, anyURL) 103 routeURL.AddParam(constant.InterfaceKey, "mock-service") 104 rst = d.isProperRouter(routeURL) 105 assert.False(t, rst) 106 }