path_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 found
  4. // at https://github.com/julienschmidt/httprouter/blob/master/LICENSE
  5. package gin
  6. import (
  7. "runtime"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. var cleanTests = []struct {
  12. path, result string
  13. }{
  14. // Already clean
  15. {"/", "/"},
  16. {"/abc", "/abc"},
  17. {"/a/b/c", "/a/b/c"},
  18. {"/abc/", "/abc/"},
  19. {"/a/b/c/", "/a/b/c/"},
  20. // missing root
  21. {"", "/"},
  22. {"abc", "/abc"},
  23. {"abc/def", "/abc/def"},
  24. {"a/b/c", "/a/b/c"},
  25. // Remove doubled slash
  26. {"//", "/"},
  27. {"/abc//", "/abc/"},
  28. {"/abc/def//", "/abc/def/"},
  29. {"/a/b/c//", "/a/b/c/"},
  30. {"/abc//def//ghi", "/abc/def/ghi"},
  31. {"//abc", "/abc"},
  32. {"///abc", "/abc"},
  33. {"//abc//", "/abc/"},
  34. // Remove . elements
  35. {".", "/"},
  36. {"./", "/"},
  37. {"/abc/./def", "/abc/def"},
  38. {"/./abc/def", "/abc/def"},
  39. {"/abc/.", "/abc/"},
  40. // Remove .. elements
  41. {"..", "/"},
  42. {"../", "/"},
  43. {"../../", "/"},
  44. {"../..", "/"},
  45. {"../../abc", "/abc"},
  46. {"/abc/def/ghi/../jkl", "/abc/def/jkl"},
  47. {"/abc/def/../ghi/../jkl", "/abc/jkl"},
  48. {"/abc/def/..", "/abc"},
  49. {"/abc/def/../..", "/"},
  50. {"/abc/def/../../..", "/"},
  51. {"/abc/def/../../..", "/"},
  52. {"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
  53. // Combinations
  54. {"abc/./../def", "/def"},
  55. {"abc//./../def", "/def"},
  56. {"abc/../../././../def", "/def"},
  57. }
  58. func TestPathClean(t *testing.T) {
  59. for _, test := range cleanTests {
  60. assert.Equal(t, cleanPath(test.path), test.result)
  61. assert.Equal(t, cleanPath(test.result), test.result)
  62. }
  63. }
  64. func TestPathCleanMallocs(t *testing.T) {
  65. if testing.Short() {
  66. t.Skip("skipping malloc count in short mode")
  67. }
  68. if runtime.GOMAXPROCS(0) > 1 {
  69. t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
  70. return
  71. }
  72. for _, test := range cleanTests {
  73. allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })
  74. assert.EqualValues(t, allocs, 0)
  75. }
  76. }