-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgzip_test.go
70 lines (60 loc) · 1.83 KB
/
gzip_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
package httpgzip_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/shurcooL/httpgzip"
)
// Test that ServeContent correctly determines the content type as "text/plain",
// not as "application/x-gzip".
func TestServeContentDetectContentType(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
content := "This is some plain text that compresses easily. " +
strings.Repeat("NaN", 16) + " Batman!"
httpgzip.ServeContent(w, req, "", time.Time{}, strings.NewReader(content))
}))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
got := resp.Header.Get("Content-Type")
want := "text/plain; charset=utf-8"
if got != want {
t.Errorf("got:\n%v\nwant:\n%v\n", got, want)
}
}
// Test that if the handler already explicitly set "Content-Encoding" header,
// then ServeContent shouldn't try to do apply compression, just serve as is.
func TestServeContentExplicitContentEncoding(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
content := "This is some plain text that compresses easily. " +
strings.Repeat("NaN", 16) + " Batman!"
w.Header()["Content-Encoding"] = nil
httpgzip.ServeContent(w, req, "", time.Time{}, strings.NewReader(content))
}))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
got := resp.Header.Get("Content-Encoding")
want := ""
if got != want {
t.Errorf("got:\n%q\nwant:\n%q\n", got, want)
}
}