test coverage and resulting bugfixes
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
32e45e3557
commit
a27b879acc
|
@ -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))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,7 +46,7 @@ func mediaType(filePath string) string {
|
||||||
if dotIdx == -1 {
|
if dotIdx == -1 {
|
||||||
return "application/octet-stream"
|
return "application/octet-stream"
|
||||||
}
|
}
|
||||||
ext := filePath[slashIdx+dotIdx:]
|
ext := filePath[slashIdx+1+dotIdx:]
|
||||||
|
|
||||||
mtype := mime.TypeByExtension(ext)
|
mtype := mime.TypeByExtension(ext)
|
||||||
if mtype == "" {
|
if mtype == "" {
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
this is file b
|
|
@ -0,0 +1 @@
|
||||||
|
# This is d
|
|
@ -0,0 +1,15 @@
|
||||||
|
package internal_test
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestAddAllTemplates(t *testing.T) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddHTMLTemplates(t *testing.T) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateLinks(t *testing.T) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRenderItems(t *testing.T) {
|
||||||
|
}
|
|
@ -48,6 +48,7 @@ func (lr *loggedResponseBody) log() {
|
||||||
|
|
||||||
func (lr *loggedResponseBody) Read(b []byte) (int, error) {
|
func (lr *loggedResponseBody) Read(b []byte) (int, error) {
|
||||||
if lr.body == nil {
|
if lr.body == nil {
|
||||||
|
lr.log()
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package logging_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"tildegit.org/tjp/gus"
|
||||||
|
"tildegit.org/tjp/gus/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLogRequests(t *testing.T) {
|
||||||
|
logger := logRecorder{}
|
||||||
|
handler := logging.LogRequests(&logger)(func(_ context.Context, _ *gus.Request) *gus.Response {
|
||||||
|
return &gus.Response{}
|
||||||
|
})
|
||||||
|
|
||||||
|
response := handler(context.Background(), &gus.Request{})
|
||||||
|
_, err := io.ReadAll(response.Body)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, 1, len(logger))
|
||||||
|
|
||||||
|
keyvals := map[string]any{}
|
||||||
|
for i := 0; i < len(logger[0])-1; i += 2 {
|
||||||
|
keyvals[logger[0][i].(string)] = logger[0][i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if assert.Contains(t, keyvals, "msg") {
|
||||||
|
assert.Equal(t, keyvals["msg"], "request")
|
||||||
|
}
|
||||||
|
assert.Contains(t, keyvals, "ts")
|
||||||
|
assert.Contains(t, keyvals, "dur")
|
||||||
|
assert.Contains(t, keyvals, "url")
|
||||||
|
assert.Contains(t, keyvals, "status")
|
||||||
|
assert.Contains(t, keyvals, "bodylen")
|
||||||
|
}
|
||||||
|
|
||||||
|
type logRecorder [][]any
|
||||||
|
|
||||||
|
func (lr *logRecorder) Log(keyvals ...any) error {
|
||||||
|
*lr = append(*lr, keyvals)
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue