github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_urldecode_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 tu "github.com/GuanceCloud/cliutils/testutil" 15 ) 16 17 func TestURLDecode(t *testing.T) { 18 cases := []struct { 19 name string 20 pl, in string 21 outKey string 22 expected interface{} 23 fail bool 24 }{ 25 { 26 name: "normal", 27 pl: `json(_, url) 28 url_decode(url)`, 29 in: `{"url":"http%3a%2f%2fwww.baidu.com%2fs%3fwd%3d%e6%b5%8b%e8%af%95"}`, 30 outKey: "url", 31 expected: "http://www.baidu.com/s?wd=测试", 32 fail: false, 33 }, 34 35 { 36 name: "normal", 37 pl: `json(_, url) 38 url_decode(url)`, 39 in: `{"url":"https:%2F%2Fkubernetes.io%2Fdocs%2Freference%2Faccess-authn-authz%2Fbootstrap-tokens%2F"}`, 40 outKey: "url", 41 expected: "https://kubernetes.io/docs/reference/access-authn-authz/bootstrap-tokens/", 42 fail: false, 43 }, 44 45 { 46 name: "non-existed key", 47 pl: `json(_, url) 48 url_decode(link)`, 49 in: `{"url":"https:%2F%2Fkubernetes.io%2Fdocs%2Freference%2Faccess-authn-authz%2Fbootstrap-tokens%2F"}`, 50 outKey: "link", 51 expected: nil, 52 fail: true, 53 }, 54 55 { 56 name: "invalid arg", 57 pl: `url_decode("https:%2F%2Fkubernetes.io%2Fdocs%2Freference%2Faccess-authn-authz%2Fbootstrap-tokens%2F")`, 58 in: `{"url":"https:%2F%2Fkubernetes.io%2Fdocs%2Freference%2Faccess-authn-authz%2Fbootstrap-tokens%2F"}`, 59 fail: true, 60 }, 61 } 62 63 for idx, tc := range cases { 64 t.Run(tc.name, func(t *testing.T) { 65 runner, err := NewTestingRunner(tc.pl) 66 if err != nil { 67 if tc.fail { 68 t.Logf("[%d]expect error: %s", idx, err) 69 } else { 70 t.Errorf("[%d] failed: %s", idx, err) 71 } 72 return 73 } 74 75 pt := ptinput.NewPlPoint( 76 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 77 errR := runScript(runner, pt) 78 79 if errR != nil { 80 t.Fatal(errR.Error()) 81 } 82 83 if v, _, err := pt.Get(tc.outKey); err != nil { 84 if !tc.fail { 85 t.Errorf("[%d]expect error", idx) 86 } 87 } else { 88 tu.Equals(t, tc.expected, v) 89 t.Logf("[%d] PASS", idx) 90 } 91 }) 92 } 93 }