This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathcompress_test.go
126 lines (105 loc) · 2.75 KB
/
compress_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
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"bytes"
"compress/flate"
"compress/gzip"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type CompressExample struct {
Compress // add this for ask compress according request accept-encoding
}
func (CompressExample) Get() string {
return fmt.Sprintf("This is a auto compress text")
}
type GZipExample struct {
GZip // add this for ask compress to GZip
}
func (GZipExample) Get() string {
return fmt.Sprintf("This is a gzip compress text")
}
type DeflateExample struct {
Deflate // add this for ask compress to Deflate, if not support then not compress
}
func (DeflateExample) Get() string {
return fmt.Sprintf("This is a deflate compress text")
}
type NoCompress struct {
}
func (NoCompress) Get() string {
return fmt.Sprintf("This is a non-compress text")
}
func TestCompressAuto(t *testing.T) {
o := Classic()
o.Get("/", new(CompressExample))
testCompress(t, o, "http://localhost:8000/",
"This is a auto compress text", "gzip")
}
func TestCompressGzip(t *testing.T) {
o := Classic()
o.Get("/", new(GZipExample))
testCompress(t, o, "http://localhost:8000/",
"This is a gzip compress text", "gzip")
}
func TestCompressDeflate(t *testing.T) {
o := Classic()
o.Get("/", new(DeflateExample))
testCompress(t, o, "http://localhost:8000/",
"This is a deflate compress text", "deflate")
}
func TestCompressNon(t *testing.T) {
o := Classic()
o.Get("/", new(NoCompress))
testCompress(t, o, "http://localhost:8000/",
"This is a non-compress text", "")
}
func TestCompressStatic(t *testing.T) {
o := New()
o.Use(Compresses([]string{".html"}))
o.Use(ClassicHandlers...)
testCompress(t, o, "http://localhost:8000/public/test.html",
"hello tango", "gzip")
}
func testCompress(t *testing.T, o *Tango, url, content, enc string) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Error(err)
}
req.Header.Add("Accept-Encoding", "gzip, deflate")
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
ce := recorder.Header().Get("Content-Encoding")
if ce == "gzip" {
r, err := gzip.NewReader(buff)
if err != nil {
t.Error(err)
}
defer r.Close()
bs, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
expect(t, string(bs), content)
} else if ce == "deflate" {
r := flate.NewReader(buff)
defer r.Close()
bs, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
expect(t, string(bs), content)
} else {
expect(t, buff.String(), content)
}
expect(t, enc, ce)
}