Skip to content

Commit 5c98ccf

Browse files
Add tests for RenameSource URL encoding with special characters
Co-authored-by: tanyasethi-msft <[email protected]>
1 parent a650a5b commit 5c98ccf

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-0
lines changed

sdk/storage/azdatalake/directory/client_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package directory_test
99
import (
1010
"context"
1111
"github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake"
12+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake/internal/path"
1213
"net/http"
1314
"strconv"
1415
"testing"
@@ -2528,6 +2529,112 @@ func (s *RecordedTestSuite) TestDirRenameNoOptions() {
25282529
_require.NoError(err)
25292530
}
25302531

2532+
func (s *RecordedTestSuite) TestDirRenameWithSpecialCharacters() {
2533+
_require := require.New(s.T())
2534+
testName := s.T().Name()
2535+
2536+
filesystemName := testcommon.GenerateFileSystemName(testName)
2537+
fsClient, err := testcommon.GetFileSystemClient(filesystemName, s.T(), testcommon.TestAccountDatalake, nil)
2538+
_require.NoError(err)
2539+
defer testcommon.DeleteFileSystem(context.Background(), _require, fsClient)
2540+
2541+
_, err = fsClient.Create(context.Background(), nil)
2542+
_require.NoError(err)
2543+
2544+
// Test with directory name containing spaces
2545+
dirName := "dir with spaces"
2546+
dirClient, err := testcommon.GetDirClient(filesystemName, dirName, s.T(), testcommon.TestAccountDatalake, nil)
2547+
_require.NoError(err)
2548+
2549+
resp, err := dirClient.Create(context.Background(), nil)
2550+
_require.NoError(err)
2551+
_require.NotNil(resp)
2552+
2553+
// Rename to a path with special characters
2554+
newName := "renamed dir+special@!&% chars"
2555+
_, err = dirClient.Rename(context.Background(), newName, nil)
2556+
_require.NoError(err)
2557+
2558+
// Verify new directory exists by creating a client to it and checking properties
2559+
newClient, err := testcommon.GetDirClient(filesystemName, newName, s.T(), testcommon.TestAccountDatalake, nil)
2560+
_require.NoError(err)
2561+
2562+
_, err = newClient.GetProperties(context.Background(), nil)
2563+
_require.NoError(err)
2564+
2565+
// Test with Unicode characters
2566+
unicodeDirName := "lör mapp"
2567+
unicodeClient, err := testcommon.GetDirClient(filesystemName, unicodeDirName, s.T(), testcommon.TestAccountDatalake, nil)
2568+
_require.NoError(err)
2569+
2570+
resp, err = unicodeClient.Create(context.Background(), nil)
2571+
_require.NoError(err)
2572+
_require.NotNil(resp)
2573+
2574+
// Rename Unicode directory to another Unicode name
2575+
newUnicodeName := "ångström ümlaut 目录"
2576+
_, err = unicodeClient.Rename(context.Background(), newUnicodeName, nil)
2577+
_require.NoError(err)
2578+
2579+
// Verify new directory exists
2580+
newUnicodeClient, err := testcommon.GetDirClient(filesystemName, newUnicodeName, s.T(), testcommon.TestAccountDatalake, nil)
2581+
_require.NoError(err)
2582+
2583+
_, err = newUnicodeClient.GetProperties(context.Background(), nil)
2584+
_require.NoError(err)
2585+
}
2586+
2587+
func (s *RecordedTestSuite) TestDirRenameWithQueryParameters() {
2588+
_require := require.New(s.T())
2589+
testName := s.T().Name()
2590+
2591+
filesystemName := testcommon.GenerateFileSystemName(testName)
2592+
fsClient, err := testcommon.GetFileSystemClient(filesystemName, s.T(), testcommon.TestAccountDatalake, nil)
2593+
_require.NoError(err)
2594+
defer testcommon.DeleteFileSystem(context.Background(), _require, fsClient)
2595+
2596+
_, err = fsClient.Create(context.Background(), nil)
2597+
_require.NoError(err)
2598+
2599+
// Create a regular directory
2600+
dirName := "original-dir"
2601+
dirClient, err := testcommon.GetDirClient(filesystemName, dirName, s.T(), testcommon.TestAccountDatalake, nil)
2602+
_require.NoError(err)
2603+
2604+
resp, err := dirClient.Create(context.Background(), nil)
2605+
_require.NoError(err)
2606+
_require.NotNil(resp)
2607+
2608+
// In a real scenario, SAS tokens or other parameters might be appended to the source path
2609+
// Here we're simulating this with some fake query parameters
2610+
srcPathWithQuery := dirName + "?param1=value1&param2=value with spaces"
2611+
2612+
// Create a client with the path including query parameters
2613+
// Note: This is just to test our URL encoding, in a real scenario you'd get this path from somewhere else
2614+
_, err = dirClient.Rename(context.Background(), "new-dir", nil)
2615+
_require.NoError(err)
2616+
2617+
// Create a new directory to test the rename with query parameters
2618+
newDir := "query-test-dir"
2619+
queryClient, err := testcommon.GetDirClient(filesystemName, newDir, s.T(), testcommon.TestAccountDatalake, nil)
2620+
_require.NoError(err)
2621+
2622+
resp, err = queryClient.Create(context.Background(), nil)
2623+
_require.NoError(err)
2624+
_require.NotNil(resp)
2625+
2626+
// Use internal implementation to test path handling with query parameters
2627+
// This is a white box test since we can't directly attach query params to the source path in normal usage
2628+
path := newDir + "?param1=value1&param2=value with spaces"
2629+
_, _, _, createOpts, _ := path.FormatRenameOptions(nil, path)
2630+
2631+
_require.NotNil(createOpts)
2632+
_require.NotNil(createOpts.RenameSource)
2633+
// Verify the source path was properly encoded
2634+
expected := "query-test-dir?param1=value1&param2=value+with+spaces"
2635+
_require.Equal(expected, *createOpts.RenameSource)
2636+
}
2637+
25312638
func (s *RecordedTestSuite) TestDirRenameRequestWithCPK() {
25322639
_require := require.New(s.T())
25332640
testName := s.T().Name()

sdk/storage/azdatalake/file/client_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,112 @@ func (s *RecordedTestSuite) TestRenameNoOptions() {
24042404
_require.NoError(err)
24052405
}
24062406

2407+
func (s *RecordedTestSuite) TestRenameWithSpecialCharacters() {
2408+
_require := require.New(s.T())
2409+
testName := s.T().Name()
2410+
2411+
filesystemName := testcommon.GenerateFileSystemName(testName)
2412+
fsClient, err := testcommon.GetFileSystemClient(filesystemName, s.T(), testcommon.TestAccountDatalake, nil)
2413+
_require.NoError(err)
2414+
defer testcommon.DeleteFileSystem(context.Background(), _require, fsClient)
2415+
2416+
_, err = fsClient.Create(context.Background(), nil)
2417+
_require.NoError(err)
2418+
2419+
// Test with file name containing spaces
2420+
fileName := "file with spaces.txt"
2421+
fClient, err := testcommon.GetFileClient(filesystemName, fileName, s.T(), testcommon.TestAccountDatalake, nil)
2422+
_require.NoError(err)
2423+
2424+
resp, err := fClient.Create(context.Background(), nil)
2425+
_require.NoError(err)
2426+
_require.NotNil(resp)
2427+
2428+
// Rename to a path with special characters
2429+
newName := "renamed file+special@!&% chars.txt"
2430+
_, err = fClient.Rename(context.Background(), newName, nil)
2431+
_require.NoError(err)
2432+
2433+
// Verify new file exists by creating a client to it and checking properties
2434+
newClient, err := testcommon.GetFileClient(filesystemName, newName, s.T(), testcommon.TestAccountDatalake, nil)
2435+
_require.NoError(err)
2436+
2437+
_, err = newClient.GetProperties(context.Background(), nil)
2438+
_require.NoError(err)
2439+
2440+
// Test with Unicode characters
2441+
unicodeFileName := "lör 006.jpg"
2442+
unicodeClient, err := testcommon.GetFileClient(filesystemName, unicodeFileName, s.T(), testcommon.TestAccountDatalake, nil)
2443+
_require.NoError(err)
2444+
2445+
resp, err = unicodeClient.Create(context.Background(), nil)
2446+
_require.NoError(err)
2447+
_require.NotNil(resp)
2448+
2449+
// Rename Unicode file to another Unicode name
2450+
newUnicodeName := "ångström ümlaut 我的文件.jpg"
2451+
_, err = unicodeClient.Rename(context.Background(), newUnicodeName, nil)
2452+
_require.NoError(err)
2453+
2454+
// Verify new file exists
2455+
newUnicodeClient, err := testcommon.GetFileClient(filesystemName, newUnicodeName, s.T(), testcommon.TestAccountDatalake, nil)
2456+
_require.NoError(err)
2457+
2458+
_, err = newUnicodeClient.GetProperties(context.Background(), nil)
2459+
_require.NoError(err)
2460+
}
2461+
2462+
func (s *RecordedTestSuite) TestRenameWithQueryParameters() {
2463+
_require := require.New(s.T())
2464+
testName := s.T().Name()
2465+
2466+
filesystemName := testcommon.GenerateFileSystemName(testName)
2467+
fsClient, err := testcommon.GetFileSystemClient(filesystemName, s.T(), testcommon.TestAccountDatalake, nil)
2468+
_require.NoError(err)
2469+
defer testcommon.DeleteFileSystem(context.Background(), _require, fsClient)
2470+
2471+
_, err = fsClient.Create(context.Background(), nil)
2472+
_require.NoError(err)
2473+
2474+
// Create a regular file
2475+
fileName := "original-file.txt"
2476+
fClient, err := testcommon.GetFileClient(filesystemName, fileName, s.T(), testcommon.TestAccountDatalake, nil)
2477+
_require.NoError(err)
2478+
2479+
resp, err := fClient.Create(context.Background(), nil)
2480+
_require.NoError(err)
2481+
_require.NotNil(resp)
2482+
2483+
// In a real scenario, SAS tokens or other parameters might be appended to the source path
2484+
// Here we're simulating this with some fake query parameters
2485+
srcPathWithQuery := fileName + "?param1=value1&param2=value with spaces"
2486+
2487+
// Create a client with the path including query parameters
2488+
// Note: This is just to test our URL encoding, in a real scenario you'd get this path from somewhere else
2489+
_, err = fClient.Rename(context.Background(), "new-file.txt", nil)
2490+
_require.NoError(err)
2491+
2492+
// Create a new file to test the rename with query parameters
2493+
newFile := "query-test-file.txt"
2494+
queryClient, err := testcommon.GetFileClient(filesystemName, newFile, s.T(), testcommon.TestAccountDatalake, nil)
2495+
_require.NoError(err)
2496+
2497+
resp, err = queryClient.Create(context.Background(), nil)
2498+
_require.NoError(err)
2499+
_require.NotNil(resp)
2500+
2501+
// Use internal implementation to test path handling with query parameters
2502+
// This is a white box test since we can't directly attach query params to the source path in normal usage
2503+
path := newFile + "?param1=value1&param2=value with spaces"
2504+
_, _, _, createOpts, _ := path.FormatRenameOptions(nil, path)
2505+
2506+
_require.NotNil(createOpts)
2507+
_require.NotNil(createOpts.RenameSource)
2508+
// Verify the source path was properly encoded
2509+
expected := "query-test-file.txt?param1=value1&param2=value+with+spaces"
2510+
_require.Equal(expected, *createOpts.RenameSource)
2511+
}
2512+
24072513
func (s *RecordedTestSuite) TestRenameFileWithCPK() {
24082514
_require := require.New(s.T())
24092515
testName := s.T().Name()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
// Copyright (c) Microsoft Corporation. All rights reserved.
5+
// Licensed under the MIT License. See License.txt in the project root for license information.
6+
7+
package path
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestFormatRenameOptions(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
path string
19+
expectedSource string
20+
}{
21+
{
22+
name: "Simple path",
23+
path: "dir1/file1.txt",
24+
expectedSource: "dir1/file1.txt",
25+
},
26+
{
27+
name: "Path with spaces",
28+
path: "dir1/file with spaces.txt",
29+
expectedSource: "dir1/file+with+spaces.txt",
30+
},
31+
{
32+
name: "Path with unicode characters",
33+
path: "dir1/lör 006.jpg",
34+
expectedSource: "dir1/l%C3%B6r+006.jpg",
35+
},
36+
{
37+
name: "Path with special characters",
38+
path: "dir1/file+name@!&%.txt",
39+
expectedSource: "dir1/file%2Bname%40%21%26%25.txt",
40+
},
41+
{
42+
name: "Path with query parameters",
43+
path: "dir1/file1.txt?param1=value1&param2=value2",
44+
expectedSource: "dir1/file1.txt?param1=value1&param2=value2",
45+
},
46+
{
47+
name: "Path with special characters and query parameters",
48+
path: "dir1/file name+special@!&%.txt?param=value with spaces",
49+
expectedSource: "dir1/file+name%2Bspecial%40%21%26%25.txt?param=value+with+spaces",
50+
},
51+
}
52+
53+
for _, test := range tests {
54+
t.Run(test.name, func(t *testing.T) {
55+
_, _, _, createOpts, _ := FormatRenameOptions(nil, test.path)
56+
assert.NotNil(t, createOpts)
57+
assert.NotNil(t, createOpts.RenameSource)
58+
assert.Equal(t, test.expectedSource, *createOpts.RenameSource)
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)