summaryrefslogtreecommitdiff
path: root/handler_test.go
blob: 048e8be3a224d9d4e80b6c869c0c8fb98816daea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package sliderule_test

import (
	"bytes"
	"context"
	"io"
	"net/url"
	"strings"
	"testing"

	sr "tildegit.org/tjp/sliderule"
	"tildegit.org/tjp/sliderule/gemini"
)

func TestFallthrough(t *testing.T) {
	h1 := sr.HandlerFunc(func(ctx context.Context, req *sr.Request) *sr.Response {
		if req.Path == "/one" {
			return gemini.Success("text/gemini", bytes.NewBufferString("one"))
		}
		return nil
	})

	h2 := sr.HandlerFunc(func(ctx context.Context, req *sr.Request) *sr.Response {
		if req.Path == "/two" {
			return gemini.Success("text/gemini", bytes.NewBufferString("two"))
		}
		return nil
	})

	fth := sr.FallthroughHandler(h1, h2)

	u, err := url.Parse("gemini://test.local/one")
	if err != nil {
		t.Fatalf("url.Parse: %s", err.Error())
	}

	resp := fth.Handle(context.Background(), &sr.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.Handle(context.Background(), &sr.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.Handle(context.Background(), &sr.Request{URL: u})

	if resp != nil {
		t.Errorf("expected nil, got %+v", resp)
	}
}

func TestFilter(t *testing.T) {
	pred := func(ctx context.Context, req *sr.Request) bool {
		return strings.HasPrefix(req.Path, "/allow")
	}
	base := sr.HandlerFunc(func(ctx context.Context, req *sr.Request) *sr.Response {
		return gemini.Success("text/gemini", bytes.NewBufferString("allowed!"))
	})
	handler := sr.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.Handle(context.Background(), &sr.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.Handle(context.Background(), &sr.Request{URL: u})
	if resp != nil {
		t.Errorf("expected nil, got %+v", resp)
	}
}