Skip to content

Commit

Permalink
preserve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
dduzgun-security committed Jan 13, 2025
1 parent 8a9c471 commit 4836732
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion get_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (g *S3Getter) parseUrl(u *url.URL) (region, bucket, path, version string, c
} else {
pathParts := strings.SplitN(u.Path, "/", 3)
if len(pathParts) != 3 {
err = fmt.Errorf("URL is not a valid S3 URL")
err = fmt.Errorf("URL is not a valid S3 compliant URL")
return
}
bucket = pathParts[1]
Expand Down
36 changes: 21 additions & 15 deletions get_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,43 +278,49 @@ func TestS3Getter_Url(t *testing.T) {

func Test_S3Getter_ParseUrl_Malformed(t *testing.T) {
tests := []struct {
name string
url string
name string
input string
expected string
}{
{
name: "path style",
url: "https://s3.amazonaws.com/bucket",
name: "path style",
input: "https://s3.amazonaws.com/bucket",
expected: "URL is not a valid S3 URL",
},
{
name: "vhost-style, dash region indication",
url: "https://bucket.s3-us-east-1.amazonaws.com",
name: "vhost-style, dash region indication",
input: "https://bucket.s3-us-east-1.amazonaws.com",
expected: "URL is not a valid S3 URL",
},
{
name: "vhost-style, dot region indication",
url: "https://bucket.s3.us-east-1.amazonaws.com",
name: "vhost-style, dot region indication",
input: "https://bucket.s3.us-east-1.amazonaws.com",
expected: "URL is not a valid S3 URL",
},
{
name: "invalid host parts",
url: "https://invalid.host.parts.lenght.s3.us-east-1.amazonaws.com",
name: "invalid host parts",
input: "https://invalid.host.parts.lenght.s3.us-east-1.amazonaws.com",
expected: "URL is not a valid S3 URL",
},
{
name: "invalid host suffix",
url: "https://bucket.s3.amazonaws.com.invalid",
name: "invalid host suffix",
input: "https://bucket.s3.amazonaws.com.invalid",
expected: "URL is not a valid S3 compliant URL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := new(S3Getter)
u, err := url.Parse(tt.url)
u, err := url.Parse(tt.input)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
_, _, _, _, _, err = g.parseUrl(u)
if err == nil {
t.Fatalf("expected error, got none")
}
if err.Error() != "URL is not a valid S3 URL" {
t.Fatalf("expected error 'URL is not a valid S3 URL', got %s", err.Error())
if err.Error() != tt.expected {
t.Fatalf("expected error '%s', got %s for %s", tt.expected, err.Error(), tt.name)
}
})
}
Expand Down

0 comments on commit 4836732

Please sign in to comment.