github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/net/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 TestIsToken(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 TestHeaderValuesContainsToken(t *testing.T) { 34 tests := []struct { 35 vals []string 36 token string 37 want bool 38 }{ 39 { 40 vals: []string{"foo"}, 41 token: "foo", 42 want: true, 43 }, 44 { 45 vals: []string{"bar", "foo"}, 46 token: "foo", 47 want: true, 48 }, 49 { 50 vals: []string{"foo"}, 51 token: "FOO", 52 want: true, 53 }, 54 { 55 vals: []string{"foo"}, 56 token: "bar", 57 want: false, 58 }, 59 { 60 vals: []string{" foo "}, 61 token: "FOO", 62 want: true, 63 }, 64 { 65 vals: []string{"foo,bar"}, 66 token: "FOO", 67 want: true, 68 }, 69 { 70 vals: []string{"bar,foo,bar"}, 71 token: "FOO", 72 want: true, 73 }, 74 { 75 vals: []string{"bar , foo"}, 76 token: "FOO", 77 want: true, 78 }, 79 { 80 vals: []string{"foo ,bar "}, 81 token: "FOO", 82 want: true, 83 }, 84 { 85 vals: []string{"bar, foo ,bar"}, 86 token: "FOO", 87 want: true, 88 }, 89 { 90 vals: []string{"bar , foo"}, 91 token: "FOO", 92 want: true, 93 }, 94 } 95 for _, tt := range tests { 96 got := HeaderValuesContainsToken(tt.vals, tt.token) 97 if got != tt.want { 98 t.Errorf("headerValuesContainsToken(%q, %q) = %v; want %v", tt.vals, tt.token, got, tt.want) 99 } 100 } 101 } 102 103 func TestPunycodeHostPort(t *testing.T) { 104 tests := []struct { 105 in, want string 106 }{ 107 {"www.google.com", "www.google.com"}, 108 {"гофер.рф", "xn--c1ae0ajs.xn--p1ai"}, 109 {"bücher.de", "xn--bcher-kva.de"}, 110 {"bücher.de:8080", "xn--bcher-kva.de:8080"}, 111 {"[1::6]:8080", "[1::6]:8080"}, 112 } 113 for _, tt := range tests { 114 got, err := PunycodeHostPort(tt.in) 115 if tt.want != got || err != nil { 116 t.Errorf("PunycodeHostPort(%q) = %q, %v, want %q, nil", tt.in, got, err, tt.want) 117 } 118 } 119 }