github.com/cloudwego/hertz@v0.9.3/pkg/common/utils/path_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * The MIT License (MIT) 16 * 17 * Copyright (c) 2014 Manuel MartÃnez-Almeida 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining a copy 20 * of this software and associated documentation files (the "Software"), to deal 21 * in the Software without restriction, including without limitation the rights 22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 23 * copies of the Software, and to permit persons to whom the Software is 24 * furnished to do so, subject to the following conditions: 25 * 26 * The above copyright notice and this permission notice shall be included in 27 * all copies or substantial portions of the Software. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 35 * THE SOFTWARE. 36 * 37 * This file may have been modified by CloudWeGo authors. All CloudWeGo 38 * Modifications are Copyright 2022 CloudWeGo Authors 39 */ 40 41 package utils 42 43 import ( 44 "testing" 45 46 "github.com/cloudwego/hertz/pkg/common/test/assert" 47 ) 48 49 func TestPathCleanPath(t *testing.T) { 50 normalPath := "/Foo/Bar/go/src/github.com/cloudwego/hertz/pkg/common/utils/path_test.go" 51 expectedNormalPath := "/Foo/Bar/go/src/github.com/cloudwego/hertz/pkg/common/utils/path_test.go" 52 cleanNormalPath := CleanPath(normalPath) 53 assert.DeepEqual(t, expectedNormalPath, cleanNormalPath) 54 55 singleDotPath := "/Foo/Bar/./././go/src" 56 expectedSingleDotPath := "/Foo/Bar/go/src" 57 cleanSingleDotPath := CleanPath(singleDotPath) 58 assert.DeepEqual(t, expectedSingleDotPath, cleanSingleDotPath) 59 60 doubleDotPath := "../../.." 61 expectedDoubleDotPath := "/" 62 cleanDoublePotPath := CleanPath(doubleDotPath) 63 assert.DeepEqual(t, expectedDoubleDotPath, cleanDoublePotPath) 64 65 // MultiDot can be treated as a file name 66 multiDotPath := "/../...." 67 expectedMultiDotPath := "/...." 68 cleanMultiDotPath := CleanPath(multiDotPath) 69 assert.DeepEqual(t, expectedMultiDotPath, cleanMultiDotPath) 70 71 nullPath := "" 72 expectedNullPath := "/" 73 cleanNullPath := CleanPath(nullPath) 74 assert.DeepEqual(t, expectedNullPath, cleanNullPath) 75 76 relativePath := "/Foo/Bar/../go/src/../../github.com/cloudwego/hertz" 77 expectedRelativePath := "/Foo/github.com/cloudwego/hertz" 78 cleanRelativePath := CleanPath(relativePath) 79 assert.DeepEqual(t, expectedRelativePath, cleanRelativePath) 80 81 multiSlashPath := "///////Foo//Bar////go//src/github.com/cloudwego/hertz//.." 82 expectedMultiSlashPath := "/Foo/Bar/go/src/github.com/cloudwego" 83 cleanMultiSlashPath := CleanPath(multiSlashPath) 84 assert.DeepEqual(t, expectedMultiSlashPath, cleanMultiSlashPath) 85 86 inputPath := "/Foo/Bar/go/src/github.com/cloudwego/hertz/pkg/common/utils/path_test.go/." 87 expectedPath := "/Foo/Bar/go/src/github.com/cloudwego/hertz/pkg/common/utils/path_test.go/" 88 cleanedPath := CleanPath(inputPath) 89 assert.DeepEqual(t, expectedPath, cleanedPath) 90 } 91 92 // The Function AddMissingPort can only add the missed port, don't consider the other error case. 93 func TestPathAddMissingPort(t *testing.T) { 94 ipList := []string{"127.0.0.1", "111.111.1.1", "[0:0:0:0:0:ffff:192.1.56.10]", "[0:0:0:0:0:ffff:c0a8:101]", "www.foobar.com"} 95 for _, ip := range ipList { 96 assert.DeepEqual(t, ip+":443", AddMissingPort(ip, true)) 97 assert.DeepEqual(t, ip+":80", AddMissingPort(ip, false)) 98 customizedPort := ":8080" 99 assert.DeepEqual(t, ip+customizedPort, AddMissingPort(ip+customizedPort, true)) 100 assert.DeepEqual(t, ip+customizedPort, AddMissingPort(ip+customizedPort, false)) 101 } 102 }