131 lines
2.7 KiB
Go
131 lines
2.7 KiB
Go
|
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 TestDirectoryDefault(t *testing.T) {
|
||
|
handler := fs.DirectoryDefault(os.DirFS("testdata"), "index.gmi")
|
||
|
|
||
|
tests := []struct {
|
||
|
url string
|
||
|
status gus.Status
|
||
|
meta string
|
||
|
body string
|
||
|
}{
|
||
|
{
|
||
|
url: "gemini://localhost/d",
|
||
|
status: gemini.StatusTemporaryRedirect,
|
||
|
meta: "gemini://localhost/d/",
|
||
|
},
|
||
|
{
|
||
|
url: "gemini://localhost/d/",
|
||
|
status: gemini.StatusSuccess,
|
||
|
meta: "text/gemini",
|
||
|
body: "# This is d\n",
|
||
|
},
|
||
|
{
|
||
|
url: "gemini://localhost/a/",
|
||
|
status: gemini.StatusNotFound,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
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))
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestDirectoryListing(t *testing.T) {
|
||
|
handler := fs.DirectoryListing(os.DirFS("testdata"), nil)
|
||
|
|
||
|
tests := []struct {
|
||
|
url string
|
||
|
status gus.Status
|
||
|
meta string
|
||
|
body string
|
||
|
}{
|
||
|
{
|
||
|
url: "gemini://localhost/",
|
||
|
status: gemini.StatusSuccess,
|
||
|
meta: "text/gemini",
|
||
|
body: "# (root)\n\n=> a/\n=> d/\n=> ../\n",
|
||
|
},
|
||
|
{
|
||
|
url: "gemini://localhost/d",
|
||
|
status: gemini.StatusTemporaryRedirect,
|
||
|
meta: "gemini://localhost/d/",
|
||
|
},
|
||
|
{
|
||
|
url: "gemini://localhost/d/",
|
||
|
status: gemini.StatusSuccess,
|
||
|
meta: "text/gemini",
|
||
|
body: "# d\n\n=> index.gmi\n=> ../\n",
|
||
|
},
|
||
|
{
|
||
|
url: "gemini://localhost/a/",
|
||
|
status: gemini.StatusSuccess,
|
||
|
meta: "text/gemini",
|
||
|
body: "# a\n\n=> b\n=> c.html\n=> ../\n",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
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))
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|