From e42652bfd3e60c862f089c6cf03d8cf0623400ef Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Wed, 3 Apr 2019 22:31:56 -0400 Subject: [PATCH] start validating handler --- builder/builder.go | 7 ++++- builder/builder_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/builder/builder.go b/builder/builder.go index 40e4eee..4e27991 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -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), ) diff --git a/builder/builder_test.go b/builder/builder_test.go index ef76099..340d654 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -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 + }) + } +} -- 2.30.1