path_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // in the LICENSE file.
  5. package httprouter
  6. import (
  7. "runtime"
  8. "testing"
  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. {"a/", "/a/"},
  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. if s := CleanPath(test.path); s != test.result {
  61. t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result)
  62. }
  63. if s := CleanPath(test.result); s != test.result {
  64. t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result)
  65. }
  66. }
  67. }
  68. func TestPathCleanMallocs(t *testing.T) {
  69. if testing.Short() {
  70. t.Skip("skipping malloc count in short mode")
  71. }
  72. if runtime.GOMAXPROCS(0) > 1 {
  73. t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
  74. return
  75. }
  76. for _, test := range cleanTests {
  77. allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) })
  78. if allocs > 0 {
  79. t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs)
  80. }
  81. }
  82. }