summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'contrib')
-rw-r--r--contrib/fs/dir_test.go130
-rw-r--r--contrib/fs/file.go2
-rw-r--r--contrib/fs/file_test.go80
-rw-r--r--contrib/fs/testdata/a/b1
-rw-r--r--contrib/fs/testdata/a/c.html0
-rw-r--r--contrib/fs/testdata/d/index.gmi1
6 files changed, 213 insertions, 1 deletions
diff --git a/contrib/fs/dir_test.go b/contrib/fs/dir_test.go
new file mode 100644
index 0000000..c7492ff
--- /dev/null
+++ b/contrib/fs/dir_test.go
@@ -0,0 +1,130 @@
+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))
+ }
+ })
+ }
+}
diff --git a/contrib/fs/file.go b/contrib/fs/file.go
index 8cb1aeb..71428ed 100644
--- a/contrib/fs/file.go
+++ b/contrib/fs/file.go
@@ -46,7 +46,7 @@ func mediaType(filePath string) string {
if dotIdx == -1 {
return "application/octet-stream"
}
- ext := filePath[slashIdx+dotIdx:]
+ ext := filePath[slashIdx+1+dotIdx:]
mtype := mime.TypeByExtension(ext)
if mtype == "" {
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))
+ }
+ })
+ }
+}
diff --git a/contrib/fs/testdata/a/b b/contrib/fs/testdata/a/b
new file mode 100644
index 0000000..77486f7
--- /dev/null
+++ b/contrib/fs/testdata/a/b
@@ -0,0 +1 @@
+this is file b
diff --git a/contrib/fs/testdata/a/c.html b/contrib/fs/testdata/a/c.html
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/contrib/fs/testdata/a/c.html
diff --git a/contrib/fs/testdata/d/index.gmi b/contrib/fs/testdata/d/index.gmi
new file mode 100644
index 0000000..faf50cf
--- /dev/null
+++ b/contrib/fs/testdata/d/index.gmi
@@ -0,0 +1 @@
+# This is d