You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
2.3 KiB

  1. // Copyright 2009 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 httplex
  5. import (
  6. "testing"
  7. )
  8. func isChar(c rune) bool { return c <= 127 }
  9. func isCtl(c rune) bool { return c <= 31 || c == 127 }
  10. func isSeparator(c rune) bool {
  11. switch c {
  12. case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
  13. return true
  14. }
  15. return false
  16. }
  17. func TestIsToken(t *testing.T) {
  18. for i := 0; i <= 130; i++ {
  19. r := rune(i)
  20. expected := isChar(r) && !isCtl(r) && !isSeparator(r)
  21. if IsTokenRune(r) != expected {
  22. t.Errorf("isToken(0x%x) = %v", r, !expected)
  23. }
  24. }
  25. }
  26. func TestHeaderValuesContainsToken(t *testing.T) {
  27. tests := []struct {
  28. vals []string
  29. token string
  30. want bool
  31. }{
  32. {
  33. vals: []string{"foo"},
  34. token: "foo",
  35. want: true,
  36. },
  37. {
  38. vals: []string{"bar", "foo"},
  39. token: "foo",
  40. want: true,
  41. },
  42. {
  43. vals: []string{"foo"},
  44. token: "FOO",
  45. want: true,
  46. },
  47. {
  48. vals: []string{"foo"},
  49. token: "bar",
  50. want: false,
  51. },
  52. {
  53. vals: []string{" foo "},
  54. token: "FOO",
  55. want: true,
  56. },
  57. {
  58. vals: []string{"foo,bar"},
  59. token: "FOO",
  60. want: true,
  61. },
  62. {
  63. vals: []string{"bar,foo,bar"},
  64. token: "FOO",
  65. want: true,
  66. },
  67. {
  68. vals: []string{"bar , foo"},
  69. token: "FOO",
  70. want: true,
  71. },
  72. {
  73. vals: []string{"foo ,bar "},
  74. token: "FOO",
  75. want: true,
  76. },
  77. {
  78. vals: []string{"bar, foo ,bar"},
  79. token: "FOO",
  80. want: true,
  81. },
  82. {
  83. vals: []string{"bar , foo"},
  84. token: "FOO",
  85. want: true,
  86. },
  87. }
  88. for _, tt := range tests {
  89. got := HeaderValuesContainsToken(tt.vals, tt.token)
  90. if got != tt.want {
  91. t.Errorf("headerValuesContainsToken(%q, %q) = %v; want %v", tt.vals, tt.token, got, tt.want)
  92. }
  93. }
  94. }
  95. func TestPunycodeHostPort(t *testing.T) {
  96. tests := []struct {
  97. in, want string
  98. }{
  99. {"www.google.com", "www.google.com"},
  100. {"гофер.рф", "xn--c1ae0ajs.xn--p1ai"},
  101. {"bücher.de", "xn--bcher-kva.de"},
  102. {"bücher.de:8080", "xn--bcher-kva.de:8080"},
  103. {"[1::6]:8080", "[1::6]:8080"},
  104. }
  105. for _, tt := range tests {
  106. got, err := PunycodeHostPort(tt.in)
  107. if tt.want != got || err != nil {
  108. t.Errorf("PunycodeHostPort(%q) = %q, %v, want %q, nil", tt.in, got, err, tt.want)
  109. }
  110. }
  111. }