From c97ddac6dfb278134025d0f0e6998433d2182845 Mon Sep 17 00:00:00 2001 From: Jack Henschel Date: Sun, 31 Jan 2021 07:31:56 +0200 Subject: [PATCH] test: Check filenames with special characters work correctly --- simples3_test.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/simples3_test.go b/simples3_test.go index 8d35991..f54fc22 100644 --- a/simples3_test.go +++ b/simples3_test.go @@ -23,6 +23,12 @@ func TestS3_FileUpload(t *testing.T) { return } defer testTxt.Close() + // Note: cannot re-use the same file descriptor due to seeking! + testTxtSpecialChars, err := os.Open("testdata/test.txt") + if err != nil { + return + } + defer testTxtSpecialChars.Close() testPng, err := os.Open("testdata/avatar.png") if err != nil { return @@ -76,6 +82,25 @@ func TestS3_FileUpload(t *testing.T) { }, wantErr: false, }, + { + name: "Upload special filename txt", + fields: tConfig{ + AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"), + SecretKey: os.Getenv("AWS_S3_SECRET_KEY"), + Endpoint: os.Getenv("AWS_S3_ENDPOINT"), + Region: os.Getenv("AWS_S3_REGION"), + }, + args: args{ + UploadInput{ + Bucket: os.Getenv("AWS_S3_BUCKET"), + ObjectKey: "xyz/example file%with$special&chars(1)?.txt", + ContentType: "text/plain", + FileName: "example file%with$special&chars(1)?.txt", + Body: testTxtSpecialChars, + }, + }, + wantErr: false, + }, } for _, testcase := range tests { tt := testcase @@ -159,6 +184,23 @@ func TestS3_FileDownload(t *testing.T) { wantErr: false, wantResponse: testPngData, }, + { + name: "txt-special-filename", + fields: tConfig{ + AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"), + SecretKey: os.Getenv("AWS_S3_SECRET_KEY"), + Endpoint: os.Getenv("AWS_S3_ENDPOINT"), + Region: os.Getenv("AWS_S3_REGION"), + }, + args: args{ + u: DownloadInput{ + Bucket: os.Getenv("AWS_S3_BUCKET"), + ObjectKey: "xyz/example file%with$special&chars(1)?.txt", + }, + }, + wantErr: false, + wantResponse: testTxtData, + }, } for _, testcase := range tests { @@ -227,6 +269,22 @@ func TestS3_FileDelete(t *testing.T) { }, wantErr: false, }, + { + name: "Delete special filename txt", + fields: tConfig{ + AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"), + SecretKey: os.Getenv("AWS_S3_SECRET_KEY"), + Endpoint: os.Getenv("AWS_S3_ENDPOINT"), + Region: os.Getenv("AWS_S3_REGION"), + }, + args: args{ + DeleteInput{ + Bucket: os.Getenv("AWS_S3_BUCKET"), + ObjectKey: "xyz/example file%with$special&chars(1)?.txt", + }, + }, + wantErr: false, + }, } for _, testcase := range tests { tt := testcase @@ -294,3 +352,52 @@ func TestCustomEndpoint(t *testing.T) { t.Errorf("S3.SetEndpoint() got = %v", s3.Endpoint) } } + +func TestGetURL(t *testing.T) { + s3 := New("us-east-1", "AccessKey", "SuperSecretKey") + + type args struct { + bucket string + params []string + } + + tests := []struct { + name string + args args + want string + }{ + { + name: "getURL: basic test", + args: args{ + bucket: "xyz", + }, + want: "https://s3.us-east-1.amazonaws.com/xyz", + }, + { + name: "getURL: multiple parameters", + args: args{ + bucket: "xyz", + params: []string{"hello", "world"}, + }, + want: "https://s3.us-east-1.amazonaws.com/xyz/hello/world", + }, + { + name: "getURL: special characters", + args: args{ + bucket: "xyz", + params: []string{"hello, world!", "#!@$%^&*(1).txt"}, + }, + want: "https://s3.us-east-1.amazonaws.com/xyz/hello%2C%20world%21/%23%21%40%24%25%5E%26%2A%281%29.txt", + }, + } + + for _, testcase := range tests { + tt := testcase + t.Run(tt.name, func(t *testing.T) { + url := s3.getURL(tt.args.bucket, tt.args.params...) + if url != tt.want { + t.Errorf("S3.getURL() got = %v, want %v", url, tt.want) + } + }) + } +}