~mna/kick

e42652bfd3e60c862f089c6cf03d8cf0623400ef — Martin Angers 1 year, 1 month ago 7686db5
start validating handler
2 files changed, 66 insertions(+), 1 deletions(-)

M builder/builder.go
M builder/builder_test.go
M builder/builder.go => builder/builder.go +6 -1
@@ 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),
		)

M builder/builder_test.go => builder/builder_test.go +60 -0
@@ 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
		})
	}
}