github.com/polarismesh/polaris@v1.17.8/common/model/naming_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package model 19 20 import ( 21 "testing" 22 23 "github.com/golang/protobuf/ptypes/wrappers" 24 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 25 apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 // TestRateLimit_Labels2Arguments 测试标签转换 30 func TestRateLimit_Labels2Arguments(t *testing.T) { 31 rateLimit := &RateLimit{ 32 Proto: &apitraffic.Rule{}, 33 Labels: "{\"business.key\": {\"type\": 1, \"value\": {\"value\": \"1234567\"}}}", 34 } 35 _, err := rateLimit.Labels2Arguments() 36 assert.Nil(t, err) 37 assert.Equal(t, 1, len(rateLimit.Proto.Arguments)) 38 assert.Equal(t, apitraffic.MatchArgument_CUSTOM, rateLimit.Proto.Arguments[0].Type) 39 assert.Equal(t, "business.key", rateLimit.Proto.Arguments[0].Key) 40 assert.Equal(t, apimodel.MatchString_REGEX, rateLimit.Proto.Arguments[0].Value.Type) 41 assert.Equal(t, "1234567", rateLimit.Proto.Arguments[0].Value.Value.GetValue()) 42 } 43 44 // TestRateLimit_Arguments2Labels 测试参数转换标签 45 func TestRateLimit_Arguments2Labels(t *testing.T) { 46 rateLimit := &RateLimit{ 47 Proto: &apitraffic.Rule{ 48 Arguments: []*apitraffic.MatchArgument{ 49 { 50 Type: apitraffic.MatchArgument_CUSTOM, 51 Key: "business.key", 52 Value: &apimodel.MatchString{Type: apimodel.MatchString_EXACT, Value: &wrappers.StringValue{Value: "1234567"}}, 53 }, 54 { 55 Type: apitraffic.MatchArgument_METHOD, 56 Value: &apimodel.MatchString{Type: apimodel.MatchString_EXACT, Value: &wrappers.StringValue{Value: "/path"}}, 57 }, 58 { 59 Type: apitraffic.MatchArgument_HEADER, 60 Key: "host", 61 Value: &apimodel.MatchString{Type: apimodel.MatchString_EXACT, Value: &wrappers.StringValue{Value: "localhost"}}, 62 }, 63 { 64 Type: apitraffic.MatchArgument_QUERY, 65 Key: "name", 66 Value: &apimodel.MatchString{Type: apimodel.MatchString_EXACT, Value: &wrappers.StringValue{Value: "ok"}}, 67 }, 68 { 69 Type: apitraffic.MatchArgument_CALLER_SERVICE, 70 Key: "default", 71 Value: &apimodel.MatchString{Type: apimodel.MatchString_EXACT, Value: &wrappers.StringValue{Value: "svc"}}, 72 }, 73 { 74 Type: apitraffic.MatchArgument_CALLER_IP, 75 Value: &apimodel.MatchString{Type: apimodel.MatchString_EXACT, Value: &wrappers.StringValue{Value: "127.0.0.1"}}, 76 }, 77 }, 78 }, 79 } 80 labels := Arguments2Labels(rateLimit.Proto.GetArguments()) 81 if len(labels) > 0 { 82 rateLimit.Proto.Labels = labels 83 } 84 var hasValue bool 85 var value *apimodel.MatchString 86 value, hasValue = rateLimit.Proto.Labels["business.key"] 87 assert.True(t, hasValue) 88 assert.Equal(t, "1234567", value.Value.GetValue()) 89 90 value, hasValue = rateLimit.Proto.Labels["$method"] 91 assert.True(t, hasValue) 92 assert.Equal(t, "/path", value.Value.GetValue()) 93 94 value, hasValue = rateLimit.Proto.Labels["$header.host"] 95 assert.True(t, hasValue) 96 assert.Equal(t, "localhost", value.Value.GetValue()) 97 98 value, hasValue = rateLimit.Proto.Labels["$query.name"] 99 assert.True(t, hasValue) 100 assert.Equal(t, "ok", value.Value.GetValue()) 101 102 value, hasValue = rateLimit.Proto.Labels["$caller_service.default"] 103 assert.True(t, hasValue) 104 assert.Equal(t, "svc", value.Value.GetValue()) 105 106 value, hasValue = rateLimit.Proto.Labels["$caller_ip"] 107 assert.True(t, hasValue) 108 assert.Equal(t, "127.0.0.1", value.Value.GetValue()) 109 }