-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_test.go
More file actions
78 lines (71 loc) · 1.47 KB
/
compression_test.go
File metadata and controls
78 lines (71 loc) · 1.47 KB
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
package pmtilr
import (
"bytes"
"compress/gzip"
"io"
"testing"
)
func TestDecompress(t *testing.T) {
tests := []struct {
name string
compression Compression
input string
expectError bool
}{
{
name: "No compression",
compression: CompressionNone,
input: "test-data",
expectError: false,
},
{
name: "Unknown compression",
compression: CompressionUnknown,
input: "test-data",
expectError: false,
},
{
name: "GZIP compression",
compression: CompressionGZIP,
input: "test-data",
expectError: false,
},
{
name: "Unsupported compression",
compression: CompressionBrotli,
input: "test-data",
expectError: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
var r io.Reader
if tc.compression == CompressionGZIP {
gw := gzip.NewWriter(&buf)
_, _ = gw.Write([]byte(tc.input))
_ = gw.Close()
r = &buf
} else {
r = bytes.NewReader([]byte(tc.input))
}
dr, err := Decompress(io.NopCloser(r), tc.compression)
if tc.expectError {
if err == nil {
t.Errorf("expected error, got none")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
out, err := io.ReadAll(dr)
if err != nil {
t.Fatalf("reading decompressed data: %v", err)
}
if string(out) != tc.input {
t.Errorf("got %q, want %q", string(out), tc.input)
}
})
}
}