From e1aa19f1e8c4a68b43add79374d6dfeae8bd9921 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Thu, 19 Jan 2023 16:03:52 -0700 Subject: New sharedhost contrib package. ReplaceTilde simply replaces a leading ~ in the URL. Fixes #9. --- contrib/sharedhost/replacement_test.go | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 contrib/sharedhost/replacement_test.go (limited to 'contrib/sharedhost/replacement_test.go') diff --git a/contrib/sharedhost/replacement_test.go b/contrib/sharedhost/replacement_test.go new file mode 100644 index 0000000..cab80bb --- /dev/null +++ b/contrib/sharedhost/replacement_test.go @@ -0,0 +1,57 @@ +package sharedhost_test + +import ( + "context" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" + + "tildegit.org/tjp/gus" + "tildegit.org/tjp/gus/contrib/sharedhost" +) + +func TestReplaceTilde(t *testing.T) { + tests := []struct { + replacement string + inputURL string + replacedPath string + }{ + { + replacement: "users/", + inputURL: "gemini://domain.com/~username/foo/bar", + replacedPath: "/users/username/foo/bar", + }, + { + replacement: "people-", + inputURL: "gemini://domain.com/non/match", + replacedPath: "/non/match", + }, + { + replacement: "people-", + inputURL: "gemini://domain.com/~someone/dir/file", + replacedPath: "/people-someone/dir/file", + }, + } + + for _, test := range tests { + t.Run(test.inputURL, func(t *testing.T) { + u, err := url.Parse(test.inputURL) + assert.Nil(t, err) + + originalPath := u.Path + + replacer := sharedhost.ReplaceTilde(test.replacement) + request := &gus.Request{URL: u} + handler := replacer(func(_ context.Context, request *gus.Request) *gus.Response { + assert.Equal(t, test.replacedPath, request.Path) + return &gus.Response{} + }) + + handler(context.Background(), request) + + // original request was unmodified + assert.Equal(t, originalPath, request.Path) + }) + } +} -- cgit v1.2.3