Skip to content
Myr

path_test.go

This file

Path

path_test.go

192 lines · 3.6 KiB · Go

Last changed by

perf(path): replace regex with custom functions in redirectTrailingSlash (#4414)Namebefore adoption440eb14

History of this file

Take it

1// Copyright 2013 Julien Schmidt. All rights reserved.2// Based on the path package, Copyright 2009 The Go Authors.3// Use of this source code is governed by a BSD-style license that can be found4// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE56package gin78import (9	"runtime"10	"strings"11	"testing"1213	"github.com/stretchr/testify/assert"14)1516type cleanPathTest struct {17	path, result string18}1920var cleanTests = []cleanPathTest{21	// Already clean22	{"/", "/"},23	{"/abc", "/abc"},24	{"/a/b/c", "/a/b/c"},25	{"/abc/", "/abc/"},26	{"/a/b/c/", "/a/b/c/"},2728	// missing root29	{"", "/"},30	{"a/", "/a/"},31	{"abc", "/abc"},32	{"abc/def", "/abc/def"},33	{"a/b/c", "/a/b/c"},3435	// Remove doubled slash36	{"//", "/"},37	{"/abc//", "/abc/"},38	{"/abc/def//", "/abc/def/"},39	{"/a/b/c//", "/a/b/c/"},40	{"/abc//def//ghi", "/abc/def/ghi"},41	{"//abc", "/abc"},42	{"///abc", "/abc"},43	{"//abc//", "/abc/"},4445	// Remove . elements46	{".", "/"},47	{"./", "/"},48	{"/abc/./def", "/abc/def"},49	{"/./abc/def", "/abc/def"},50	{"/abc/.", "/abc/"},5152	// Remove .. elements53	{"..", "/"},54	{"../", "/"},55	{"../../", "/"},56	{"../..", "/"},57	{"../../abc", "/abc"},58	{"/abc/def/ghi/../jkl", "/abc/def/jkl"},59	{"/abc/def/../ghi/../jkl", "/abc/jkl"},60	{"/abc/def/..", "/abc"},61	{"/abc/def/../..", "/"},62	{"/abc/def/../../..", "/"},63	{"/abc/def/../../..", "/"},64	{"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},6566	// Combinations67	{"abc/./../def", "/def"},68	{"abc//./../def", "/def"},69	{"abc/../../././../def", "/def"},70}7172func TestPathClean(t *testing.T) {73	for _, test := range cleanTests {74		assert.Equal(t, test.result, cleanPath(test.path))75		assert.Equal(t, test.result, cleanPath(test.result))76	}77}7879func TestPathCleanMallocs(t *testing.T) {80	if testing.Short() {81		t.Skip("skipping malloc count in short mode")82	}8384	if runtime.GOMAXPROCS(0) > 1 {85		t.Skip("skipping malloc count; GOMAXPROCS>1")86	}8788	for _, test := range cleanTests {89		allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })90		assert.InDelta(t, 0, allocs, 0.01)91	}92}9394func BenchmarkPathClean(b *testing.B) {95	b.ReportAllocs()9697	for b.Loop() {98		for _, test := range cleanTests {99			cleanPath(test.path)100		}101	}102}103104func genLongPaths() (testPaths []cleanPathTest) {105	for i := 1; i <= 1234; i++ {106		ss := strings.Repeat("a", i)107108		correctPath := "/" + ss109		testPaths = append(testPaths, cleanPathTest{110			path:   correctPath,111			result: correctPath,112		}, cleanPathTest{113			path:   ss,114			result: correctPath,115		}, cleanPathTest{116			path:   "//" + ss,117			result: correctPath,118		}, cleanPathTest{119			path:   "/" + ss + "/b/..",120			result: correctPath,121		})122	}123	return124}125126func TestPathCleanLong(t *testing.T) {127	cleanTests := genLongPaths()128129	for _, test := range cleanTests {130		assert.Equal(t, test.result, cleanPath(test.path))131		assert.Equal(t, test.result, cleanPath(test.result))132	}133}134135func BenchmarkPathCleanLong(b *testing.B) {136	cleanTests := genLongPaths()137138	b.ReportAllocs()139140	for b.Loop() {141		for _, test := range cleanTests {142			cleanPath(test.path)143		}144	}145}146147func TestRemoveRepeatedChar(t *testing.T) {148	testCases := []struct {149		name string150		str  string151		char byte152		want string153	}{154		{155			name: "empty",156			str:  "",157			char: 'a',158			want: "",159		},160		{161			name: "noSlash",162			str:  "abc",163			char: ',',164			want: "abc",165		},166		{167			name: "withSlash",168			str:  "/a/b/c/",169			char: '/',170			want: "/a/b/c/",171		},172		{173			name: "withRepeatedSlashes",174			str:  "/a//b///c////",175			char: '/',176			want: "/a/b/c/",177		},178		{179			name: "threeSlashes",180			str:  "///",181			char: '/',182			want: "/",183		},184	}185186	for _, tc := range testCases {187		t.Run(tc.name, func(t *testing.T) {188			res := removeRepeatedChar(tc.str, tc.char)189			assert.Equal(t, tc.want, res)190		})191	}192}