diff --git a/detect_gcs.go b/detect_gcs.go index 2bf1bcf2..bdf7f344 100644 --- a/detect_gcs.go +++ b/detect_gcs.go @@ -29,7 +29,9 @@ func (d *GCSDetector) Detect(src, _ string) (string, bool, error) { } if strings.HasSuffix(parsedURL.Host, ".googleapis.com") { - return d.detectHTTP(strings.ReplaceAll(src, "https://", "")) + src = strings.TrimPrefix(src, "https://") + src = strings.TrimPrefix(src, "http://") + return d.detectHTTP(src) } return "", false, nil diff --git a/detect_gcs_test.go b/detect_gcs_test.go index e5022ca4..175f3fa6 100644 --- a/detect_gcs_test.go +++ b/detect_gcs_test.go @@ -60,6 +60,18 @@ func TestGCSDetector_MalformedDetectHTTP(t *testing.T) { "", "gcs::https://www.googleapis.com/storage/v1/my-bucket/foo/bar", }, + { + "valid url with https scheme", + "https://www.googleapis.com/storage/v1/my-bucket/foo/bar", + "", + "gcs::https://www.googleapis.com/storage/v1/my-bucket/foo/bar", + }, + { + "valid url with http scheme", + "http://www.googleapis.com/storage/v1/my-bucket/foo/bar", + "", + "gcs::https://www.googleapis.com/storage/v1/my-bucket/foo/bar", + }, { "empty url", "", @@ -67,14 +79,20 @@ func TestGCSDetector_MalformedDetectHTTP(t *testing.T) { "", }, { - "not valid url length", + "not valid url", + "storage/v1/my-bucket/foo/bar", + "error parsing GCS URL", + "", + }, + { + "not valid url domain", "www.googleapis.com.invalid/storage/v1/", "URL is not a valid GCS URL", "", }, { "not valid url length", - "www.invalid.com/storage/v1", + "http://www.googleapis.com/storage", "URL is not a valid GCS URL", "", }, diff --git a/detect_s3.go b/detect_s3.go index e0d69829..698d7770 100644 --- a/detect_s3.go +++ b/detect_s3.go @@ -29,7 +29,9 @@ func (d *S3Detector) Detect(src, _ string) (string, bool, error) { } if strings.HasSuffix(parsedURL.Host, ".amazonaws.com") { - return d.detectHTTP(strings.ReplaceAll(src, "https://", "")) + src = strings.TrimPrefix(src, "https://") + src = strings.TrimPrefix(src, "http://") + return d.detectHTTP(src) } return "", false, nil diff --git a/detect_s3_test.go b/detect_s3_test.go index 883c7c8b..f892b01a 100644 --- a/detect_s3_test.go +++ b/detect_s3_test.go @@ -90,3 +90,70 @@ func TestS3Detector(t *testing.T) { } } } + +func TestS3Detector_MalformedDetectHTTP(t *testing.T) { + cases := []struct { + Name string + Input string + Expected string + Output string + }{ + { + "valid url", + "s3.amazonaws.com/bucket/foo/bar", + "", + "s3::https://s3.amazonaws.com/bucket/foo/bar", + }, + { + "valid url with https scheme", + "https://s3.amazonaws.com/bucket/foo/bar", + "", + "s3::https://s3.amazonaws.com/bucket/foo/bar", + }, + { + "valid url with http scheme", + "http://s3.amazonaws.com/bucket/foo/bar", + "", + "s3::https://s3.amazonaws.com/bucket/foo/bar", + }, + { + "empty url", + "", + "", + "", + }, + { + "not valid url", + "bucket/foo/bar", + "error parsing S3 URL", + "", + }, + { + "not valid url domain", + "s3.amazonaws.com.invalid/bucket/foo/bar", + "error parsing S3 URL", + "", + }, + { + "not valid url lenght", + "http://s3.amazonaws.com", + "URL is not a valid S3 URL", + "", + }, + } + + pwd := "/pwd" + f := new(S3Detector) + for _, tc := range cases { + output, _, err := f.Detect(tc.Input, pwd) + if err != nil { + if err.Error() != tc.Expected { + t.Fatalf("expected error %s, got %s for %s", tc.Expected, err.Error(), tc.Name) + } + } + + if output != tc.Output { + t.Fatalf("expected %s, got %s for %s", tc.Output, output, tc.Name) + } + } +}