-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
101 lines (89 loc) · 2.09 KB
/
utils_test.go
File metadata and controls
101 lines (89 loc) · 2.09 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package httpc
import (
"bytes"
"compress/gzip"
"testing"
)
func TestDecodeGzipBody(t *testing.T) {
tests := []struct {
name string
input string
expectError bool
}{
{
name: "Valid gzip data",
input: "Hello, World!",
expectError: false,
},
{
name: "Empty data",
input: "",
expectError: false,
},
{
name: "Large data",
input: string(bytes.Repeat([]byte("Test data "), 1000)),
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Compress the data
var buf bytes.Buffer
if len(tt.input) > 0 {
gzWriter := gzip.NewWriter(&buf)
_, err := gzWriter.Write([]byte(tt.input))
if err != nil {
t.Fatalf("Failed to compress data: %v", err)
}
gzWriter.Close()
}
// Decode the data
decoded, err := decodeGzipBody(buf.Bytes())
if tt.expectError && err == nil {
t.Error("Expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !tt.expectError && string(decoded) != tt.input {
t.Errorf("Decoded data mismatch. Got %q, want %q", string(decoded), tt.input)
}
})
}
}
func TestDecodeGzipBody_InvalidData(t *testing.T) {
// Test with invalid gzip data
invalidData := []byte("this is not gzip data")
decoded, err := decodeGzipBody(invalidData)
// Should return the original data on error
if err == nil {
t.Error("Expected error for invalid gzip data")
}
if !bytes.Equal(decoded, invalidData) {
t.Error("Should return original data when decompression fails")
}
}
func TestIsGzipEncoded(t *testing.T) {
tests := []struct {
encoding string
expected bool
}{
{"gzip", true},
{"GZIP", true},
{"Gzip", true},
{"GzIp", true},
{"deflate", false},
{"br", false},
{"", false},
{"gzip, deflate", false}, // Not exactly "gzip"
}
for _, tt := range tests {
t.Run(tt.encoding, func(t *testing.T) {
got := isGzipEncoded(tt.encoding)
if got != tt.expected {
t.Errorf("isGzipEncoded(%q) = %v, want %v", tt.encoding, got, tt.expected)
}
})
}
}