dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/common/utils_test.go (about) 1 // 2 // Copyright (C) 2023 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package common 7 8 import ( 9 "net/url" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestURLEncode(t *testing.T) { 17 tests := []struct { 18 name string 19 input string 20 output string 21 }{ 22 {"valid", "^[this]+{is}?test:string*#", "%5E%5Bthis%5D%2B%7Bis%7D%3Ftest:string%2A%23"}, 23 {"valid - special character", "this-is_test.string~哈囉世界< >/!#%^*()+,`@$&", "this%2Dis%5Ftest%2Estring%7E%E5%93%88%E5%9B%89%E4%B8%96%E7%95%8C%3C%20%3E%2F%21%23%25%5E%2A%28%29%2B%2C%60@$&"}, 24 } 25 for _, tt := range tests { 26 t.Run(tt.name, func(t *testing.T) { 27 res := URLEncode(tt.input) 28 assert.Equal(t, tt.output, res) 29 30 unescaped, err := url.PathUnescape(tt.output) 31 require.NoError(t, err) 32 assert.Equal(t, tt.input, unescaped) 33 }) 34 } 35 } 36 37 func TestPathBuild(t *testing.T) { 38 tests := []struct { 39 name string 40 enableNameFieldEscape bool 41 prefixPath string 42 deviceServiceName string 43 deviceName string 44 expectedPath string 45 }{ 46 { 47 "valid with name filed escape", true, 48 "edgex/system-events/core-metadata/device/add", "^[this]+{is}?test:string*#", "this-is_test.string~哈囉世界< >/!#%^*()+,`@$&", 49 "edgex/system-events/core-metadata/device/add/%5E%5Bthis%5D%2B%7Bis%7D%3Ftest:string%2A%23/this%2Dis%5Ftest%2Estring%7E%E5%93%88%E5%9B%89%E4%B8%96%E7%95%8C%3C%20%3E%2F%21%23%25%5E%2A%28%29%2B%2C%60@$&", 50 }, 51 { 52 "valid without name filed escape", false, 53 "edgex/system-events/core-metadata/device/add", "device-onvif-camera", "camera-device", 54 "edgex/system-events/core-metadata/device/add/device-onvif-camera/camera-device"}, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 res := NewPathBuilder().EnableNameFieldEscape(tt.enableNameFieldEscape). 59 SetPath(tt.prefixPath).SetNameFieldPath(tt.deviceServiceName).SetNameFieldPath(tt.deviceName).BuildPath() 60 assert.Equal(t, tt.expectedPath, res) 61 }) 62 } 63 }