@@ 3,6 3,7 @@
package builder
import (
+ "compress/gzip"
"crypto/tls"
"errors"
"fmt"
@@ 191,8 192,12 @@ func rootMiddleware(s *kick.Server, router *httprouter.Router) ([]middleware, er
}
if conf := s.Root.Gzip; conf != nil {
+ level := conf.CompressionLevel
+ if level == 0 {
+ level = gzip.DefaultCompression
+ }
gzh, err := gziphandler.GzipHandlerWithOpts(
- gziphandler.CompressionLevel(conf.CompressionLevel),
+ gziphandler.CompressionLevel(level),
gziphandler.ContentTypes(conf.ContentTypes),
gziphandler.MinSize(conf.MinSize),
)
@@ 3,6 3,7 @@ package builder
import (
"crypto/tls"
"fmt"
+ "io"
"net/http"
"net/http/httptest"
"os"
@@ 10,6 11,7 @@ import (
"testing"
"git.sr.ht/~mna/kick"
+ "github.com/unrolled/secure"
)
func TestTLS_Intermediate(t *testing.T) {
@@ 265,3 267,61 @@ func TestHandler_InvalidRoute(t *testing.T) {
})
}
}
+
+func TestHandler_Valid(t *testing.T) {
+ s := &kick.Server{
+ Root: &kick.Root{
+ MethodNotAllowedHandler: statusHandler(405),
+ NotFoundHandler: statusHandler(404),
+ PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}) {
+ w.WriteHeader(500)
+ },
+ TrustProxyHeaders: true,
+ AllowMethodOverride: true,
+ RequestIDHeader: "X-Request-Id",
+ SecurityHeaders: &secure.Options{
+ FrameDeny: true,
+ },
+ Gzip: &kick.GzipConfig{
+ ContentTypes: []string{"application/xml"},
+ },
+ },
+ Routes: []*kick.Route{
+ {
+ Method: "GET",
+ Path: "/",
+ Handler: statusHandler(200),
+ },
+ {
+ Method: "POST",
+ Path: "/panic",
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ panic(io.EOF)
+ }),
+ },
+ },
+ }
+
+ h, err := Handler(s)
+ if err != nil {
+ t.Fatalf("want no error, got %s", err)
+ }
+
+ cases := []struct {
+ req string // formatted as "METHOD /path/... body"
+ reqh string // formatted as "Name:Value Name:Value"
+ code int
+ resh string // formatted as "Name:Value", * means any value
+ }{
+ {
+ req: "GET /",
+ code: 200,
+ resh: "X-Request-Id:*",
+ },
+ }
+ for _, c := range cases {
+ t.Run(fmt.Sprintf("%s %s", c.req, c.reqh), func(t *testing.T) {
+ _ = h
+ })
+ }
+}