github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/test/fasthttprouter_path_test.go (about) 1 // Copyright 2013 Julien Schmidt. All rights reserved. 2 // Copyright (c) 2015-2016, 招牌疯子 3 // Copyright (c) 2017, Kirill Danshin 4 // Use of this source code is governed by a BSD-style license that can be found 5 // in the 3rd-Party License/fasthttprouter file. 6 package test 7 8 import ( 9 "runtime" 10 "testing" 11 12 "github.com/gramework/gramework" 13 ) 14 15 var cleanTests = []struct { 16 path, result string 17 }{ 18 // Already clean 19 {"/", "/"}, 20 {"/abc", "/abc"}, 21 {"/a/b/c", "/a/b/c"}, 22 {"/abc/", "/abc/"}, 23 {"/a/b/c/", "/a/b/c/"}, 24 25 // missing root 26 {"", "/"}, 27 {"abc", "/abc"}, 28 {"abc/def", "/abc/def"}, 29 {"a/b/c", "/a/b/c"}, 30 31 // Remove doubled slash 32 {"//", "/"}, 33 {"/abc//", "/abc/"}, 34 {"/abc/def//", "/abc/def/"}, 35 {"/a/b/c//", "/a/b/c/"}, 36 {"/abc//def//ghi", "/abc/def/ghi"}, 37 {"//abc", "/abc"}, 38 {"///abc", "/abc"}, 39 {"//abc//", "/abc/"}, 40 41 // Remove . elements 42 {".", "/"}, 43 {"./", "/"}, 44 {"/abc/./def", "/abc/def"}, 45 {"/./abc/def", "/abc/def"}, 46 {"/abc/.", "/abc/"}, 47 48 // Remove .. elements 49 {"..", "/"}, 50 {"../", "/"}, 51 {"../../", "/"}, 52 {"../..", "/"}, 53 {"../../abc", "/abc"}, 54 {"/abc/def/ghi/../jkl", "/abc/def/jkl"}, 55 {"/abc/def/../ghi/../jkl", "/abc/jkl"}, 56 {"/abc/def/..", "/abc"}, 57 {"/abc/def/../..", "/"}, 58 {"/abc/def/../../..", "/"}, 59 {"/abc/def/../../..", "/"}, 60 {"/abc/def/../../../ghi/jkl/../../../mno", "/mno"}, 61 62 // Combinations 63 {"abc/./../def", "/def"}, 64 {"abc//./../def", "/def"}, 65 {"abc/../../././../def", "/def"}, 66 } 67 68 func TestPathClean(t *testing.T) { 69 for _, test := range cleanTests { 70 if s := gramework.CleanPath(test.path); s != test.result { 71 t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result) 72 } 73 if s := gramework.CleanPath(test.result); s != test.result { 74 t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result) 75 } 76 } 77 } 78 79 func TestPathCleanMallocs(t *testing.T) { 80 if testing.Short() { 81 t.Skip("skipping malloc count in short mode") 82 } 83 if runtime.GOMAXPROCS(0) > 1 { 84 t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1") 85 return 86 } 87 88 for _, test := range cleanTests { 89 allocs := testing.AllocsPerRun(100, func() { gramework.CleanPath(test.result) }) 90 if allocs > 0 { 91 t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs) 92 } 93 } 94 }