From ac0dc9922ee9e5171f5e2d3e0f9a2e6efb807f78 Mon Sep 17 00:00:00 2001 From: buengese Date: Sun, 19 Jun 2022 15:39:05 +0200 Subject: [PATCH] copyurl: add tests for the option to honor the HTTP header filename directive --- fs/operations/operations_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index 545bcbe4d..04b26e8b8 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -729,10 +729,15 @@ func TestCopyURL(t *testing.T) { // check when reading from regular HTTP server status := 0 + nameHeader := false + headerFilename := "headerfilename.txt" handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if status != 0 { http.Error(w, "an error ocurred", status) } + if nameHeader { + w.Header().Set("Content-Disposition", `attachment; filename="folder\`+headerFilename+`"`) + } _, err := w.Write([]byte(contents)) assert.NoError(t, err) }) @@ -757,10 +762,22 @@ func TestCopyURL(t *testing.T) { assert.Equal(t, int64(len(contents)), o.Size()) assert.Equal(t, urlFileName, o.Remote()) + // Check header file naming + nameHeader = true + o, err = operations.CopyURL(ctx, r.Fremote, "", ts.URL, true, true, false) + require.NoError(t, err) + assert.Equal(t, int64(len(contents)), o.Size()) + assert.Equal(t, headerFilename, o.Remote()) + // Check auto file naming when url without file name _, err = operations.CopyURL(ctx, r.Fremote, "file1", ts.URL, true, false, false) require.Error(t, err) + // Check header file naming without header set + nameHeader = false + _, err = operations.CopyURL(ctx, r.Fremote, "file1", ts.URL, true, true, false) + require.Error(t, err) + // Check an error is returned for a 404 status = http.StatusNotFound o, err = operations.CopyURL(ctx, r.Fremote, "file1", ts.URL, false, false, false) @@ -779,7 +796,7 @@ func TestCopyURL(t *testing.T) { o, err = operations.CopyURL(ctx, r.Fremote, "file2", tss.URL, false, false, false) require.NoError(t, err) assert.Equal(t, int64(len(contents)), o.Size()) - fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1, file2, fstest.NewItem(urlFileName, contents, t1)}, nil, fs.ModTimeNotSupported) + fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1, file2, fstest.NewItem(urlFileName, contents, t1), fstest.NewItem(headerFilename, contents, t1)}, nil, fs.ModTimeNotSupported) } func TestCopyURLToWriter(t *testing.T) {