2017-04-25 13:57:59 +00:00
|
|
|
// Serve webdav tests set up a server and run the integration tests
|
|
|
|
// for the webdav remote against it.
|
|
|
|
//
|
|
|
|
// We skip tests on platforms with troublesome character mappings
|
|
|
|
|
2018-11-25 16:34:51 +00:00
|
|
|
//+build !windows,!darwin,go1.9
|
2017-04-25 13:57:59 +00:00
|
|
|
|
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2019-06-12 12:01:48 +00:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2017-04-25 13:57:59 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-06-12 12:01:48 +00:00
|
|
|
"strings"
|
2017-04-25 13:57:59 +00:00
|
|
|
"testing"
|
2019-06-12 12:01:48 +00:00
|
|
|
"time"
|
2017-04-25 13:57:59 +00:00
|
|
|
|
2018-01-11 16:05:41 +00:00
|
|
|
_ "github.com/ncw/rclone/backend/local"
|
2018-02-14 20:39:11 +00:00
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib"
|
2019-06-12 12:01:48 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fs/filter"
|
2017-04-25 13:57:59 +00:00
|
|
|
"github.com/ncw/rclone/fstest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-12 12:01:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-10 10:54:44 +00:00
|
|
|
"golang.org/x/net/webdav"
|
2017-04-25 13:57:59 +00:00
|
|
|
)
|
|
|
|
|
2018-02-14 20:39:11 +00:00
|
|
|
const (
|
2019-05-01 10:16:22 +00:00
|
|
|
testBindAddress = "localhost:0"
|
2018-02-14 20:39:11 +00:00
|
|
|
)
|
|
|
|
|
2018-06-10 10:54:44 +00:00
|
|
|
// check interfaces
|
|
|
|
var (
|
|
|
|
_ os.FileInfo = FileInfo{nil}
|
|
|
|
_ webdav.ETager = FileInfo{nil}
|
|
|
|
_ webdav.ContentTyper = FileInfo{nil}
|
|
|
|
)
|
|
|
|
|
2017-04-25 13:57:59 +00:00
|
|
|
// TestWebDav runs the webdav server then runs the unit tests for the
|
|
|
|
// webdav remote against it.
|
|
|
|
func TestWebDav(t *testing.T) {
|
2018-02-14 20:39:11 +00:00
|
|
|
opt := httplib.DefaultOpt
|
|
|
|
opt.ListenAddr = testBindAddress
|
|
|
|
|
2017-04-25 13:57:59 +00:00
|
|
|
fstest.Initialise()
|
|
|
|
|
|
|
|
fremote, _, clean, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer clean()
|
|
|
|
|
2019-06-17 08:34:30 +00:00
|
|
|
err = fremote.Mkdir(context.Background(), "")
|
2017-04-25 13:57:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Start the server
|
2018-02-19 14:02:19 +00:00
|
|
|
w := newWebDAV(fremote, &opt)
|
2018-11-01 17:16:31 +00:00
|
|
|
assert.NoError(t, w.serve())
|
|
|
|
defer func() {
|
|
|
|
w.Close()
|
|
|
|
w.Wait()
|
|
|
|
}()
|
2017-04-25 13:57:59 +00:00
|
|
|
|
|
|
|
// Change directory to run the tests
|
2018-01-11 16:05:41 +00:00
|
|
|
err = os.Chdir("../../../backend/webdav")
|
2017-04-25 13:57:59 +00:00
|
|
|
assert.NoError(t, err, "failed to cd to webdav remote")
|
|
|
|
|
|
|
|
// Run the webdav tests with an on the fly remote
|
|
|
|
args := []string{"test"}
|
|
|
|
if testing.Verbose() {
|
|
|
|
args = append(args, "-v")
|
|
|
|
}
|
|
|
|
if *fstest.Verbose {
|
|
|
|
args = append(args, "-verbose")
|
|
|
|
}
|
|
|
|
args = append(args, "-remote", "webdavtest:")
|
|
|
|
cmd := exec.Command("go", args...)
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"RCLONE_CONFIG_WEBDAVTEST_TYPE=webdav",
|
2019-05-01 10:16:22 +00:00
|
|
|
"RCLONE_CONFIG_WEBDAVTEST_URL="+w.Server.URL(),
|
2017-04-25 13:57:59 +00:00
|
|
|
"RCLONE_CONFIG_WEBDAVTEST_VENDOR=other",
|
|
|
|
)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Logf("\n----------\n%s----------\n", string(out))
|
|
|
|
}
|
|
|
|
assert.NoError(t, err, "Running webdav integration tests")
|
|
|
|
}
|
2019-06-12 12:01:48 +00:00
|
|
|
|
|
|
|
// Test serve http functionality in serve webdav
|
|
|
|
// While similar to http serve, there are some inconsistencies
|
|
|
|
// in the handling of some requests such as POST requests
|
|
|
|
|
|
|
|
var (
|
|
|
|
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTPFunction(t *testing.T) {
|
|
|
|
// cd to correct directory for testing
|
|
|
|
err := os.Chdir("../../cmd/serve/webdav")
|
|
|
|
assert.NoError(t, err, "failed to cd to webdav cmd directory")
|
|
|
|
|
|
|
|
// exclude files called hidden.txt and directories called hidden
|
|
|
|
require.NoError(t, filter.Active.AddRule("- hidden.txt"))
|
|
|
|
require.NoError(t, filter.Active.AddRule("- hidden/**"))
|
|
|
|
|
|
|
|
// Uses the same test files as http tests but with different golden.
|
|
|
|
f, err := fs.NewFs("../http/testdata/files")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
opt := httplib.DefaultOpt
|
|
|
|
opt.ListenAddr = testBindAddress
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
w := newWebDAV(f, &opt)
|
|
|
|
assert.NoError(t, w.serve())
|
|
|
|
defer func() {
|
|
|
|
w.Close()
|
|
|
|
w.Wait()
|
|
|
|
}()
|
|
|
|
testURL := w.Server.URL()
|
|
|
|
pause := time.Millisecond
|
|
|
|
i := 0
|
|
|
|
for ; i < 10; i++ {
|
|
|
|
resp, err := http.Head(testURL)
|
|
|
|
if err == nil {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// t.Logf("couldn't connect, sleeping for %v: %v", pause, err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
pause *= 2
|
|
|
|
}
|
|
|
|
if i >= 10 {
|
|
|
|
t.Fatal("couldn't connect to server")
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpTestGET(t, testURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
err := ioutil.WriteFile(fileName, got, 0666)
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
want, err := ioutil.ReadFile(fileName)
|
|
|
|
require.NoError(t, err, "problem")
|
|
|
|
wants := strings.Split(string(want), "\n")
|
|
|
|
gots := strings.Split(string(got), "\n")
|
|
|
|
assert.Equal(t, wants, gots, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HelpTestGET(t *testing.T, testURL string) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
URL string
|
|
|
|
Status int
|
|
|
|
Golden string
|
|
|
|
Method string
|
|
|
|
Range string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
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.StatusOK,
|
|
|
|
Golden: "testdata/golden/onepost.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
method := test.Method
|
|
|
|
if method == "" {
|
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, testURL+test.URL, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if test.Range != "" {
|
|
|
|
req.Header.Add("Range", test.Range)
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.Status, resp.StatusCode, test.Golden)
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkGolden(t, test.Golden, body)
|
|
|
|
}
|
|
|
|
}
|