From 46ad450327111b9d28b592658d75ef57da498298 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 15 Feb 2023 16:44:29 -0700 Subject: Switch Handler to an interface. HandlerFunc is much better as a function returning a Handler, rather than a newtype for the function type itself. This way there is no confusion creating a type-inferenced variable with HandlerFunc(func(... and then using a HandlerFunc where a Handler is expected. Much better to only have one public type. --- handler_test.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'handler_test.go') diff --git a/handler_test.go b/handler_test.go index a83ef3b..18ef562 100644 --- a/handler_test.go +++ b/handler_test.go @@ -13,19 +13,19 @@ import ( ) func TestFallthrough(t *testing.T) { - h1 := func(ctx context.Context, req *gus.Request) *gus.Response { + h1 := gus.HandlerFunc(func(ctx context.Context, req *gus.Request) *gus.Response { if req.Path == "/one" { return gemini.Success("text/gemini", bytes.NewBufferString("one")) } return nil - } + }) - h2 := func(ctx context.Context, req *gus.Request) *gus.Response { + h2 := gus.HandlerFunc(func(ctx context.Context, req *gus.Request) *gus.Response { if req.Path == "/two" { return gemini.Success("text/gemini", bytes.NewBufferString("two")) } return nil - } + }) fth := gus.FallthroughHandler(h1, h2) @@ -34,7 +34,7 @@ func TestFallthrough(t *testing.T) { t.Fatalf("url.Parse: %s", err.Error()) } - resp := fth(context.Background(), &gus.Request{URL: u}) + resp := fth.Handle(context.Background(), &gus.Request{URL: u}) if resp.Status != gemini.StatusSuccess { t.Errorf("expected status %d, got %d", gemini.StatusSuccess, resp.Status) @@ -57,7 +57,7 @@ func TestFallthrough(t *testing.T) { t.Fatalf("url.Parse: %s", err.Error()) } - resp = fth(context.Background(), &gus.Request{URL: u}) + resp = fth.Handle(context.Background(), &gus.Request{URL: u}) if resp.Status != gemini.StatusSuccess { t.Errorf("expected status %d, got %d", gemini.StatusSuccess, resp.Status) @@ -80,7 +80,7 @@ func TestFallthrough(t *testing.T) { t.Fatalf("url.Parse: %s", err.Error()) } - resp = fth(context.Background(), &gus.Request{URL: u}) + resp = fth.Handle(context.Background(), &gus.Request{URL: u}) if resp != nil { t.Errorf("expected nil, got %+v", resp) @@ -91,9 +91,9 @@ func TestFilter(t *testing.T) { pred := func(ctx context.Context, req *gus.Request) bool { return strings.HasPrefix(req.Path, "/allow") } - base := func(ctx context.Context, req *gus.Request) *gus.Response { + base := gus.HandlerFunc(func(ctx context.Context, req *gus.Request) *gus.Response { return gemini.Success("text/gemini", bytes.NewBufferString("allowed!")) - } + }) handler := gus.Filter(pred, nil)(base) u, err := url.Parse("gemini://test.local/allow/please") @@ -101,7 +101,7 @@ func TestFilter(t *testing.T) { t.Fatalf("url.Parse: %s", err.Error()) } - resp := handler(context.Background(), &gus.Request{URL: u}) + resp := handler.Handle(context.Background(), &gus.Request{URL: u}) if resp.Status != gemini.StatusSuccess { t.Errorf("expected status %d, got %d", gemini.StatusSuccess, resp.Status) } @@ -111,7 +111,7 @@ func TestFilter(t *testing.T) { t.Fatalf("url.Parse: %s", err.Error()) } - resp = handler(context.Background(), &gus.Request{URL: u}) + resp = handler.Handle(context.Background(), &gus.Request{URL: u}) if resp != nil { t.Errorf("expected nil, got %+v", resp) } -- cgit v1.2.3