2018-10-28 22:08:20 +08:00
|
|
|
package serve
|
|
|
|
|
|
|
|
import (
|
2021-12-09 00:14:45 +08:00
|
|
|
"context"
|
2018-10-28 22:08:20 +08:00
|
|
|
"errors"
|
2018-12-23 08:16:50 +08:00
|
|
|
"html/template"
|
2022-11-23 17:44:53 +08:00
|
|
|
"io"
|
2018-10-28 22:08:20 +08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2020-05-01 02:24:11 +08:00
|
|
|
"time"
|
2018-10-28 22:08:20 +08:00
|
|
|
|
2023-08-02 12:52:19 +08:00
|
|
|
libhttp "github.com/rclone/rclone/lib/http"
|
2018-10-28 22:08:20 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-23 08:16:50 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-28 22:08:20 +08:00
|
|
|
)
|
|
|
|
|
2018-12-23 08:16:50 +08:00
|
|
|
func GetTemplate(t *testing.T) *template.Template {
|
2023-08-02 12:52:19 +08:00
|
|
|
htmlTemplate, err := libhttp.GetTemplate("../../../cmd/serve/http/testdata/golden/testindex.html")
|
2018-12-23 08:16:50 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
return htmlTemplate
|
|
|
|
}
|
|
|
|
|
2018-10-28 22:08:20 +08:00
|
|
|
func TestNewDirectory(t *testing.T) {
|
2018-12-23 08:16:50 +08:00
|
|
|
d := NewDirectory("z", GetTemplate(t))
|
2018-10-28 22:08:20 +08:00
|
|
|
assert.Equal(t, "z", d.DirRemote)
|
|
|
|
assert.Equal(t, "Directory listing of /z", d.Title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetQuery(t *testing.T) {
|
2018-12-23 08:16:50 +08:00
|
|
|
d := NewDirectory("z", GetTemplate(t))
|
2018-10-28 22:08:20 +08:00
|
|
|
assert.Equal(t, "", d.Query)
|
|
|
|
d.SetQuery(url.Values{"potato": []string{"42"}})
|
|
|
|
assert.Equal(t, "?potato=42", d.Query)
|
|
|
|
d.SetQuery(url.Values{})
|
|
|
|
assert.Equal(t, "", d.Query)
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:24:11 +08:00
|
|
|
func TestAddHTMLEntry(t *testing.T) {
|
|
|
|
var modtime = time.Now()
|
|
|
|
var d = NewDirectory("z", GetTemplate(t))
|
|
|
|
d.AddHTMLEntry("", true, 0, modtime)
|
|
|
|
d.AddHTMLEntry("dir", true, 0, modtime)
|
|
|
|
d.AddHTMLEntry("a/b/c/d.txt", false, 64, modtime)
|
|
|
|
d.AddHTMLEntry("a/b/c/colon:colon.txt", false, 64, modtime)
|
|
|
|
d.AddHTMLEntry("\"quotes\".txt", false, 64, modtime)
|
|
|
|
assert.Equal(t, []DirEntry{
|
|
|
|
{remote: "", URL: "/", Leaf: "/", IsDir: true, Size: 0, ModTime: modtime},
|
|
|
|
{remote: "dir", URL: "dir/", Leaf: "dir/", IsDir: true, Size: 0, ModTime: modtime},
|
|
|
|
{remote: "a/b/c/d.txt", URL: "d.txt", Leaf: "d.txt", IsDir: false, Size: 64, ModTime: modtime},
|
|
|
|
{remote: "a/b/c/colon:colon.txt", URL: "./colon:colon.txt", Leaf: "colon:colon.txt", IsDir: false, Size: 64, ModTime: modtime},
|
|
|
|
{remote: "\"quotes\".txt", URL: "%22quotes%22.txt", Leaf: "\"quotes\".txt", Size: 64, IsDir: false, ModTime: modtime},
|
|
|
|
}, d.Entries)
|
|
|
|
|
|
|
|
// Now test with a query parameter
|
|
|
|
d = NewDirectory("z", GetTemplate(t)).SetQuery(url.Values{"potato": []string{"42"}})
|
|
|
|
d.AddHTMLEntry("file", false, 64, modtime)
|
|
|
|
d.AddHTMLEntry("dir", true, 0, modtime)
|
|
|
|
assert.Equal(t, []DirEntry{
|
|
|
|
{remote: "file", URL: "file?potato=42", Leaf: "file", IsDir: false, Size: 64, ModTime: modtime},
|
|
|
|
{remote: "dir", URL: "dir/?potato=42", Leaf: "dir/", IsDir: true, Size: 0, ModTime: modtime},
|
|
|
|
}, d.Entries)
|
|
|
|
}
|
|
|
|
|
2018-10-28 22:08:20 +08:00
|
|
|
func TestAddEntry(t *testing.T) {
|
2018-12-23 08:16:50 +08:00
|
|
|
var d = NewDirectory("z", GetTemplate(t))
|
2018-10-28 22:08:20 +08:00
|
|
|
d.AddEntry("", true)
|
|
|
|
d.AddEntry("dir", true)
|
|
|
|
d.AddEntry("a/b/c/d.txt", false)
|
|
|
|
d.AddEntry("a/b/c/colon:colon.txt", false)
|
|
|
|
d.AddEntry("\"quotes\".txt", false)
|
|
|
|
assert.Equal(t, []DirEntry{
|
|
|
|
{remote: "", URL: "/", Leaf: "/"},
|
|
|
|
{remote: "dir", URL: "dir/", Leaf: "dir/"},
|
|
|
|
{remote: "a/b/c/d.txt", URL: "d.txt", Leaf: "d.txt"},
|
|
|
|
{remote: "a/b/c/colon:colon.txt", URL: "./colon:colon.txt", Leaf: "colon:colon.txt"},
|
|
|
|
{remote: "\"quotes\".txt", URL: "%22quotes%22.txt", Leaf: "\"quotes\".txt"},
|
|
|
|
}, d.Entries)
|
|
|
|
|
|
|
|
// Now test with a query parameter
|
2018-12-23 08:16:50 +08:00
|
|
|
d = NewDirectory("z", GetTemplate(t)).SetQuery(url.Values{"potato": []string{"42"}})
|
2018-10-28 22:08:20 +08:00
|
|
|
d.AddEntry("file", false)
|
|
|
|
d.AddEntry("dir", true)
|
|
|
|
assert.Equal(t, []DirEntry{
|
|
|
|
{remote: "file", URL: "file?potato=42", Leaf: "file"},
|
|
|
|
{remote: "dir", URL: "dir/?potato=42", Leaf: "dir/"},
|
|
|
|
}, d.Entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
2021-12-09 00:14:45 +08:00
|
|
|
ctx := context.Background()
|
2018-10-28 22:08:20 +08:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
err := errors.New("help")
|
2021-12-09 00:14:45 +08:00
|
|
|
Error(ctx, "potato", w, "sausage", err)
|
2018-10-28 22:08:20 +08:00
|
|
|
resp := w.Result()
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
|
2022-11-23 17:44:53 +08:00
|
|
|
body, _ := io.ReadAll(resp.Body)
|
2018-10-28 22:08:20 +08:00
|
|
|
assert.Equal(t, "sausage.\n", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServe(t *testing.T) {
|
2018-12-23 08:16:50 +08:00
|
|
|
d := NewDirectory("aDirectory", GetTemplate(t))
|
2018-10-28 22:08:20 +08:00
|
|
|
d.AddEntry("file", false)
|
|
|
|
d.AddEntry("dir", true)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "http://example.com/aDirectory/", nil)
|
|
|
|
d.Serve(w, r)
|
|
|
|
resp := w.Result()
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
2022-11-23 17:44:53 +08:00
|
|
|
body, _ := io.ReadAll(resp.Body)
|
2018-10-28 22:08:20 +08:00
|
|
|
assert.Equal(t, `<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Directory listing of /aDirectory</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Directory listing of /aDirectory</h1>
|
|
|
|
<a href="file">file</a><br />
|
|
|
|
<a href="dir/">dir/</a><br />
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`, string(body))
|
|
|
|
}
|