github.com/ezoic/ws@v1.0.4-0.20220713205711-5c1d69e074c5/http_test.go (about) 1 package ws 2 3 import ( 4 "bufio" 5 "io/ioutil" 6 "net/url" 7 "testing" 8 9 "github.com/ezoic/httphead" 10 ) 11 12 type httpVersionCase struct { 13 in []byte 14 major int 15 minor int 16 ok bool 17 } 18 19 var httpVersionCases = []httpVersionCase{ 20 {[]byte("HTTP/1.1"), 1, 1, true}, 21 {[]byte("HTTP/1.0"), 1, 0, true}, 22 {[]byte("HTTP/1.2"), 1, 2, true}, 23 {[]byte("HTTP/42.1092"), 42, 1092, true}, 24 } 25 26 func TestParseHttpVersion(t *testing.T) { 27 for _, c := range httpVersionCases { 28 t.Run(string(c.in), func(t *testing.T) { 29 major, minor, ok := httpParseVersion(c.in) 30 if major != c.major || minor != c.minor || ok != c.ok { 31 t.Errorf( 32 "parseHttpVersion([]byte(%q)) = %v, %v, %v; want %v, %v, %v", 33 string(c.in), major, minor, ok, c.major, c.minor, c.ok, 34 ) 35 } 36 }) 37 } 38 } 39 40 func BenchmarkParseHttpVersion(b *testing.B) { 41 for _, c := range httpVersionCases { 42 b.Run(string(c.in), func(b *testing.B) { 43 for i := 0; i < b.N; i++ { 44 _, _, _ = httpParseVersion(c.in) 45 } 46 }) 47 } 48 } 49 50 func BenchmarkHttpWriteUpgradeRequest(b *testing.B) { 51 for _, test := range []struct { 52 url *url.URL 53 protocols []string 54 extensions []httphead.Option 55 headers HandshakeHeaderFunc 56 }{ 57 { 58 url: makeURL("ws://example.org"), 59 }, 60 } { 61 bw := bufio.NewWriter(ioutil.Discard) 62 nonce := make([]byte, nonceSize) 63 initNonce(nonce) 64 65 var headers HandshakeHeader 66 if test.headers != nil { 67 headers = test.headers 68 } 69 70 b.ResetTimer() 71 b.Run("", func(b *testing.B) { 72 for i := 0; i < b.N; i++ { 73 httpWriteUpgradeRequest(bw, 74 test.url, 75 nonce, 76 test.protocols, 77 test.extensions, 78 headers, 79 ) 80 } 81 }) 82 } 83 } 84 85 func makeURL(s string) *url.URL { 86 ret, err := url.Parse(s) 87 if err != nil { 88 panic(err) 89 } 90 return ret 91 }