-
Notifications
You must be signed in to change notification settings - Fork 1
/
static_test.go
173 lines (138 loc) · 4.35 KB
/
static_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package static_test
import (
"bytes"
"compress/gzip"
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"github.com/gin-gonic/gin"
. "github.com/hyperboloide/static"
"github.com/hyperboloide/static/demo/files"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Static", func() {
Context("Basic Handler", func() {
var ts *httptest.Server
var staticHandler *Handler
BeforeEach(func() {
r := gin.Default()
rootGroup := r.Group("/")
{
staticHandler = NewHandler(files.Asset, files.AssetNames)
staticHandler.Register(rootGroup)
}
ts = httptest.NewServer(r)
})
AfterEach(func() {
ts.Close()
})
checkFile := func(pth, contentType string, maxAge int) {
resp, err := http.Get(ts.URL + "/" + pth)
Ω(err).ToNot(HaveOccurred())
body, err := ioutil.ReadAll(resp.Body)
Ω(err).ToNot(HaveOccurred())
err = resp.Body.Close()
Ω(err).ToNot(HaveOccurred())
Ω(resp.StatusCode).To(Equal(200))
ma := fmt.Sprintf("max-age=%d", maxAge)
Ω(resp.Header.Get("Cache-Control")).To(Equal(ma))
Ω(resp.Header.Get("Content-Type")).To(Equal(contentType))
size := len(files.MustAsset(pth))
Ω(resp.Header.Get("Content-Length")).To(Equal(fmt.Sprintf("%d", size)))
eq := bytes.Equal(body, files.MustAsset(pth))
Ω(eq).To(BeTrue())
sum := md5.Sum(body)
Ω(resp.Header.Get("Etag")).To(Equal(fmt.Sprintf("%x", sum)))
}
It("should get index.html", func() {
checkFile("index.html", "text/html; charset=utf-8", 86400)
})
It("should get test.jpg", func() {
checkFile("test.jpg", "image/jpeg", 86400)
})
It("should get bootstrap/css/bootstrap.min.css", func() {
checkFile("bootstrap/css/bootstrap.min.css", "text/css; charset=utf-8", 86400)
})
It("should return 404 for /", func() {
resp, err := http.Get(ts.URL + "/")
Ω(err).ToNot(HaveOccurred())
Ω(resp.StatusCode).To(Equal(404))
})
It("should not return 404 for / if 'index.html' is added to indexes", func() {
staticHandler.AddIndexes("index.html")
resp, err := http.Get(ts.URL + "/")
Ω(err).ToNot(HaveOccurred())
Ω(resp.StatusCode).To(Equal(200))
})
})
Context("Gzip Handler", func() {
var ts *httptest.Server
var gzHandler *Handler
BeforeEach(func() {
r := gin.Default()
rootGroup := r.Group("/")
{
var err error
gzHandler, err = NewGzipHandler(files.Asset, files.AssetNames, gzip.BestCompression)
Ω(err).ToNot(HaveOccurred())
gzHandler.Register(rootGroup)
}
ts = httptest.NewServer(r)
})
AfterEach(func() {
ts.Close()
})
checkFile := func(pth, contentType string, maxAge int) {
client := new(http.Client)
rqst, err := http.NewRequest("GET", ts.URL+"/"+pth, nil)
rqst.Header.Add("Accept-Encoding", "gzip")
resp, err := client.Do(rqst)
defer resp.Body.Close()
Ω(err).ToNot(HaveOccurred())
body, err := ioutil.ReadAll(resp.Body)
Ω(err).ToNot(HaveOccurred())
err = resp.Body.Close()
Ω(err).ToNot(HaveOccurred())
Ω(resp.StatusCode).To(Equal(200))
ma := fmt.Sprintf("max-age=%d", maxAge)
Ω(resp.Header.Get("Cache-Control")).To(Equal(ma))
Ω(resp.Header.Get("Content-Type")).To(Equal(contentType))
Ω(resp.Header.Get("Content-Encoding")).To(Equal("gzip"))
// Check is gziped
var b bytes.Buffer
gw, err := gzip.NewWriterLevel(&b, gzip.BestCompression)
Ω(err).ToNot(HaveOccurred())
_, err = gw.Write(files.MustAsset(pth))
Ω(err).ToNot(HaveOccurred())
err = gw.Close()
Ω(err).ToNot(HaveOccurred())
eq := bytes.Equal(body, b.Bytes())
Ω(eq).To(BeTrue())
sum := md5.Sum(body)
Ω(resp.Header.Get("Etag")).To(Equal(fmt.Sprintf("%x", sum)))
}
It("should get index.html", func() {
checkFile("index.html", "text/html; charset=utf-8", 86400)
})
It("should get test.jpg", func() {
checkFile("test.jpg", "image/jpeg", 86400)
})
It("should get bootstrap/css/bootstrap.min.css", func() {
checkFile("bootstrap/css/bootstrap.min.css", "text/css; charset=utf-8", 86400)
})
It("should return 404 for /", func() {
resp, err := http.Get(ts.URL + "/")
Ω(err).ToNot(HaveOccurred())
Ω(resp.StatusCode).To(Equal(404))
})
It("should not return 404 for / if 'index.html' is added to indexes", func() {
gzHandler.AddIndexes("index.html")
resp, err := http.Get(ts.URL + "/")
Ω(err).ToNot(HaveOccurred())
Ω(resp.StatusCode).To(Equal(200))
})
})
})