-
Notifications
You must be signed in to change notification settings - Fork 222
/
bench_test.go
124 lines (105 loc) · 2.86 KB
/
bench_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cors
import (
"net/http"
"strings"
"testing"
)
type FakeResponse struct {
header http.Header
}
func (r FakeResponse) Header() http.Header {
return r.header
}
func (r FakeResponse) WriteHeader(n int) {
}
func (r FakeResponse) Write(b []byte) (n int, err error) {
return len(b), nil
}
const (
headerOrigin = "Origin"
headerACRM = "Access-Control-Request-Method"
headerACRH = "Access-Control-Request-Headers"
dummyEndpoint = "http://example.com/foo"
dummyOrigin = "https://somedomain.com"
)
func BenchmarkWithout(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodGet, dummyEndpoint, nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testHandler.ServeHTTP(resps[i], req)
}
}
func BenchmarkDefault(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodGet, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(resps[i], req)
}
}
func BenchmarkAllowedOrigin(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodGet, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
c := New(Options{
AllowedOrigins: []string{dummyOrigin},
})
handler := c.Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(resps[i], req)
}
}
func BenchmarkPreflight(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodOptions, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
req.Header.Add(headerACRM, http.MethodGet)
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(resps[i], req)
}
}
func BenchmarkPreflightHeader(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodOptions, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
req.Header.Add(headerACRM, http.MethodGet)
req.Header.Add(headerACRH, "accept")
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(resps[i], req)
}
}
func BenchmarkPreflightAdversarialACRH(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodOptions, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
req.Header.Add(headerACRM, http.MethodGet)
req.Header.Add(headerACRH, strings.Repeat(",", 1024))
handler := Default().Handler(testHandler)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(resps[i], req)
}
}
func makeFakeResponses(n int) []*FakeResponse {
resps := make([]*FakeResponse, n)
for i := 0; i < n; i++ {
resps[i] = &FakeResponse{http.Header{
"Content-Type": []string{"text/plain"},
}}
}
return resps
}