github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/utils/cputil/urlquery_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cputil 16 17 import ( 18 "encoding/base64" 19 "fmt" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 24 "github.com/erda-project/erda-infra/providers/component-protocol/cptype" 25 ) 26 27 func TestGetURLQuery(t *testing.T) { 28 type FilterURLQueryStruct struct { 29 Name string `json:"name,omitempty"` 30 Age int `json:"age,omitempty"` 31 } 32 33 type args struct { 34 sdk *cptype.SDK 35 resultStructPtr interface{} 36 } 37 tests := []struct { 38 name string 39 args args 40 wantErr bool 41 }{ 42 { 43 name: "nil inParams", 44 args: args{ 45 sdk: &cptype.SDK{InParams: nil}, 46 resultStructPtr: nil, 47 }, 48 wantErr: false, 49 }, 50 { 51 name: "inParams not nil, but url query is empty", 52 args: args{ 53 sdk: &cptype.SDK{ 54 InParams: cptype.InParams{MakeCompURLQueryKey("c1"): ""}, 55 Comp: &cptype.Component{Name: "c1"}, 56 }, 57 resultStructPtr: nil, 58 }, 59 wantErr: true, 60 }, 61 { 62 name: "normal", 63 args: args{ 64 sdk: &cptype.SDK{ 65 InParams: cptype.InParams{ 66 MakeCompURLQueryKey("c1"): func() string { 67 q := FilterURLQueryStruct{Name: "bob", Age: 20} 68 b, err := json.Marshal(&q) 69 assert.NoError(t, err) 70 base64URLQuery := base64.URLEncoding.EncodeToString(b) 71 return base64URLQuery 72 }(), 73 }, 74 Comp: &cptype.Component{Name: "c1"}, 75 }, 76 resultStructPtr: &FilterURLQueryStruct{}, 77 }, 78 wantErr: false, 79 }, 80 } 81 for _, tt := range tests { 82 t.Run(tt.name, func(t *testing.T) { 83 err := GetURLQuery(tt.args.sdk, tt.args.resultStructPtr) 84 if err != nil { 85 t.Log(err) 86 } 87 assert.Equal(t, err != nil, tt.wantErr) 88 fmt.Println(tt.args.resultStructPtr) 89 }) 90 } 91 } 92 93 func TestEmptyBase64Decode(t *testing.T) { 94 _, err := base64.StdEncoding.DecodeString("") 95 assert.NoError(t, err) 96 }