From 029cd5b52db8385d221690cbf75e5e4825e145d8 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 11 Jan 2023 13:19:42 -0700 Subject: much more extensive gus/gemini testing --- gemini/handler_test.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 gemini/handler_test.go (limited to 'gemini/handler_test.go') diff --git a/gemini/handler_test.go b/gemini/handler_test.go new file mode 100644 index 0000000..c83df65 --- /dev/null +++ b/gemini/handler_test.go @@ -0,0 +1,117 @@ +package gemini_test + +import ( + "bytes" + "context" + "io" + "net/url" + "strings" + "testing" + + "tildegit.org/tjp/gus/gemini" +) + +func TestFallthrough(t *testing.T) { + h1 := func(ctx context.Context, req *gemini.Request) *gemini.Response { + if req.Path == "/one" { + return gemini.Success("text/gemini", bytes.NewBufferString("one")) + } + return gemini.NotFound("nope") + } + + h2 := func(ctx context.Context, req *gemini.Request) *gemini.Response { + if req.Path == "/two" { + return gemini.Success("text/gemini", bytes.NewBufferString("two")) + } + return gemini.NotFound("no way") + } + + fth := gemini.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(), &gemini.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(), &gemini.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(), &gemini.Request{URL: u}) + + if resp.Status != gemini.StatusNotFound { + t.Errorf("expected status %d, got %d", gemini.StatusNotFound, resp.Status) + } +} + +func TestFilter(t *testing.T) { + pred := func(ctx context.Context, req *gemini.Request) bool { + return strings.HasPrefix(req.Path, "/allow") + } + base := func(ctx context.Context, req *gemini.Request) *gemini.Response { + return gemini.Success("text/gemini", bytes.NewBufferString("allowed!")) + } + handler := gemini.Filter(pred, base, nil) + + u, err := url.Parse("gemini://test.local/allow/please") + if err != nil { + t.Fatalf("url.Parse: %s", err.Error()) + } + + resp := handler(context.Background(), &gemini.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(), &gemini.Request{URL: u}) + if resp.Status != gemini.StatusNotFound { + t.Errorf("expected status %d, got %d", gemini.StatusNotFound, resp.Status) + } +} -- cgit v1.2.3