github.com/kaydxh/golang@v0.0.131/go/encoding/base64/base64_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package base64_test 23 24 import ( 25 "testing" 26 27 base64_ "github.com/kaydxh/golang/go/encoding/base64" 28 "gotest.tools/v3/assert" 29 ) 30 31 func TestString(t *testing.T) { 32 testCases := []struct { 33 name string 34 expected string 35 }{ 36 { 37 name: "hello word", 38 expected: "", 39 }, 40 { 41 name: "http://12306.com?a=%b", 42 expected: "", 43 }, 44 } 45 46 for _, testCase := range testCases { 47 t.Run(testCase.name, func(t *testing.T) { 48 encoded := base64_.EncodeString(testCase.name) 49 t.Logf("base64 encode : %v", encoded) 50 decoded, err := base64_.DecodeString(encoded) 51 if err != nil { 52 t.Fatalf("failed to decode string, err: %v", err) 53 } 54 55 assert.Equal(t, testCase.name, decoded) 56 }) 57 } 58 /* 59 content := "hello word" 60 61 encoded := base64_.EncodeString(content) 62 t.Logf("base64 encode : %v", encoded) 63 64 decoded, err := base64_.DecodeString(encoded) 65 if err != nil { 66 t.Fatalf("failed to decode string, err: %v", err) 67 } 68 t.Logf("base64 decode : %v", decoded) 69 */ 70 //assert.Equal(t, content, decoded) 71 } 72 73 func TestURL(t *testing.T) { 74 testCases := []struct { 75 name string 76 expected string 77 }{ 78 { 79 name: `http://baidu.com?a=10&b="hello"`, 80 expected: "", 81 }, 82 { 83 name: "http://12306.com?a=%b", 84 expected: "", 85 }, 86 } 87 88 for _, testCase := range testCases { 89 t.Run(testCase.name, func(t *testing.T) { 90 encoded := base64_.EncodeURL(testCase.name) 91 t.Logf("base64 encode : %v", encoded) 92 decoded, err := base64_.DecodeURL(encoded) 93 if err != nil { 94 t.Fatalf("failed to decode url, err: %v", err) 95 } 96 97 assert.Equal(t, testCase.name, decoded) 98 }) 99 } 100 }