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