path_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "runtime"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. var cleanTests = []struct {
  11. path, result string
  12. }{
  13. // Already clean
  14. {"/", "/"},
  15. {"/abc", "/abc"},
  16. {"/a/b/c", "/a/b/c"},
  17. {"/abc/", "/abc/"},
  18. {"/a/b/c/", "/a/b/c/"},
  19. // missing root
  20. {"", "/"},
  21. {"abc", "/abc"},
  22. {"abc/def", "/abc/def"},
  23. {"a/b/c", "/a/b/c"},
  24. // Remove doubled slash
  25. {"//", "/"},
  26. {"/abc//", "/abc/"},
  27. {"/abc/def//", "/abc/def/"},
  28. {"/a/b/c//", "/a/b/c/"},
  29. {"/abc//def//ghi", "/abc/def/ghi"},
  30. {"//abc", "/abc"},
  31. {"///abc", "/abc"},
  32. {"//abc//", "/abc/"},
  33. // Remove . elements
  34. {".", "/"},
  35. {"./", "/"},
  36. {"/abc/./def", "/abc/def"},
  37. {"/./abc/def", "/abc/def"},
  38. {"/abc/.", "/abc/"},
  39. // Remove .. elements
  40. {"..", "/"},
  41. {"../", "/"},
  42. {"../../", "/"},
  43. {"../..", "/"},
  44. {"../../abc", "/abc"},
  45. {"/abc/def/ghi/../jkl", "/abc/def/jkl"},
  46. {"/abc/def/../ghi/../jkl", "/abc/jkl"},
  47. {"/abc/def/..", "/abc"},
  48. {"/abc/def/../..", "/"},
  49. {"/abc/def/../../..", "/"},
  50. {"/abc/def/../../..", "/"},
  51. {"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
  52. // Combinations
  53. {"abc/./../def", "/def"},
  54. {"abc//./../def", "/def"},
  55. {"abc/../../././../def", "/def"},
  56. }
  57. func TestPathClean(t *testing.T) {
  58. for _, test := range cleanTests {
  59. assert.Equal(t, cleanPath(test.path), test.result)
  60. assert.Equal(t, cleanPath(test.result), test.result)
  61. }
  62. }
  63. func TestPathCleanMallocs(t *testing.T) {
  64. if testing.Short() {
  65. t.Skip("skipping malloc count in short mode")
  66. }
  67. if runtime.GOMAXPROCS(0) > 1 {
  68. t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
  69. return
  70. }
  71. for _, test := range cleanTests {
  72. allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })
  73. assert.EqualValues(t, allocs, 0)
  74. }
  75. }