2017-06-19 18:36:14 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2017-06-19 18:36:14 +02:00
|
|
|
"fmt"
|
2022-08-20 16:38:02 +02:00
|
|
|
"io"
|
2017-06-19 18:36:14 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-01-23 23:39:05 +01:00
|
|
|
"path"
|
2017-06-19 18:36:14 +02:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2022-01-23 23:39:05 +01:00
|
|
|
"strconv"
|
2019-08-12 16:29:35 +02:00
|
|
|
"strings"
|
2017-06-19 18:36:14 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-03-10 16:40:34 +01:00
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
|
|
|
"github.com/rclone/rclone/lib/rest"
|
2017-06-19 18:36:14 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-24 00:17:59 +01:00
|
|
|
remoteName = "TestHTTP"
|
|
|
|
testPath = "test"
|
|
|
|
filesPath = filepath.Join(testPath, "files")
|
|
|
|
headers = []string{"X-Potato", "sausage", "X-Rhubarb", "cucumber"}
|
|
|
|
lineEndSize = 1
|
2017-06-19 18:36:14 +02:00
|
|
|
)
|
|
|
|
|
2022-12-08 13:43:53 +01:00
|
|
|
// prepareServer prepares the test server and shuts it down automatically
|
|
|
|
// when the test completes.
|
|
|
|
func prepareServer(t *testing.T) configmap.Simple {
|
2017-06-19 18:36:14 +02:00
|
|
|
// file server for test/files
|
|
|
|
fileServer := http.FileServer(http.Dir(filesPath))
|
|
|
|
|
2022-01-24 00:17:59 +01:00
|
|
|
// verify the file path is correct, and also check which line endings
|
|
|
|
// are used to get sizes right ("\n" except on Windows, but even there
|
|
|
|
// we may have "\n" or "\r\n" depending on git crlf setting)
|
2022-08-20 16:38:02 +02:00
|
|
|
fileList, err := os.ReadDir(filesPath)
|
2022-01-24 00:17:59 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, len(fileList), 0)
|
|
|
|
for _, file := range fileList {
|
|
|
|
if !file.IsDir() {
|
2022-08-20 16:38:02 +02:00
|
|
|
data, _ := os.ReadFile(filepath.Join(filesPath, file.Name()))
|
2022-01-24 00:17:59 +01:00
|
|
|
if strings.HasSuffix(string(data), "\r\n") {
|
|
|
|
lineEndSize = 2
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:29:35 +02:00
|
|
|
// test the headers are there then pass on to fileServer
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
what := fmt.Sprintf("%s %s: Header ", r.Method, r.URL.Path)
|
|
|
|
assert.Equal(t, headers[1], r.Header.Get(headers[0]), what+headers[0])
|
|
|
|
assert.Equal(t, headers[3], r.Header.Get(headers[2]), what+headers[2])
|
|
|
|
fileServer.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
2017-06-19 18:36:14 +02:00
|
|
|
// Make the test server
|
2019-08-12 16:29:35 +02:00
|
|
|
ts := httptest.NewServer(handler)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
|
|
|
// Configure the remote
|
2021-04-26 23:37:49 +02:00
|
|
|
configfile.Install()
|
2017-06-19 18:36:14 +02:00
|
|
|
// fs.Config.LogLevel = fs.LogLevelDebug
|
|
|
|
// fs.Config.DumpHeaders = true
|
|
|
|
// fs.Config.DumpBodies = true
|
2018-05-14 19:06:57 +02:00
|
|
|
// config.FileSet(remoteName, "type", "http")
|
|
|
|
// config.FileSet(remoteName, "url", ts.URL)
|
|
|
|
|
|
|
|
m := configmap.Simple{
|
2019-08-12 16:29:35 +02:00
|
|
|
"type": "http",
|
|
|
|
"url": ts.URL,
|
|
|
|
"headers": strings.Join(headers, ","),
|
2018-05-14 19:06:57 +02:00
|
|
|
}
|
2022-12-08 13:43:53 +01:00
|
|
|
t.Cleanup(ts.Close)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2022-12-08 13:43:53 +01:00
|
|
|
return m
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
2022-12-08 13:43:53 +01:00
|
|
|
// prepare prepares the test server and shuts it down automatically
|
|
|
|
// when the test completes.
|
|
|
|
func prepare(t *testing.T) fs.Fs {
|
|
|
|
m := prepareServer(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
|
|
|
// Instantiate it
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := NewFs(context.Background(), remoteName, "", m)
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-08 13:43:53 +01:00
|
|
|
return f
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:58:47 +01:00
|
|
|
func testListRoot(t *testing.T, f fs.Fs, noSlash bool) {
|
2019-06-17 10:34:30 +02:00
|
|
|
entries, err := f.List(context.Background(), "")
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sort.Sort(entries)
|
|
|
|
|
|
|
|
require.Equal(t, 4, len(entries))
|
|
|
|
|
|
|
|
e := entries[0]
|
|
|
|
assert.Equal(t, "four", e.Remote())
|
2017-06-30 14:37:29 +02:00
|
|
|
assert.Equal(t, int64(-1), e.Size())
|
|
|
|
_, ok := e.(fs.Directory)
|
2017-06-19 18:36:14 +02:00
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
e = entries[1]
|
2017-08-02 14:19:36 +02:00
|
|
|
assert.Equal(t, "one%.txt", e.Remote())
|
2022-01-24 00:17:59 +01:00
|
|
|
assert.Equal(t, int64(5+lineEndSize), e.Size())
|
2017-06-19 18:36:14 +02:00
|
|
|
_, ok = e.(*Object)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
e = entries[2]
|
|
|
|
assert.Equal(t, "three", e.Remote())
|
2017-06-30 14:37:29 +02:00
|
|
|
assert.Equal(t, int64(-1), e.Size())
|
|
|
|
_, ok = e.(fs.Directory)
|
2017-06-19 18:36:14 +02:00
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
e = entries[3]
|
|
|
|
assert.Equal(t, "two.html", e.Remote())
|
2019-02-08 14:58:47 +01:00
|
|
|
if noSlash {
|
|
|
|
assert.Equal(t, int64(-1), e.Size())
|
|
|
|
_, ok = e.(fs.Directory)
|
|
|
|
assert.True(t, ok)
|
|
|
|
} else {
|
2022-01-24 00:17:59 +01:00
|
|
|
assert.Equal(t, int64(40+lineEndSize), e.Size())
|
2019-02-08 14:58:47 +01:00
|
|
|
_, ok = e.(*Object)
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListRoot(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
f := prepare(t)
|
2019-02-08 14:58:47 +01:00
|
|
|
testListRoot(t, f, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListRootNoSlash(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
f := prepare(t)
|
2019-02-08 14:58:47 +01:00
|
|
|
f.(*Fs).opt.NoSlash = true
|
|
|
|
|
|
|
|
testListRoot(t, f, true)
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestListSubDir(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
f := prepare(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
entries, err := f.List(context.Background(), "three")
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sort.Sort(entries)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(entries))
|
|
|
|
|
|
|
|
e := entries[0]
|
|
|
|
assert.Equal(t, "three/underthree.txt", e.Remote())
|
2022-01-24 00:17:59 +01:00
|
|
|
assert.Equal(t, int64(8+lineEndSize), e.Size())
|
2017-06-19 18:36:14 +02:00
|
|
|
_, ok := e.(*Object)
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewObject(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
f := prepare(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
o, err := f.NewObject(context.Background(), "four/under four.txt")
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-08-01 00:15:31 +02:00
|
|
|
assert.Equal(t, "four/under four.txt", o.Remote())
|
2022-01-24 00:17:59 +01:00
|
|
|
assert.Equal(t, int64(8+lineEndSize), o.Size())
|
2017-06-19 18:36:14 +02:00
|
|
|
_, ok := o.(*Object)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
// Test the time is correct on the object
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
tObj := o.ModTime(context.Background())
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2017-08-01 00:15:31 +02:00
|
|
|
fi, err := os.Stat(filepath.Join(filesPath, "four", "under four.txt"))
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
tFile := fi.ModTime()
|
|
|
|
|
2020-04-21 16:46:01 +02:00
|
|
|
fstest.AssertTimeEqualWithPrecision(t, o.Remote(), tFile, tObj, time.Second)
|
2018-12-04 18:40:44 +01:00
|
|
|
|
|
|
|
// check object not found
|
2019-06-17 10:34:30 +02:00
|
|
|
o, err = f.NewObject(context.Background(), "not found.txt")
|
2018-12-04 18:40:44 +01:00
|
|
|
assert.Nil(t, o)
|
|
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpen(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
m := prepareServer(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2022-03-20 11:12:48 +01:00
|
|
|
for _, head := range []bool{false, true} {
|
|
|
|
if !head {
|
|
|
|
m.Set("no_head", "true")
|
|
|
|
}
|
|
|
|
f, err := NewFs(context.Background(), remoteName, "", m)
|
|
|
|
require.NoError(t, err)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2022-03-20 11:12:48 +01:00
|
|
|
for _, rangeRead := range []bool{false, true} {
|
|
|
|
o, err := f.NewObject(context.Background(), "four/under four.txt")
|
|
|
|
require.NoError(t, err)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2022-03-20 11:12:48 +01:00
|
|
|
if !head {
|
|
|
|
// Test mod time is still indeterminate
|
|
|
|
tObj := o.ModTime(context.Background())
|
|
|
|
assert.Equal(t, time.Duration(0), time.Unix(0, 0).Sub(tObj))
|
|
|
|
|
|
|
|
// Test file size is still indeterminate
|
|
|
|
assert.Equal(t, int64(-1), o.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
if !rangeRead {
|
|
|
|
// Test normal read
|
|
|
|
fd, err := o.Open(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
data, err = io.ReadAll(fd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, fd.Close())
|
|
|
|
if lineEndSize == 2 {
|
|
|
|
assert.Equal(t, "beetroot\r\n", string(data))
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "beetroot\n", string(data))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Test with range request
|
|
|
|
fd, err := o.Open(context.Background(), &fs.RangeOption{Start: 1, End: 5})
|
|
|
|
require.NoError(t, err)
|
|
|
|
data, err = io.ReadAll(fd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, fd.Close())
|
|
|
|
assert.Equal(t, "eetro", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(filepath.Join(filesPath, "four", "under four.txt"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
tFile := fi.ModTime()
|
|
|
|
|
|
|
|
// Test the time is always correct on the object after file open
|
|
|
|
tObj := o.ModTime(context.Background())
|
|
|
|
fstest.AssertTimeEqualWithPrecision(t, o.Remote(), tFile, tObj, time.Second)
|
|
|
|
|
|
|
|
if !rangeRead {
|
|
|
|
// Test the file size
|
|
|
|
assert.Equal(t, int64(len(data)), o.Size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMimeType(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
f := prepare(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
o, err := f.NewObject(context.Background(), "four/under four.txt")
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
do, ok := o.(fs.MimeTyper)
|
|
|
|
require.True(t, ok)
|
2019-06-17 10:34:30 +02:00
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", do.MimeType(context.Background()))
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsAFileRoot(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
m := prepareServer(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := NewFs(context.Background(), remoteName, "one%.txt", m)
|
2017-06-19 18:36:14 +02:00
|
|
|
assert.Equal(t, err, fs.ErrorIsFile)
|
|
|
|
|
2019-02-08 14:58:47 +01:00
|
|
|
testListRoot(t, f, false)
|
2017-06-19 18:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsAFileSubDir(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
m := prepareServer(t)
|
2017-06-19 18:36:14 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := NewFs(context.Background(), remoteName, "three/underthree.txt", m)
|
2017-06-19 18:36:14 +02:00
|
|
|
assert.Equal(t, err, fs.ErrorIsFile)
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
entries, err := f.List(context.Background(), "")
|
2017-06-19 18:36:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sort.Sort(entries)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(entries))
|
|
|
|
|
|
|
|
e := entries[0]
|
|
|
|
assert.Equal(t, "underthree.txt", e.Remote())
|
2022-01-24 00:17:59 +01:00
|
|
|
assert.Equal(t, int64(8+lineEndSize), e.Size())
|
2017-06-19 18:36:14 +02:00
|
|
|
_, ok := e.(*Object)
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseName(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
2018-02-14 12:26:37 +01:00
|
|
|
base string
|
|
|
|
val string
|
|
|
|
wantErr error
|
|
|
|
want string
|
2017-06-19 18:36:14 +02:00
|
|
|
}{
|
2018-02-14 12:26:37 +01:00
|
|
|
{"http://example.com/", "potato", nil, "potato"},
|
|
|
|
{"http://example.com/dir/", "potato", nil, "potato"},
|
|
|
|
{"http://example.com/dir/", "potato?download=true", errFoundQuestionMark, ""},
|
|
|
|
{"http://example.com/dir/", "../dir/potato", nil, "potato"},
|
|
|
|
{"http://example.com/dir/", "..", errNotUnderRoot, ""},
|
|
|
|
{"http://example.com/dir/", "http://example.com/", errNotUnderRoot, ""},
|
|
|
|
{"http://example.com/dir/", "http://example.com/dir/", errNameIsEmpty, ""},
|
|
|
|
{"http://example.com/dir/", "http://example.com/dir/potato", nil, "potato"},
|
|
|
|
{"http://example.com/dir/", "https://example.com/dir/potato", errSchemeMismatch, ""},
|
|
|
|
{"http://example.com/dir/", "http://notexample.com/dir/potato", errHostMismatch, ""},
|
|
|
|
{"http://example.com/dir/", "/dir/", errNameIsEmpty, ""},
|
|
|
|
{"http://example.com/dir/", "/dir/potato", nil, "potato"},
|
|
|
|
{"http://example.com/dir/", "subdir/potato", errNameContainsSlash, ""},
|
|
|
|
{"http://example.com/dir/", "With percent %25.txt", nil, "With percent %.txt"},
|
|
|
|
{"http://example.com/dir/", "With colon :", errURLJoinFailed, ""},
|
|
|
|
{"http://example.com/dir/", rest.URLPathEscape("With colon :"), nil, "With colon :"},
|
|
|
|
{"http://example.com/Dungeons%20%26%20Dragons/", "/Dungeons%20&%20Dragons/D%26D%20Basic%20%28Holmes%2C%20B%2C%20X%2C%20BECMI%29/", nil, "D&D Basic (Holmes, B, X, BECMI)/"},
|
2017-06-19 18:36:14 +02:00
|
|
|
} {
|
|
|
|
u, err := url.Parse(test.base)
|
|
|
|
require.NoError(t, err)
|
2018-02-14 12:26:37 +01:00
|
|
|
got, gotErr := parseName(u, test.val)
|
2017-06-19 18:36:14 +02:00
|
|
|
what := fmt.Sprintf("test %d base=%q, val=%q", i, test.base, test.val)
|
2018-02-14 12:26:37 +01:00
|
|
|
assert.Equal(t, test.wantErr, gotErr, what)
|
2017-06-19 18:36:14 +02:00
|
|
|
assert.Equal(t, test.want, got, what)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load HTML from the file given and parse it, checking it against the entries passed in
|
|
|
|
func parseHTML(t *testing.T, name string, base string, want []string) {
|
|
|
|
in, err := os.Open(filepath.Join(testPath, "index_files", name))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, in.Close())
|
|
|
|
}()
|
|
|
|
if base == "" {
|
|
|
|
base = "http://example.com/"
|
|
|
|
}
|
|
|
|
u, err := url.Parse(base)
|
|
|
|
require.NoError(t, err)
|
|
|
|
entries, err := parse(u, in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, want, entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseEmpty(t *testing.T) {
|
|
|
|
parseHTML(t, "empty.html", "", []string(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseApache(t *testing.T) {
|
|
|
|
parseHTML(t, "apache.html", "http://example.com/nick/pub/", []string{
|
|
|
|
"SWIG-embed.tar.gz",
|
|
|
|
"avi2dvd.pl",
|
|
|
|
"cambert.exe",
|
|
|
|
"cambert.gz",
|
|
|
|
"fedora_demo.gz",
|
|
|
|
"gchq-challenge/",
|
|
|
|
"mandelterm/",
|
|
|
|
"pgp-key.txt",
|
|
|
|
"pymath/",
|
|
|
|
"rclone",
|
|
|
|
"readdir.exe",
|
|
|
|
"rush_hour_solver_cut_down.py",
|
|
|
|
"snake-puzzle/",
|
|
|
|
"stressdisk/",
|
|
|
|
"timer-test",
|
|
|
|
"words-to-regexp.pl",
|
2017-08-02 14:19:36 +02:00
|
|
|
"Now 100% better.mp3",
|
|
|
|
"Now better.mp3",
|
2017-06-19 18:36:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseMemstore(t *testing.T) {
|
|
|
|
parseHTML(t, "memstore.html", "", []string{
|
|
|
|
"test/",
|
|
|
|
"v1.35/",
|
|
|
|
"v1.36-01-g503cd84/",
|
|
|
|
"rclone-beta-latest-freebsd-386.zip",
|
|
|
|
"rclone-beta-latest-freebsd-amd64.zip",
|
|
|
|
"rclone-beta-latest-windows-amd64.zip",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseNginx(t *testing.T) {
|
|
|
|
parseHTML(t, "nginx.html", "", []string{
|
|
|
|
"deltas/",
|
|
|
|
"objects/",
|
|
|
|
"refs/",
|
|
|
|
"state/",
|
|
|
|
"config",
|
|
|
|
"summary",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCaddy(t *testing.T) {
|
|
|
|
parseHTML(t, "caddy.html", "", []string{
|
|
|
|
"mimetype.zip",
|
|
|
|
"rclone-delete-empty-dirs.py",
|
|
|
|
"rclone-show-empty-dirs.py",
|
|
|
|
"stat-windows-386.zip",
|
|
|
|
"v1.36-155-gcf29ee8b-team-driveβ/",
|
|
|
|
"v1.36-156-gca76b3fb-team-driveβ/",
|
|
|
|
"v1.36-156-ge1f0e0f5-team-driveβ/",
|
|
|
|
"v1.36-22-g06ea13a-ssh-agentβ/",
|
|
|
|
})
|
|
|
|
}
|
2022-01-23 23:39:05 +01:00
|
|
|
|
|
|
|
func TestFsNoSlashRoots(t *testing.T) {
|
|
|
|
// Test Fs with roots that does not end with '/', the logic that
|
|
|
|
// decides if url is to be considered a file or directory, based
|
|
|
|
// on result from a HEAD request.
|
|
|
|
|
|
|
|
// Handler for faking HEAD responses with different status codes
|
|
|
|
headCount := 0
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
headCount++
|
|
|
|
responseCode, err := strconv.Atoi(path.Base(r.URL.String()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
if strings.HasPrefix(r.URL.String(), "/redirect/") {
|
|
|
|
var redir string
|
|
|
|
if strings.HasPrefix(r.URL.String(), "/redirect/file/") {
|
|
|
|
redir = "/redirected"
|
|
|
|
} else if strings.HasPrefix(r.URL.String(), "/redirect/dir/") {
|
|
|
|
redir = "/redirected/"
|
|
|
|
} else {
|
|
|
|
require.Fail(t, "Redirect test requests must start with '/redirect/file/' or '/redirect/dir/'")
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, redir, responseCode)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(responseCode), responseCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make the test server
|
|
|
|
ts := httptest.NewServer(handler)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
// Configure the remote
|
|
|
|
configfile.Install()
|
|
|
|
m := configmap.Simple{
|
|
|
|
"type": "http",
|
|
|
|
"url": ts.URL,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
for i, test := range []struct {
|
|
|
|
root string
|
|
|
|
isFile bool
|
|
|
|
}{
|
|
|
|
// 2xx success
|
|
|
|
{"parent/200", true},
|
|
|
|
{"parent/204", true},
|
|
|
|
|
|
|
|
// 3xx redirection Redirect status 301, 302, 303, 307, 308
|
|
|
|
{"redirect/file/301", true}, // Request is redirected to "/redirected"
|
|
|
|
{"redirect/dir/301", false}, // Request is redirected to "/redirected/"
|
|
|
|
{"redirect/file/302", true}, // Request is redirected to "/redirected"
|
|
|
|
{"redirect/dir/302", false}, // Request is redirected to "/redirected/"
|
|
|
|
{"redirect/file/303", true}, // Request is redirected to "/redirected"
|
|
|
|
{"redirect/dir/303", false}, // Request is redirected to "/redirected/"
|
|
|
|
|
|
|
|
{"redirect/file/304", true}, // Not really a redirect, handled like 4xx errors (below)
|
|
|
|
{"redirect/file/305", true}, // Not really a redirect, handled like 4xx errors (below)
|
|
|
|
{"redirect/file/306", true}, // Not really a redirect, handled like 4xx errors (below)
|
|
|
|
|
|
|
|
{"redirect/file/307", true}, // Request is redirected to "/redirected"
|
|
|
|
{"redirect/dir/307", false}, // Request is redirected to "/redirected/"
|
|
|
|
{"redirect/file/308", true}, // Request is redirected to "/redirected"
|
|
|
|
{"redirect/dir/308", false}, // Request is redirected to "/redirected/"
|
|
|
|
|
|
|
|
// 4xx client errors
|
|
|
|
{"parent/403", true}, // Forbidden status (head request blocked)
|
|
|
|
{"parent/404", false}, // Not found status
|
|
|
|
} {
|
|
|
|
for _, noHead := range []bool{false, true} {
|
|
|
|
var isFile bool
|
|
|
|
if noHead {
|
|
|
|
m.Set("no_head", "true")
|
|
|
|
isFile = true
|
|
|
|
} else {
|
|
|
|
m.Set("no_head", "false")
|
|
|
|
isFile = test.isFile
|
|
|
|
}
|
|
|
|
headCount = 0
|
|
|
|
f, err := NewFs(context.Background(), remoteName, test.root, m)
|
|
|
|
if noHead {
|
|
|
|
assert.Equal(t, 0, headCount)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, 1, headCount)
|
|
|
|
}
|
|
|
|
if isFile {
|
|
|
|
assert.ErrorIs(t, err, fs.ErrorIsFile)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
var endpoint string
|
|
|
|
if isFile {
|
|
|
|
parent, _ := path.Split(test.root)
|
|
|
|
endpoint = "/" + parent
|
|
|
|
} else {
|
|
|
|
endpoint = "/" + test.root + "/"
|
|
|
|
}
|
|
|
|
what := fmt.Sprintf("i=%d, root=%q, isFile=%v, noHead=%v", i, test.root, isFile, noHead)
|
|
|
|
assert.Equal(t, ts.URL+endpoint, f.String(), what)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|