forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brotli_test.go
177 lines (154 loc) · 4.48 KB
/
brotli_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package fasthttp
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"testing"
)
func TestBrotliBytesSerial(t *testing.T) {
t.Parallel()
if err := testBrotliBytes(); err != nil {
t.Fatal(err)
}
}
func TestBrotliBytesConcurrent(t *testing.T) {
t.Parallel()
if err := testConcurrent(10, testBrotliBytes); err != nil {
t.Fatal(err)
}
}
func testBrotliBytes() error {
for _, s := range compressTestcases {
if err := testBrotliBytesSingleCase(s); err != nil {
return err
}
}
return nil
}
func testBrotliBytesSingleCase(s string) error {
prefix := []byte("foobar")
brotlipedS := AppendBrotliBytes(prefix, []byte(s))
if !bytes.Equal(brotlipedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when compressing %q: %q. Expecting %q", s, brotlipedS[:len(prefix)], prefix)
}
unbrotliedS, err := AppendUnbrotliBytes(prefix, brotlipedS[len(prefix):])
if err != nil {
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
}
if !bytes.Equal(unbrotliedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, unbrotliedS[:len(prefix)], prefix)
}
unbrotliedS = unbrotliedS[len(prefix):]
if string(unbrotliedS) != s {
return fmt.Errorf("unexpected uncompressed string %q. Expecting %q", unbrotliedS, s)
}
return nil
}
func TestBrotliCompressSerial(t *testing.T) {
t.Parallel()
if err := testBrotliCompress(); err != nil {
t.Fatal(err)
}
}
func TestBrotliCompressConcurrent(t *testing.T) {
t.Parallel()
if err := testConcurrent(10, testBrotliCompress); err != nil {
t.Fatal(err)
}
}
func testBrotliCompress() error {
for _, s := range compressTestcases {
if err := testBrotliCompressSingleCase(s); err != nil {
return err
}
}
return nil
}
func testBrotliCompressSingleCase(s string) error {
var buf bytes.Buffer
zw := acquireStacklessBrotliWriter(&buf, CompressDefaultCompression)
if _, err := zw.Write([]byte(s)); err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
releaseStacklessBrotliWriter(zw, CompressDefaultCompression)
zr, err := acquireBrotliReader(&buf)
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
body, err := ioutil.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
}
releaseBrotliReader(zr)
return nil
}
func TestCompressHandlerBrotliLevel(t *testing.T) {
t.Parallel()
expectedBody := string(createFixedBody(2e4))
h := CompressHandlerBrotliLevel(func(ctx *RequestCtx) {
ctx.Write([]byte(expectedBody)) //nolint:errcheck
}, CompressBrotliDefaultCompression, CompressDefaultCompression)
var ctx RequestCtx
var resp Response
// verify uncompressed response
h(&ctx)
s := ctx.Response.String()
br := bufio.NewReader(bytes.NewBufferString(s))
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
ce := resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "" {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
}
body := resp.Body()
if string(body) != expectedBody {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}
// verify gzip-compressed response
ctx.Request.Reset()
ctx.Response.Reset()
ctx.Request.Header.Set("Accept-Encoding", "gzip, deflate, sdhc")
h(&ctx)
s = ctx.Response.String()
br = bufio.NewReader(bytes.NewBufferString(s))
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
ce = resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "gzip" {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "gzip")
}
body, err := resp.BodyGunzip()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(body) != expectedBody {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}
// verify brotli-compressed response
ctx.Request.Reset()
ctx.Response.Reset()
ctx.Request.Header.Set("Accept-Encoding", "gzip, deflate, sdhc, br")
h(&ctx)
s = ctx.Response.String()
br = bufio.NewReader(bytes.NewBufferString(s))
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
ce = resp.Header.Peek(HeaderContentEncoding)
if string(ce) != "br" {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "br")
}
body, err = resp.BodyUnbrotli()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(body) != expectedBody {
t.Fatalf("unexpected body %q. Expecting %q", body, expectedBody)
}
}