-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentencoding_test.go
108 lines (99 loc) · 2.91 KB
/
contentencoding_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
package contentencoding_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
contentencoding "github.com/johejo/go-content-encoding"
)
func TestDecode_compress(t *testing.T) {
tests := []struct {
name string
encoding string
data string
}{
{"brotli", "br", "testdata/test.txt.br"},
{"gzip", "gzip", "testdata/test.txt.gz"},
{"zstd", "zstd", "testdata/test.txt.zst"},
{"gzip+zstd", "gzip, zstd", "testdata/test.txt.gz.zst"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux := http.NewServeMux()
mux.Handle("/", contentencoding.Decode()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
txt := strings.TrimSpace(string(b))
if txt != "test" {
t.Errorf("should be test but got='%s'", txt)
}
})))
f, err := os.Open(tt.data)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { f.Close() })
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", f)
req.Header.Set("Content-Encoding", tt.encoding)
mux.ServeHTTP(rec, req)
result := rec.Result()
if result.StatusCode != http.StatusOK {
t.Errorf("%v", result)
}
})
}
}
func TestDecode_WithDecoder(t *testing.T) {
customDecoder := &contentencoding.Decoder{
Encoding: "custom",
Handler: func(w http.ResponseWriter, r *http.Request) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(strings.NewReader(string(b) + "-custom"))
return nil
},
}
mux := http.NewServeMux()
dm := contentencoding.Decode(contentencoding.WithDecoder(customDecoder))
mux.Handle("/", dm(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
txt := strings.TrimSpace(string(b))
if txt != "test-custom" {
t.Errorf("should be test but got='%s'", txt)
}
})))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("test"))
req.Header.Set("Content-Encoding", "custom")
mux.ServeHTTP(rec, req)
result := rec.Result()
if result.StatusCode != http.StatusOK {
t.Errorf("%v", result)
}
}
func TestDecode_WithErrorHandler(t *testing.T) {
mux := http.NewServeMux()
errHandler := contentencoding.ErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(999) // custom error code
})
dm := contentencoding.Decode(contentencoding.WithErrorHandler(errHandler))
mux.Handle("/", dm(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("test")) // not compressed
req.Header.Set("Content-Encoding", "gzip")
mux.ServeHTTP(rec, req)
result := rec.Result()
if result.StatusCode != 999 {
t.Errorf("invalid Accept-Encoding, %v", result)
}
}