2016-12-07 02:00:24 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-05-05 16:41:08 +08:00
|
|
|
"context"
|
2016-12-07 02:00:24 +08:00
|
|
|
"flag"
|
2022-08-20 22:38:02 +08:00
|
|
|
"io"
|
2016-12-07 02:00:24 +08:00
|
|
|
"net/http"
|
2022-08-20 22:38:02 +08:00
|
|
|
"os"
|
2016-12-07 02:00:24 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
_ "github.com/rclone/rclone/backend/local"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-03-10 23:40:34 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2021-04-20 13:14:21 +08:00
|
|
|
httplib "github.com/rclone/rclone/lib/http"
|
2016-12-07 02:00:24 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-02-19 22:02:19 +08:00
|
|
|
var (
|
|
|
|
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
|
2019-06-13 17:51:16 +08:00
|
|
|
httpServer *server
|
2019-05-01 18:16:22 +08:00
|
|
|
testURL string
|
2018-02-19 22:02:19 +08:00
|
|
|
)
|
2016-12-07 02:00:24 +08:00
|
|
|
|
|
|
|
const (
|
2019-05-01 18:16:22 +08:00
|
|
|
testBindAddress = "localhost:0"
|
2020-05-08 23:15:21 +08:00
|
|
|
testTemplate = "testdata/golden/testindex.html"
|
2016-12-07 02:00:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func startServer(t *testing.T, f fs.Fs) {
|
2018-02-15 23:01:19 +08:00
|
|
|
opt := httplib.DefaultOpt
|
|
|
|
opt.ListenAddr = testBindAddress
|
2021-04-20 13:14:21 +08:00
|
|
|
httpServer = newServer(f, testTemplate)
|
|
|
|
router, err := httplib.Router()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
2021-05-02 16:08:06 +08:00
|
|
|
httpServer.Bind(router)
|
2021-04-20 13:14:21 +08:00
|
|
|
testURL = httplib.URL()
|
2016-12-07 02:00:24 +08:00
|
|
|
|
|
|
|
// try to connect to the test server
|
|
|
|
pause := time.Millisecond
|
|
|
|
for i := 0; i < 10; i++ {
|
2019-05-01 18:16:22 +08:00
|
|
|
resp, err := http.Head(testURL)
|
2016-12-07 02:00:24 +08:00
|
|
|
if err == nil {
|
2019-05-01 18:16:22 +08:00
|
|
|
_ = resp.Body.Close()
|
2016-12-07 02:00:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// t.Logf("couldn't connect, sleeping for %v: %v", pause, err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
pause *= 2
|
|
|
|
}
|
|
|
|
t.Fatal("couldn't connect to server")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:41:08 +08:00
|
|
|
var (
|
|
|
|
datedObject = "two.txt"
|
|
|
|
expectedTime = time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)
|
|
|
|
)
|
|
|
|
|
2016-12-07 02:00:24 +08:00
|
|
|
func TestInit(t *testing.T) {
|
2020-11-27 01:10:41 +08:00
|
|
|
ctx := context.Background()
|
2016-12-07 02:00:24 +08:00
|
|
|
// Configure the remote
|
2021-04-27 05:37:49 +08:00
|
|
|
configfile.Install()
|
2016-12-07 02:00:24 +08:00
|
|
|
// fs.Config.LogLevel = fs.LogLevelDebug
|
|
|
|
// fs.Config.DumpHeaders = true
|
|
|
|
// fs.Config.DumpBodies = true
|
|
|
|
|
|
|
|
// exclude files called hidden.txt and directories called hidden
|
2020-11-27 01:10:41 +08:00
|
|
|
fi := filter.GetConfig(ctx)
|
|
|
|
require.NoError(t, fi.AddRule("- hidden.txt"))
|
|
|
|
require.NoError(t, fi.AddRule("- hidden/**"))
|
2016-12-07 02:00:24 +08:00
|
|
|
|
|
|
|
// Create a test Fs
|
2020-11-05 23:18:51 +08:00
|
|
|
f, err := fs.NewFs(context.Background(), "testdata/files")
|
2016-12-07 02:00:24 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-05 16:41:08 +08:00
|
|
|
// set date of datedObject to expectedTime
|
|
|
|
obj, err := f.NewObject(context.Background(), datedObject)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, obj.SetModTime(context.Background(), expectedTime))
|
|
|
|
|
2016-12-07 02:00:24 +08:00
|
|
|
startServer(t, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check body against the file, or re-write body if -updategolden is
|
|
|
|
// set.
|
|
|
|
func checkGolden(t *testing.T, fileName string, got []byte) {
|
|
|
|
if *updateGolden {
|
|
|
|
t.Logf("Updating golden file %q", fileName)
|
2022-08-20 22:38:02 +08:00
|
|
|
err := os.WriteFile(fileName, got, 0666)
|
2016-12-07 02:00:24 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2022-08-20 22:38:02 +08:00
|
|
|
want, err := os.ReadFile(fileName)
|
2016-12-07 02:00:24 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
wants := strings.Split(string(want), "\n")
|
|
|
|
gots := strings.Split(string(got), "\n")
|
|
|
|
assert.Equal(t, wants, gots, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 03:56:31 +08:00
|
|
|
func TestGET(t *testing.T) {
|
2016-12-07 02:00:24 +08:00
|
|
|
for _, test := range []struct {
|
|
|
|
URL string
|
|
|
|
Status int
|
|
|
|
Golden string
|
|
|
|
Method string
|
2017-10-28 03:56:31 +08:00
|
|
|
Range string
|
2016-12-07 02:00:24 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
URL: "",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/index.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "notfound",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/notfound.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "dirnotfound/",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/dirnotfound.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "hidden/",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/hiddendir.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "one%25.txt",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/one.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "hidden.txt",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/hidden.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "three/",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/three.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "three/a.txt",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/a.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "",
|
|
|
|
Method: "HEAD",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/indexhead.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "one%25.txt",
|
|
|
|
Method: "HEAD",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/onehead.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "",
|
|
|
|
Method: "POST",
|
|
|
|
Status: http.StatusMethodNotAllowed,
|
|
|
|
Golden: "testdata/golden/indexpost.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "one%25.txt",
|
|
|
|
Method: "POST",
|
|
|
|
Status: http.StatusMethodNotAllowed,
|
|
|
|
Golden: "testdata/golden/onepost.txt",
|
|
|
|
},
|
2017-10-28 03:56:31 +08:00
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/two.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusPartialContent,
|
|
|
|
Range: "bytes=2-5",
|
|
|
|
Golden: "testdata/golden/two2-5.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusPartialContent,
|
|
|
|
Range: "bytes=0-6",
|
|
|
|
Golden: "testdata/golden/two-6.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusPartialContent,
|
|
|
|
Range: "bytes=3-",
|
|
|
|
Golden: "testdata/golden/two3-.txt",
|
|
|
|
},
|
2016-12-07 02:00:24 +08:00
|
|
|
} {
|
|
|
|
method := test.Method
|
|
|
|
if method == "" {
|
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, testURL+test.URL, nil)
|
|
|
|
require.NoError(t, err)
|
2017-10-28 03:56:31 +08:00
|
|
|
if test.Range != "" {
|
|
|
|
req.Header.Add("Range", test.Range)
|
|
|
|
}
|
2016-12-07 02:00:24 +08:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.Status, resp.StatusCode, test.Golden)
|
2022-08-20 22:38:02 +08:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2016-12-07 02:00:24 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
// Check we got a Last-Modified header and that it is a valid date
|
2020-05-05 16:41:08 +08:00
|
|
|
if test.Status == http.StatusOK || test.Status == http.StatusPartialContent {
|
|
|
|
lastModified := resp.Header.Get("Last-Modified")
|
|
|
|
assert.NotEqual(t, "", lastModified, test.Golden)
|
|
|
|
modTime, err := http.ParseTime(lastModified)
|
|
|
|
assert.NoError(t, err, test.Golden)
|
|
|
|
// check the actual date on our special file
|
|
|
|
if test.URL == datedObject {
|
|
|
|
assert.Equal(t, expectedTime, modTime, test.Golden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 02:00:24 +08:00
|
|
|
checkGolden(t, test.Golden, body)
|
|
|
|
}
|
|
|
|
}
|
2018-01-06 00:47:00 +08:00
|
|
|
|
2018-02-19 22:02:19 +08:00
|
|
|
func TestFinalise(t *testing.T) {
|
2021-04-20 13:14:21 +08:00
|
|
|
_ = httplib.Shutdown()
|
2018-02-19 22:02:19 +08:00
|
|
|
}
|