url_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package template
  5. import (
  6. "testing"
  7. )
  8. func TestURLNormalizer(t *testing.T) {
  9. tests := []struct {
  10. url, want string
  11. }{
  12. {"", ""},
  13. {
  14. "http://example.com:80/foo/bar?q=foo%20&bar=x+y#frag",
  15. "http://example.com:80/foo/bar?q=foo%20&bar=x+y#frag",
  16. },
  17. {" ", "%20"},
  18. {"%7c", "%7c"},
  19. {"%7C", "%7C"},
  20. {"%2", "%252"},
  21. {"%", "%25"},
  22. {"%z", "%25z"},
  23. {"/foo|bar/%5c\u1234", "/foo%7cbar/%5c%e1%88%b4"},
  24. }
  25. for _, test := range tests {
  26. if got := urlNormalizer(test.url); test.want != got {
  27. t.Errorf("%q: want\n\t%q\nbut got\n\t%q", test.url, test.want, got)
  28. }
  29. if test.want != urlNormalizer(test.want) {
  30. t.Errorf("not idempotent: %q", test.want)
  31. }
  32. }
  33. }
  34. func TestURLFilters(t *testing.T) {
  35. input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
  36. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
  37. ` !"#$%&'()*+,-./` +
  38. `0123456789:;<=>?` +
  39. `@ABCDEFGHIJKLMNO` +
  40. `PQRSTUVWXYZ[\]^_` +
  41. "`abcdefghijklmno" +
  42. "pqrstuvwxyz{|}~\x7f" +
  43. "\u00A0\u0100\u2028\u2029\ufeff\U0001D11E")
  44. tests := []struct {
  45. name string
  46. escaper func(...interface{}) string
  47. escaped string
  48. }{
  49. {
  50. "urlEscaper",
  51. urlEscaper,
  52. "%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f" +
  53. "%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f" +
  54. "%20%21%22%23%24%25%26%27%28%29%2a%2b%2c-.%2f" +
  55. "0123456789%3a%3b%3c%3d%3e%3f" +
  56. "%40ABCDEFGHIJKLMNO" +
  57. "PQRSTUVWXYZ%5b%5c%5d%5e_" +
  58. "%60abcdefghijklmno" +
  59. "pqrstuvwxyz%7b%7c%7d~%7f" +
  60. "%c2%a0%c4%80%e2%80%a8%e2%80%a9%ef%bb%bf%f0%9d%84%9e",
  61. },
  62. {
  63. "urlNormalizer",
  64. urlNormalizer,
  65. "%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f" +
  66. "%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f" +
  67. "%20!%22#$%25&%27%28%29*+,-./" +
  68. "0123456789:;%3c=%3e?" +
  69. "@ABCDEFGHIJKLMNO" +
  70. "PQRSTUVWXYZ[%5c]%5e_" +
  71. "%60abcdefghijklmno" +
  72. "pqrstuvwxyz%7b%7c%7d~%7f" +
  73. "%c2%a0%c4%80%e2%80%a8%e2%80%a9%ef%bb%bf%f0%9d%84%9e",
  74. },
  75. }
  76. for _, test := range tests {
  77. if s := test.escaper(input); s != test.escaped {
  78. t.Errorf("%s: want\n\t%q\ngot\n\t%q", test.name, test.escaped, s)
  79. continue
  80. }
  81. }
  82. }
  83. func BenchmarkURLEscaper(b *testing.B) {
  84. for i := 0; i < b.N; i++ {
  85. urlEscaper("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
  86. }
  87. }
  88. func BenchmarkURLEscaperNoSpecials(b *testing.B) {
  89. for i := 0; i < b.N; i++ {
  90. urlEscaper("TheQuickBrownFoxJumpsOverTheLazyDog.")
  91. }
  92. }
  93. func BenchmarkURLNormalizer(b *testing.B) {
  94. for i := 0; i < b.N; i++ {
  95. urlNormalizer("The quick brown fox jumps over the lazy dog.\n")
  96. }
  97. }
  98. func BenchmarkURLNormalizerNoSpecials(b *testing.B) {
  99. for i := 0; i < b.N; i++ {
  100. urlNormalizer("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
  101. }
  102. }