Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve http: tests for --auth-proxy

Nick Craig-Wood2dabbe83ac61
diff --git a/cmd/serve/http/http_test.go b/cmd/serve/http/http_test.go
index 0d09820e2..497e9732b 100644
--- a/cmd/serve/http/http_test.go
+++ b/cmd/serve/http/http_test.go
@@ -6,13 +6,14 @@ import (
"io"
"net/http"
"os"
+ "path/filepath"
"strings"
"testing"
"time"
_ "github.com/rclone/rclone/backend/local"
+ "github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
"github.com/rclone/rclone/fs"
- "github.com/rclone/rclone/fs/config/configfile"
"github.com/rclone/rclone/fs/filter"
libhttp "github.com/rclone/rclone/lib/http"
"github.com/stretchr/testify/assert"
@@ -21,18 +22,16 @@ import (
var (
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
- sc *HTTP
- testURL string
)
const (
testBindAddress = "localhost:0"
+ testUser = "user"
+ testPass = "pass"
testTemplate = "testdata/golden/testindex.html"
)
-func start(t *testing.T, f fs.Fs) {
- ctx := context.Background()
-
+func start(ctx context.Context, t *testing.T, f fs.Fs) (s *HTTP, testURL string) {
opts := Options{
HTTP: libhttp.DefaultCfg(),
Template: libhttp.TemplateConfig{
@@ -40,10 +39,13 @@ func start(t *testing.T, f fs.Fs) {
},
}
opts.HTTP.ListenAddr = []string{testBindAddress}
+ if proxyflags.Opt.AuthProxy == "" {
+ opts.Auth.BasicUser = testUser
+ opts.Auth.BasicPass = testPass
+ }
s, err := run(ctx, f, opts)
require.NoError(t, err, "failed to start server")
- sc = s
urls := s.server.URLs()
require.Len(t, urls, 1, "expected one URL")
@@ -63,6 +65,8 @@ func start(t *testing.T, f fs.Fs) {
pause *= 2
}
t.Fatal("couldn't connect to server")
+
+ return s, testURL
}
var (
@@ -70,31 +74,6 @@ var (
expectedTime = time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)
)
-func TestInit(t *testing.T) {
- ctx := context.Background()
- // Configure the remote
- configfile.Install()
- // fs.Config.LogLevel = fs.LogLevelDebug
- // fs.Config.DumpHeaders = true
- // fs.Config.DumpBodies = true
-
- // exclude files called hidden.txt and directories called hidden
- fi := filter.GetConfig(ctx)
- require.NoError(t, fi.AddRule("- hidden.txt"))
- require.NoError(t, fi.AddRule("- hidden/**"))
-
- // Create a test Fs
- f, err := fs.NewFs(context.Background(), "testdata/files")
- require.NoError(t, err)
-
- // 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))
-
- start(t, f)
-}
-
// check body against the file, or re-write body if -updategolden is
// set.
func checkGolden(t *testing.T, fileName string, got []byte) {
@@ -111,7 +90,49 @@ func checkGolden(t *testing.T, fileName string, got []byte) {
}
}
-func TestGET(t *testing.T) {
+func testGET(t *testing.T, useProxy bool) {
+ ctx := context.Background()
+ // ci := fs.GetConfig(ctx)
+ // ci.LogLevel = fs.LogLevelDebug
+
+ // exclude files called hidden.txt and directories called hidden
+ fi := filter.GetConfig(ctx)
+ require.NoError(t, fi.AddRule("- hidden.txt"))
+ require.NoError(t, fi.AddRule("- hidden/**"))
+
+ var f fs.Fs
+ if useProxy {
+ // the backend config will be made by the proxy
+ prog, err := filepath.Abs("../servetest/proxy_code.go")
+ require.NoError(t, err)
+ files, err := filepath.Abs("testdata/files")
+ require.NoError(t, err)
+ cmd := "go run " + prog + " " + files
+
+ // FIXME this is untidy setting a global variable!
+ proxyflags.Opt.AuthProxy = cmd
+ defer func() {
+ proxyflags.Opt.AuthProxy = ""
+ }()
+
+ f = nil
+ } else {
+ // Create a test Fs
+ var err error
+ f, err = fs.NewFs(context.Background(), "testdata/files")
+ require.NoError(t, err)
+
+ // 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))
+ }
+
+ s, testURL := start(ctx, t, f)
+ defer func() {
+ assert.NoError(t, s.server.Shutdown())
+ }()
+
for _, test := range []struct {
URL string
Status int
@@ -216,6 +237,7 @@ func TestGET(t *testing.T) {
if test.Range != "" {
req.Header.Add("Range", test.Range)
}
+ req.SetBasicAuth(testUser, testPass)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, test.Status, resp.StatusCode, test.Golden)
@@ -237,3 +259,11 @@ func TestGET(t *testing.T) {
checkGolden(t, test.Golden, body)
}
}
+
+func TestGET(t *testing.T) {
+ testGET(t, false)
+}
+
+func TestAuthProxy(t *testing.T) {
+ testGET(t, true)
+}