diff options
Diffstat (limited to 'gemini/gemtext/sub_test.go')
-rw-r--r-- | gemini/gemtext/sub_test.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/gemini/gemtext/sub_test.go b/gemini/gemtext/sub_test.go index f080705..9e0fcc8 100644 --- a/gemini/gemtext/sub_test.go +++ b/gemini/gemtext/sub_test.go @@ -2,8 +2,15 @@ package gemtext import ( "bytes" + "context" + "fmt" + "io" "net/url" "testing" + + "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/gemini" + "tildegit.org/tjp/sliderule/internal/types" ) func TestGemsubToAtom(t *testing.T) { @@ -61,3 +68,71 @@ func TestGemsubToAtom(t *testing.T) { }) } } + +func TestAutoAtom(t *testing.T) { + rout := &sliderule.Router{} + + rout.Route("/foo.gmi", types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response { + return gemini.Success("text/gemini", bytes.NewBufferString(` +# This is my gemini page + +## a subtitle + +=> ./first-post.gmi 2023-05-17 - My first post +=> ./second-post.gmi 2023-06-02 second-ever post +`[1:])) + })) + + rout.Route("/bar.gmi", types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response { + return gemini.Success("text/gemini", bytes.NewBufferString(` +# Another homepage + +=> ./first-post.gmi 2023-05-17 - first post +=> ./second-post.gmi 2023-06-02 second post +`[1:])) + })) + + h := AutoAtom(rout.Handler()) + + response := h.Handle(context.Background(), &types.Request{URL: &url.URL{ + Scheme: "gemini", + Host: "127.0.0.1", + Path: "/foo.gmi.atom", + }}) + if response.Status != gemini.StatusSuccess { + t.Fatal("bad response code") + } + + result, err := io.ReadAll(response.Body) + if err != nil { + t.Fatal(err) + } + + target := ` +<?xml version="1.0" encoding="utf-8"?> +<feed xmlns="http://www.w3.org/2005/Atom"> + <id>gemini://127.0.0.1/foo.gmi.atom</id> + <link href="gemini://127.0.0.1/foo.gmi.atom"/> + <title>This is my gemini page</title> + <subtitle>a subtitle</subtitle> + <updated>2023-06-02T12:00:00Z</updated> + <entry> + <id>./first-post.gmi</id> + <link rel="alternate" href="./first-post.gmi"/> + <title>My first post</title> + <updated>2023-05-17T12:00:00Z</updated> + </entry> + <entry> + <id>./second-post.gmi</id> + <link rel="alternate" href="./second-post.gmi"/> + <title>second-ever post</title> + <updated>2023-06-02T12:00:00Z</updated> + </entry> +</feed> +`[1:] + if string(result) != target { + fmt.Println(target) + fmt.Println(string(result)) + t.Fatal("response body") + } +} |