golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/http/httpguts/httplex_test.go (about) 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 5 package httpguts 6 7 import ( 8 "testing" 9 ) 10 11 func isChar(c rune) bool { return c <= 127 } 12 13 func isCtl(c rune) bool { return c <= 31 || c == 127 } 14 15 func isSeparator(c rune) bool { 16 switch c { 17 case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t': 18 return true 19 } 20 return false 21 } 22 23 func TestIsTokenRune(t *testing.T) { 24 for i := 0; i <= 130; i++ { 25 r := rune(i) 26 expected := isChar(r) && !isCtl(r) && !isSeparator(r) 27 if IsTokenRune(r) != expected { 28 t.Errorf("isToken(0x%x) = %v", r, !expected) 29 } 30 } 31 } 32 33 func BenchmarkIsTokenRune(b *testing.B) { 34 for i := 0; i < b.N; i++ { 35 var r rune 36 for ; r < 1024; r++ { 37 IsTokenRune(r) 38 } 39 } 40 } 41 42 func TestHeaderValuesContainsToken(t *testing.T) { 43 tests := []struct { 44 vals []string 45 token string 46 want bool 47 }{ 48 { 49 vals: []string{"foo"}, 50 token: "foo", 51 want: true, 52 }, 53 { 54 vals: []string{"bar", "foo"}, 55 token: "foo", 56 want: true, 57 }, 58 { 59 vals: []string{"foo"}, 60 token: "FOO", 61 want: true, 62 }, 63 { 64 vals: []string{"foo"}, 65 token: "bar", 66 want: false, 67 }, 68 { 69 vals: []string{" foo "}, 70 token: "FOO", 71 want: true, 72 }, 73 { 74 vals: []string{"foo,bar"}, 75 token: "FOO", 76 want: true, 77 }, 78 { 79 vals: []string{"bar,foo,bar"}, 80 token: "FOO", 81 want: true, 82 }, 83 { 84 vals: []string{"bar , foo"}, 85 token: "FOO", 86 want: true, 87 }, 88 { 89 vals: []string{"foo ,bar "}, 90 token: "FOO", 91 want: true, 92 }, 93 { 94 vals: []string{"bar, foo ,bar"}, 95 token: "FOO", 96 want: true, 97 }, 98 { 99 vals: []string{"bar , foo"}, 100 token: "FOO", 101 want: true, 102 }, 103 } 104 for _, tt := range tests { 105 got := HeaderValuesContainsToken(tt.vals, tt.token) 106 if got != tt.want { 107 t.Errorf("headerValuesContainsToken(%q, %q) = %v; want %v", tt.vals, tt.token, got, tt.want) 108 } 109 } 110 } 111 112 func TestValidHeaderFieldName(t *testing.T) { 113 tests := []struct { 114 in string 115 want bool 116 }{ 117 {"", false}, 118 {"Accept Charset", false}, 119 {"Accept-Charset", true}, 120 {"AccepT-EncodinG", true}, 121 {"CONNECTION", true}, 122 {"résumé", false}, 123 } 124 for _, tt := range tests { 125 got := ValidHeaderFieldName(tt.in) 126 if tt.want != got { 127 t.Errorf("ValidHeaderFieldName(%q) = %t; want %t", tt.in, got, tt.want) 128 } 129 } 130 } 131 132 func BenchmarkValidHeaderFieldName(b *testing.B) { 133 names := []string{ 134 "", 135 "Accept Charset", 136 "Accept-Charset", 137 "AccepT-EncodinG", 138 "CONNECTION", 139 "résumé", 140 } 141 b.ReportAllocs() 142 b.ResetTimer() 143 for i := 0; i < b.N; i++ { 144 for _, name := range names { 145 ValidHeaderFieldName(name) 146 } 147 } 148 } 149 150 func TestPunycodeHostPort(t *testing.T) { 151 tests := []struct { 152 in, want string 153 }{ 154 {"www.google.com", "www.google.com"}, 155 {"гофер.рф", "xn--c1ae0ajs.xn--p1ai"}, 156 {"bücher.de", "xn--bcher-kva.de"}, 157 {"bücher.de:8080", "xn--bcher-kva.de:8080"}, 158 {"[1::6]:8080", "[1::6]:8080"}, 159 } 160 for _, tt := range tests { 161 got, err := PunycodeHostPort(tt.in) 162 if tt.want != got || err != nil { 163 t.Errorf("PunycodeHostPort(%q) = %q, %v, want %q, nil", tt.in, got, err, tt.want) 164 } 165 } 166 }