path_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {"a/", "/a/"},
  23. {"abc", "/abc"},
  24. {"abc/def", "/abc/def"},
  25. {"a/b/c", "/a/b/c"},
  26. // Remove doubled slash
  27. {"//", "/"},
  28. {"/abc//", "/abc/"},
  29. {"/abc/def//", "/abc/def/"},
  30. {"/a/b/c//", "/a/b/c/"},
  31. {"/abc//def//ghi", "/abc/def/ghi"},
  32. {"//abc", "/abc"},
  33. {"///abc", "/abc"},
  34. {"//abc//", "/abc/"},
  35. // Remove . elements
  36. {".", "/"},
  37. {"./", "/"},
  38. {"/abc/./def", "/abc/def"},
  39. {"/./abc/def", "/abc/def"},
  40. {"/abc/.", "/abc/"},
  41. // Remove .. elements
  42. {"..", "/"},
  43. {"../", "/"},
  44. {"../../", "/"},
  45. {"../..", "/"},
  46. {"../../abc", "/abc"},
  47. {"/abc/def/ghi/../jkl", "/abc/def/jkl"},
  48. {"/abc/def/../ghi/../jkl", "/abc/jkl"},
  49. {"/abc/def/..", "/abc"},
  50. {"/abc/def/../..", "/"},
  51. {"/abc/def/../../..", "/"},
  52. {"/abc/def/../../..", "/"},
  53. {"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
  54. // Combinations
  55. {"abc/./../def", "/def"},
  56. {"abc//./../def", "/def"},
  57. {"abc/../../././../def", "/def"},
  58. }
  59. func TestPathClean(t *testing.T) {
  60. for _, test := range cleanTests {
  61. assert.Equal(t, test.result, cleanPath(test.path))
  62. assert.Equal(t, test.result, cleanPath(test.result))
  63. }
  64. }
  65. func TestPathCleanMallocs(t *testing.T) {
  66. if testing.Short() {
  67. t.Skip("skipping malloc count in short mode")
  68. }
  69. if runtime.GOMAXPROCS(0) > 1 {
  70. t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
  71. return
  72. }
  73. for _, test := range cleanTests {
  74. allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })
  75. assert.EqualValues(t, allocs, 0)
  76. }
  77. }