diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-01-17 15:59:29 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-01-17 15:59:29 -0700 |
commit | 2ef530daa47b301a40c1ee93cd43b8f36fc68c0b (patch) | |
tree | b9753719f5f0e5312bb5008d40f40247ce14e15a /handler_test.go | |
parent | 30e21f8513d49661cb6e1583d301e34e898d48a9 (diff) |
pull request, response, handlers out of the gemini package
Diffstat (limited to 'handler_test.go')
-rw-r--r-- | handler_test.go | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/handler_test.go b/handler_test.go new file mode 100644 index 0000000..a83ef3b --- /dev/null +++ b/handler_test.go @@ -0,0 +1,118 @@ +package gus_test + +import ( + "bytes" + "context" + "io" + "net/url" + "strings" + "testing" + + "tildegit.org/tjp/gus" + "tildegit.org/tjp/gus/gemini" +) + +func TestFallthrough(t *testing.T) { + h1 := 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 { + if req.Path == "/two" { + return gemini.Success("text/gemini", bytes.NewBufferString("two")) + } + return nil + } + + fth := gus.FallthroughHandler(h1, h2) + + u, err := url.Parse("gemini://test.local/one") + if err != nil { + t.Fatalf("url.Parse: %s", err.Error()) + } + + resp := fth(context.Background(), &gus.Request{URL: u}) + + if resp.Status != gemini.StatusSuccess { + t.Errorf("expected status %d, got %d", gemini.StatusSuccess, resp.Status) + } + + if resp.Meta != "text/gemini" { + t.Errorf(`expected meta "text/gemini", got %q`, resp.Meta) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Errorf("Read: %s", err.Error()) + } + if string(body) != "one" { + t.Errorf(`expected body "one", got %q`, string(body)) + } + + u, err = url.Parse("gemini://test.local/two") + if err != nil { + t.Fatalf("url.Parse: %s", err.Error()) + } + + resp = fth(context.Background(), &gus.Request{URL: u}) + + if resp.Status != gemini.StatusSuccess { + t.Errorf("expected status %d, got %d", gemini.StatusSuccess, resp.Status) + } + + if resp.Meta != "text/gemini" { + t.Errorf(`expected meta "text/gemini", got %q`, resp.Meta) + } + + body, err = io.ReadAll(resp.Body) + if err != nil { + t.Errorf("Read: %s", err.Error()) + } + if string(body) != "two" { + t.Errorf(`expected body "two", got %q`, string(body)) + } + + u, err = url.Parse("gemini://test.local/three") + if err != nil { + t.Fatalf("url.Parse: %s", err.Error()) + } + + resp = fth(context.Background(), &gus.Request{URL: u}) + + if resp != nil { + t.Errorf("expected nil, got %+v", resp) + } +} + +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 { + return gemini.Success("text/gemini", bytes.NewBufferString("allowed!")) + } + handler := gus.Filter(pred, nil)(base) + + u, err := url.Parse("gemini://test.local/allow/please") + if err != nil { + t.Fatalf("url.Parse: %s", err.Error()) + } + + resp := handler(context.Background(), &gus.Request{URL: u}) + if resp.Status != gemini.StatusSuccess { + t.Errorf("expected status %d, got %d", gemini.StatusSuccess, resp.Status) + } + + u, err = url.Parse("gemini://test.local/disallow/please") + if err != nil { + t.Fatalf("url.Parse: %s", err.Error()) + } + + resp = handler(context.Background(), &gus.Request{URL: u}) + if resp != nil { + t.Errorf("expected nil, got %+v", resp) + } +} |