2018-01-05 17:47:00 +01:00
|
|
|
// +build go1.8
|
|
|
|
|
2016-12-06 19:00:24 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-01-05 17:47:00 +01:00
|
|
|
"path"
|
2016-12-06 19:00:24 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-01-11 17:05:41 +01:00
|
|
|
_ "github.com/ncw/rclone/backend/local"
|
2018-02-15 16:01:19 +01:00
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib"
|
2016-12-06 19:00:24 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
|
|
|
"github.com/ncw/rclone/fs/filter"
|
2016-12-06 19:00:24 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-02-19 15:02:19 +01:00
|
|
|
var (
|
|
|
|
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
|
|
|
|
httpServer *server
|
|
|
|
)
|
2016-12-06 19:00:24 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
testBindAddress = "localhost:51777"
|
|
|
|
testURL = "http://" + testBindAddress + "/"
|
|
|
|
)
|
|
|
|
|
|
|
|
func startServer(t *testing.T, f fs.Fs) {
|
2018-02-15 16:01:19 +01:00
|
|
|
opt := httplib.DefaultOpt
|
|
|
|
opt.ListenAddr = testBindAddress
|
2018-02-19 15:02:19 +01:00
|
|
|
httpServer = newServer(f, &opt)
|
2018-11-01 18:16:31 +01:00
|
|
|
assert.NoError(t, httpServer.Serve())
|
2016-12-06 19:00:24 +01:00
|
|
|
|
|
|
|
// try to connect to the test server
|
|
|
|
pause := time.Millisecond
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
conn, err := net.Dial("tcp", testBindAddress)
|
|
|
|
if err == nil {
|
|
|
|
_ = conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// t.Logf("couldn't connect, sleeping for %v: %v", pause, err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
pause *= 2
|
|
|
|
}
|
|
|
|
t.Fatal("couldn't connect to server")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
// Configure the remote
|
2018-01-12 17:30:54 +01:00
|
|
|
config.LoadConfig()
|
2016-12-06 19:00:24 +01:00
|
|
|
// fs.Config.LogLevel = fs.LogLevelDebug
|
|
|
|
// fs.Config.DumpHeaders = true
|
|
|
|
// fs.Config.DumpBodies = true
|
|
|
|
|
|
|
|
// exclude files called hidden.txt and directories called hidden
|
2018-01-12 17:30:54 +01:00
|
|
|
require.NoError(t, filter.Active.AddRule("- hidden.txt"))
|
|
|
|
require.NoError(t, filter.Active.AddRule("- hidden/**"))
|
2016-12-06 19:00:24 +01:00
|
|
|
|
|
|
|
// Create a test Fs
|
|
|
|
f, err := fs.NewFs("testdata/files")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
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)
|
|
|
|
err := ioutil.WriteFile(fileName, got, 0666)
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
want, err := ioutil.ReadFile(fileName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wants := strings.Split(string(want), "\n")
|
|
|
|
gots := strings.Split(string(got), "\n")
|
|
|
|
assert.Equal(t, wants, gots, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:56:31 +02:00
|
|
|
func TestGET(t *testing.T) {
|
2016-12-06 19:00:24 +01:00
|
|
|
for _, test := range []struct {
|
|
|
|
URL string
|
|
|
|
Status int
|
|
|
|
Golden string
|
|
|
|
Method string
|
2017-10-27 21:56:31 +02:00
|
|
|
Range string
|
2016-12-06 19:00:24 +01: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-27 21:56:31 +02: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-06 19:00:24 +01:00
|
|
|
} {
|
|
|
|
method := test.Method
|
|
|
|
if method == "" {
|
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, testURL+test.URL, nil)
|
|
|
|
require.NoError(t, err)
|
2017-10-27 21:56:31 +02:00
|
|
|
if test.Range != "" {
|
|
|
|
req.Header.Add("Range", test.Range)
|
|
|
|
}
|
2016-12-06 19:00:24 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 17:47:00 +01:00
|
|
|
|
|
|
|
type mockNode struct {
|
|
|
|
path string
|
|
|
|
isdir bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n mockNode) Path() string { return n.path }
|
|
|
|
func (n mockNode) Name() string {
|
|
|
|
if n.path == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return path.Base(n.path)
|
|
|
|
}
|
|
|
|
func (n mockNode) IsDir() bool { return n.isdir }
|
|
|
|
|
|
|
|
func TestAddEntry(t *testing.T) {
|
|
|
|
var es entries
|
|
|
|
es.addEntry(mockNode{path: "", isdir: true})
|
|
|
|
es.addEntry(mockNode{path: "dir", isdir: true})
|
|
|
|
es.addEntry(mockNode{path: "a/b/c/d.txt", isdir: false})
|
|
|
|
es.addEntry(mockNode{path: "a/b/c/colon:colon.txt", isdir: false})
|
|
|
|
es.addEntry(mockNode{path: "\"quotes\".txt", isdir: false})
|
|
|
|
assert.Equal(t, entries{
|
|
|
|
{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"},
|
|
|
|
}, es)
|
|
|
|
}
|
2018-02-19 15:02:19 +01:00
|
|
|
|
|
|
|
func TestFinalise(t *testing.T) {
|
2018-11-01 18:16:31 +01:00
|
|
|
httpServer.Close()
|
|
|
|
httpServer.Wait()
|
2018-02-19 15:02:19 +01:00
|
|
|
}
|