package middleware | package middleware | ||||
import ( | |||||
"io" | |||||
"net/http" | |||||
"net/http/httptest" | |||||
"testing" | |||||
"github.com/goanywhere/rex" | |||||
. "github.com/smartystreets/goconvey/convey" | |||||
) | |||||
func TestCompress(t *testing.T) { | |||||
app := rex.New() | |||||
app.Use(Compress) | |||||
app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "app") | |||||
}) | |||||
Convey("rex.middleware.Compress", t, func() { | |||||
request, _ := http.NewRequest("GET", "/", nil) | |||||
request.Header.Set("Accept-Encoding", "gzip") | |||||
response := httptest.NewRecorder() | |||||
app.ServeHTTP(response, request) | |||||
So(response.Header().Get("Content-Encoding"), ShouldEqual, "gzip") | |||||
}) | |||||
} |
func Static(dir string) func(http.Handler) http.Handler { | func Static(dir string) func(http.Handler) http.Handler { | ||||
var ( | var ( | ||||
fs = http.Dir(dir) | fs = http.Dir(dir) | ||||
prefix = path.Join("/", dir) | |||||
prefix = path.Join("/", path.Base(path.Dir(dir))) | |||||
) | ) | ||||
return func(next http.Handler) http.Handler { | return func(next http.Handler) http.Handler { | ||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
package middleware | |||||
import ( | |||||
"io" | |||||
"net/http" | |||||
"net/http/httptest" | |||||
"os" | |||||
"path" | |||||
"testing" | |||||
"github.com/goanywhere/rex" | |||||
. "github.com/smartystreets/goconvey/convey" | |||||
) | |||||
func TestStatic(t *testing.T) { | |||||
tempdir := os.TempDir() | |||||
filename := path.Join(tempdir, "favicon.ico") | |||||
app := rex.New() | |||||
app.Use(Static(tempdir)) | |||||
prefix := path.Join("/", path.Base(path.Dir(filename))) | |||||
app.GET(prefix, func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "app") | |||||
}) | |||||
Convey("rex.middleware.Static", t, func() { | |||||
request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil) | |||||
response := httptest.NewRecorder() | |||||
app.ServeHTTP(response, request) | |||||
So(response.Code, ShouldEqual, http.StatusOK) | |||||
}) | |||||
} |