github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/backend/webdav/webdav_internal_test.go (about)

     1  package webdav_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/rclone/rclone/backend/webdav"
    12  	"github.com/rclone/rclone/fs"
    13  	"github.com/rclone/rclone/fs/config/configfile"
    14  	"github.com/rclone/rclone/fs/config/configmap"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  var (
    20  	remoteName = "TestWebDAV"
    21  	headers    = []string{"X-Potato", "sausage", "X-Rhubarb", "cucumber"}
    22  )
    23  
    24  // prepareServer the test server and return a function to tidy it up afterwards
    25  // with each request the headers option tests are executed
    26  func prepareServer(t *testing.T) (configmap.Simple, func()) {
    27  	// test the headers are there send send a dummy response to About
    28  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    29  		what := fmt.Sprintf("%s %s: Header ", r.Method, r.URL.Path)
    30  		assert.Equal(t, headers[1], r.Header.Get(headers[0]), what+headers[0])
    31  		assert.Equal(t, headers[3], r.Header.Get(headers[2]), what+headers[2])
    32  		fmt.Fprintf(w, `<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
    33  <d:response>
    34   <d:href>/remote.php/webdav/</d:href>
    35   <d:propstat>
    36    <d:prop>
    37     <d:quota-available-bytes>-3</d:quota-available-bytes>
    38     <d:quota-used-bytes>376461895</d:quota-used-bytes>
    39    </d:prop>
    40    <d:status>HTTP/1.1 200 OK</d:status>
    41   </d:propstat>
    42  </d:response>
    43  </d:multistatus>`)
    44  	})
    45  
    46  	// Make the test server
    47  	ts := httptest.NewServer(handler)
    48  
    49  	// Configure the remote
    50  	configfile.Install()
    51  
    52  	m := configmap.Simple{
    53  		"type": "webdav",
    54  		"url":  ts.URL,
    55  		// add headers to test the headers option
    56  		"headers": strings.Join(headers, ","),
    57  	}
    58  
    59  	// return a function to tidy up
    60  	return m, ts.Close
    61  }
    62  
    63  // prepare the test server and return a function to tidy it up afterwards
    64  func prepare(t *testing.T) (fs.Fs, func()) {
    65  	m, tidy := prepareServer(t)
    66  
    67  	// Instantiate the WebDAV server
    68  	f, err := webdav.NewFs(context.Background(), remoteName, "", m)
    69  	require.NoError(t, err)
    70  
    71  	return f, tidy
    72  }
    73  
    74  // TestHeaders any request will test the headers option
    75  func TestHeaders(t *testing.T) {
    76  	f, tidy := prepare(t)
    77  	defer tidy()
    78  
    79  	// send an About response since that is all the dummy server can return
    80  	_, err := f.Features().About(context.Background())
    81  	require.NoError(t, err)
    82  }