-
Notifications
You must be signed in to change notification settings - Fork 41
/
compression_test.go
54 lines (50 loc) · 1.37 KB
/
compression_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
package antch
import (
"compress/gzip"
"compress/zlib"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestCompressionHandlerWithGzip(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
msg := []byte("hello world")
w.Header().Set("Content-Encoding", "gzip")
zw := gzip.NewWriter(w)
defer zw.Close()
zw.Write(msg)
}))
defer ts.Close()
testCompressionHandler(t, ts, []byte("hello world"))
}
func TestCompressionHandlerWithDeflate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
msg := []byte("hello world")
w.Header().Set("Content-Encoding", "deflate")
zw := zlib.NewWriter(w)
defer zw.Close()
zw.Write(msg)
}))
defer ts.Close()
testCompressionHandler(t, ts, []byte("hello world"))
}
func testCompressionHandler(t *testing.T, ts *httptest.Server, want []byte) {
handler := CompressionMiddleware()(defaultMessageHandler())
req, _ := http.NewRequest("GET", ts.URL, nil)
resp, err := handler.Send(req)
if err != nil {
t.Fatal(err)
}
if v := resp.Header.Get("Content-Encoding"); v != "" {
t.Errorf("Content-Encoding = %s; want empty", v)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
if v := string(b); v != string(want) {
t.Errorf("body = %s; want %s", v, want)
}
}