diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-02-15 16:44:29 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-02-15 16:44:29 -0700 |
commit | 46ad450327111b9d28b592658d75ef57da498298 (patch) | |
tree | 2b837bac9ae36d5a34dda06ba745850da216257d /handler_test.go | |
parent | bc96af40db6104580c22086c8db7c8119a404257 (diff) |
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.
Diffstat (limited to 'handler_test.go')
-rw-r--r-- | handler_test.go | 22 |
1 files changed, 11 insertions, 11 deletions
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) } |