-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip_handler_test.go
134 lines (97 loc) · 3.45 KB
/
gzip_handler_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
// Copyright (c) 2016 Sebastian Mancke and eBay, both MIT licensed
package gzip_handler
import (
"bytes"
"compress/gzip"
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"testing"
"gopkg.in/go-extras/testing.v1/assert"
)
var contentTypes = regexp.MustCompile(`^(text/.*|application/(javascript|json|font-woff|xml)|.*\+(json|xml))(;.*)?$`)
func Test_GzipHandler_CompressableType(t *testing.T) {
server := httptest.NewServer(NewGzipHandler(test_text_handler(), contentTypes))
assertEqual := assert.Equal(t)
r, err := http.NewRequest("GET", server.URL, nil)
assertEqual(err, nil)
r.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(r)
assertEqual(err, nil)
assertEqual(resp.Header.Get("Content-Type"), "text/plain; charset=utf-8")
assertEqual(resp.Header.Get("Content-Encoding"), "gzip")
gzBytes, err := ioutil.ReadAll(resp.Body)
assertEqual(err, nil)
assertEqual(resp.Header.Get("Content-Length"), strconv.Itoa(len(gzBytes)))
reader, err := gzip.NewReader(bytes.NewBuffer(gzBytes))
assertEqual(err, nil)
defer reader.Close()
bytes, err := ioutil.ReadAll(reader)
assertEqual(err, nil)
assertEqual(string(bytes), "Hello World")
}
func Test_GzipHandler_NotCompressingTwice(t *testing.T) {
server := httptest.NewServer(NewGzipHandler(test_already_compressed_handler(), contentTypes))
assertEqual := assert.Equal(t)
r, err := http.NewRequest("GET", server.URL, nil)
assertEqual(err, nil)
r.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(r)
assertEqual(err, nil)
assertEqual(resp.Header.Get("Content-Encoding"), "gzip")
reader, err := gzip.NewReader(resp.Body)
assertEqual(err, nil)
defer reader.Close()
bytes, err := ioutil.ReadAll(reader)
assertEqual(err, nil)
assertEqual(string(bytes), "Hello World")
}
func Test_GzipHandler_CompressableType_NoAccept(t *testing.T) {
server := httptest.NewServer(NewGzipHandler(test_text_handler(), contentTypes))
assertEqual := assert.Equal(t)
r, err := http.NewRequest("GET", server.URL, nil)
assertEqual(err, nil)
r.Header.Set("Accept-Encoding", "none")
resp, err := http.DefaultClient.Do(r)
assertEqual(err, nil)
assertEqual(resp.Header.Get("Content-Encoding"), "")
bytes, err := ioutil.ReadAll(resp.Body)
assertEqual(err, nil)
assertEqual(string(bytes), "Hello World")
}
func Test_GzipHandler_NonCompressableType(t *testing.T) {
server := httptest.NewServer(NewGzipHandler(test_binary_handler(), contentTypes))
assertEqual := assert.Equal(t)
r, err := http.NewRequest("GET", server.URL, nil)
assertEqual(err, nil)
r.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(r)
assertEqual(err, nil)
assertEqual(resp.Header.Get("Content-Encoding"), "")
bytes, err := ioutil.ReadAll(resp.Body)
assertEqual(err, nil)
assertEqual(bytes, []byte{42})
}
func test_text_handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := []byte("Hello World")
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Write(b)
})
}
func test_binary_handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/jpg")
w.Write([]byte{42})
})
}
func test_already_compressed_handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", "gzip")
gzWriter := gzip.NewWriter(w)
gzWriter.Write([]byte("Hello World"))
gzWriter.Close()
})
}