2016-06-07 08:08:50 +00:00
|
|
|
// Copyright 2013 Julien Schmidt. All rights reserved.
|
|
|
|
// Based on the path package, Copyright 2009 The Go Authors.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
|
|
|
// in the LICENSE file.
|
|
|
|
|
|
|
|
package httprouter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cleanTests = []struct {
|
|
|
|
path, result string
|
|
|
|
}{
|
|
|
|
// Already clean
|
|
|
|
{"/", "/"},
|
|
|
|
{"/abc", "/abc"},
|
|
|
|
{"/a/b/c", "/a/b/c"},
|
|
|
|
{"/abc/", "/abc/"},
|
|
|
|
{"/a/b/c/", "/a/b/c/"},
|
|
|
|
|
|
|
|
// missing root
|
|
|
|
{"", "/"},
|
2018-10-23 03:00:09 +00:00
|
|
|
{"a/", "/a/"},
|
2016-06-07 08:08:50 +00:00
|
|
|
{"abc", "/abc"},
|
|
|
|
{"abc/def", "/abc/def"},
|
|
|
|
{"a/b/c", "/a/b/c"},
|
|
|
|
|
|
|
|
// Remove doubled slash
|
|
|
|
{"//", "/"},
|
|
|
|
{"/abc//", "/abc/"},
|
|
|
|
{"/abc/def//", "/abc/def/"},
|
|
|
|
{"/a/b/c//", "/a/b/c/"},
|
|
|
|
{"/abc//def//ghi", "/abc/def/ghi"},
|
|
|
|
{"//abc", "/abc"},
|
|
|
|
{"///abc", "/abc"},
|
|
|
|
{"//abc//", "/abc/"},
|
|
|
|
|
|
|
|
// Remove . elements
|
|
|
|
{".", "/"},
|
|
|
|
{"./", "/"},
|
|
|
|
{"/abc/./def", "/abc/def"},
|
|
|
|
{"/./abc/def", "/abc/def"},
|
|
|
|
{"/abc/.", "/abc/"},
|
|
|
|
|
|
|
|
// Remove .. elements
|
|
|
|
{"..", "/"},
|
|
|
|
{"../", "/"},
|
|
|
|
{"../../", "/"},
|
|
|
|
{"../..", "/"},
|
|
|
|
{"../../abc", "/abc"},
|
|
|
|
{"/abc/def/ghi/../jkl", "/abc/def/jkl"},
|
|
|
|
{"/abc/def/../ghi/../jkl", "/abc/jkl"},
|
|
|
|
{"/abc/def/..", "/abc"},
|
|
|
|
{"/abc/def/../..", "/"},
|
|
|
|
{"/abc/def/../../..", "/"},
|
|
|
|
{"/abc/def/../../..", "/"},
|
|
|
|
{"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
|
|
|
|
|
|
|
|
// Combinations
|
|
|
|
{"abc/./../def", "/def"},
|
|
|
|
{"abc//./../def", "/def"},
|
|
|
|
{"abc/../../././../def", "/def"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathClean(t *testing.T) {
|
|
|
|
for _, test := range cleanTests {
|
|
|
|
if s := CleanPath(test.path); s != test.result {
|
|
|
|
t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result)
|
|
|
|
}
|
|
|
|
if s := CleanPath(test.result); s != test.result {
|
|
|
|
t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathCleanMallocs(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping malloc count in short mode")
|
|
|
|
}
|
|
|
|
if runtime.GOMAXPROCS(0) > 1 {
|
|
|
|
t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cleanTests {
|
|
|
|
allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) })
|
|
|
|
if allocs > 0 {
|
|
|
|
t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|