diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-01-26 16:22:58 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-01-26 16:22:58 -0700 |
commit | a27b879accb191b6a6c6e76a6251ed751967f73a (patch) | |
tree | d7c5f337ca9cb4143997665fc616759fbf5e2d3f /contrib/fs/file_test.go | |
parent | 32e45e3557e49cc868aa4437ef0aa56ab6470be8 (diff) |
test coverage and resulting bugfixes
Diffstat (limited to 'contrib/fs/file_test.go')
-rw-r--r-- | contrib/fs/file_test.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/contrib/fs/file_test.go b/contrib/fs/file_test.go new file mode 100644 index 0000000..4f371c7 --- /dev/null +++ b/contrib/fs/file_test.go @@ -0,0 +1,80 @@ +package fs_test + +import ( + "context" + "io" + "net/url" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "tildegit.org/tjp/gus" + "tildegit.org/tjp/gus/contrib/fs" + "tildegit.org/tjp/gus/gemini" +) + +func TestFileHandler(t *testing.T) { + handler := fs.FileHandler(os.DirFS("testdata")) + + tests := []struct { + url string + status gus.Status + meta string + body string + }{ + { + url: "gemini://localhost/d", + status: gemini.StatusNotFound, + }, + { + url: "gemini://localhost/d/", + status: gemini.StatusNotFound, + }, + { + url: "gemini://localhost/d/index.gmi", + status: gemini.StatusSuccess, + meta: "text/gemini", + body: "# This is d\n", + }, + { + url: "gemini://localhost/a/b", + status: gemini.StatusSuccess, + meta: "application/octet-stream", + body: "this is file b\n", + }, + { + url: "gemini://localhost/a/c.html", + status: gemini.StatusSuccess, + meta: "text/html; charset=utf-8", + body: "", + }, + } + + for _, test := range tests { + t.Run(test.url, func(t *testing.T) { + u, err := url.Parse(test.url) + require.Nil(t, err) + + request := &gus.Request{URL: u} + response := handler(context.Background(), request) + + if response == nil { + assert.Equal(t, test.status, gemini.StatusNotFound) + return + } else { + assert.Equal(t, test.status, response.Status) + } + + if test.meta != "" { + assert.Equal(t, test.meta, response.Meta) + } + if test.body != "" { + body, err := io.ReadAll(response.Body) + require.Nil(t, err) + assert.Equal(t, test.body, string(body)) + } + }) + } +} |