2015-06-06 18:58:37 -05:00
|
|
|
package gzip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
2015-06-06 19:27:36 -05:00
|
|
|
func TestGzipHandler(t *testing.T) {
|
2015-06-06 18:58:37 -05:00
|
|
|
|
|
|
|
pathFilter := PathFilter{make(Set)}
|
|
|
|
badPaths := []string{"/bad", "/nogzip", "/nongzip"}
|
|
|
|
for _, p := range badPaths {
|
|
|
|
pathFilter.IgnoredPaths.Add(p)
|
|
|
|
}
|
2015-06-10 15:59:08 -05:00
|
|
|
extFilter := ExtFilter{make(Set)}
|
|
|
|
for _, e := range []string{".txt", ".html", ".css", ".md"} {
|
|
|
|
extFilter.Exts.Add(e)
|
|
|
|
}
|
2015-06-06 18:58:37 -05:00
|
|
|
gz := Gzip{Configs: []Config{
|
2015-12-07 17:17:05 -05:00
|
|
|
{RequestFilters: []RequestFilter{pathFilter, extFilter}},
|
2015-06-06 18:58:37 -05:00
|
|
|
}}
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
gz.Next = nextFunc(true)
|
2015-06-10 15:59:08 -05:00
|
|
|
var exts = []string{
|
|
|
|
".html", ".css", ".md",
|
|
|
|
}
|
|
|
|
for _, e := range exts {
|
2015-06-06 18:58:37 -05:00
|
|
|
url := "/file" + e
|
|
|
|
r, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
_, err = gz.ServeHTTP(w, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
gz.Next = nextFunc(false)
|
|
|
|
for _, p := range badPaths {
|
2015-06-10 15:59:08 -05:00
|
|
|
for _, e := range exts {
|
2015-06-06 18:58:37 -05:00
|
|
|
url := p + "/file" + e
|
|
|
|
r, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
_, err = gz.ServeHTTP(w, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
gz.Next = nextFunc(false)
|
2015-06-10 15:59:08 -05:00
|
|
|
exts = []string{
|
2015-06-06 18:58:37 -05:00
|
|
|
".htm1", ".abc", ".mdx",
|
|
|
|
}
|
|
|
|
for _, e := range exts {
|
|
|
|
url := "/file" + e
|
|
|
|
r, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
_, err = gz.ServeHTTP(w, r)
|
2015-06-10 15:59:08 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-06 18:58:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func nextFunc(shouldGzip bool) middleware.Handler {
|
|
|
|
return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-12-08 06:01:24 -05:00
|
|
|
w.Write([]byte("test"))
|
2015-06-06 18:58:37 -05:00
|
|
|
if shouldGzip {
|
|
|
|
if r.Header.Get("Accept-Encoding") != "" {
|
|
|
|
return 0, fmt.Errorf("Accept-Encoding header not expected")
|
|
|
|
}
|
|
|
|
if w.Header().Get("Content-Encoding") != "gzip" {
|
|
|
|
return 0, fmt.Errorf("Content-Encoding must be gzip, found %v", r.Header.Get("Content-Encoding"))
|
|
|
|
}
|
2015-11-13 15:59:38 -05:00
|
|
|
if w.Header().Get("Vary") != "Accept-Encoding" {
|
|
|
|
return 0, fmt.Errorf("Vary must be Accept-Encoding, found %v", r.Header.Get("Vary"))
|
|
|
|
}
|
2015-12-18 14:58:23 -05:00
|
|
|
if _, ok := w.(*gzipResponseWriter); !ok {
|
2015-06-06 18:58:37 -05:00
|
|
|
return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w)
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if r.Header.Get("Accept-Encoding") == "" {
|
|
|
|
return 0, fmt.Errorf("Accept-Encoding header expected")
|
|
|
|
}
|
|
|
|
if w.Header().Get("Content-Encoding") == "gzip" {
|
|
|
|
return 0, fmt.Errorf("Content-Encoding must not be gzip, found gzip")
|
|
|
|
}
|
2015-12-18 14:58:23 -05:00
|
|
|
if _, ok := w.(*gzipResponseWriter); ok {
|
2015-06-06 18:58:37 -05:00
|
|
|
return 0, fmt.Errorf("ResponseWriter should not be gzipResponseWriter")
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
})
|
|
|
|
}
|