From 608705456f7262d1aead8906207dc1151746a505 Mon Sep 17 00:00:00 2001 From: James Brookes Date: Wed, 24 Jul 2024 14:37:20 -0400 Subject: [PATCH 1/4] Add function to create archive URL --- internal/releasesjson/downloader.go | 49 +++++++++++++++++++---------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/internal/releasesjson/downloader.go b/internal/releasesjson/downloader.go index b50fea0..82251f4 100644 --- a/internal/releasesjson/downloader.go +++ b/internal/releasesjson/downloader.go @@ -14,6 +14,7 @@ import ( "net/http" "net/url" "os" + "path" "path/filepath" "runtime" "strings" @@ -64,23 +65,9 @@ func (d *Downloader) DownloadAndUnpack(ctx context.Context, pv *ProductVersion, client := httpclient.NewHTTPClient() - archiveURL := pb.URL - if d.BaseURL != "" { - // If custom URL is set, use that instead of the one from the JSON. - // Also ensures that absolute download links from mocked responses - // are still pointing to the mock server if one is set. - baseURL, err := url.Parse(d.BaseURL) - if err != nil { - return nil, err - } - - u, err := url.Parse(archiveURL) - if err != nil { - return nil, err - } - u.Scheme = baseURL.Scheme - u.Host = baseURL.Host - archiveURL = u.String() + archiveURL, err := determineArchiveURL(pb.URL, d.BaseURL) + if err != nil { + return nil, err } d.Logger.Printf("downloading archive from %s", archiveURL) @@ -237,3 +224,31 @@ func isLicenseFile(filename string) bool { } return false } + +// determineArchiveURL determines the archive URL based on the base URL provided. +func determineArchiveURL(archiveURL, baseURL string) (string, error) { + // If custom URL is set, use that instead of the one from the JSON. + // Also ensures that absolute download links from mocked responses + // are still pointing to the mock server if one is set. + if baseURL == "" { + return archiveURL, nil + } + + base, err := url.Parse(baseURL) + if err != nil { + return "", err + } + + u, err := url.Parse(archiveURL) + if err != nil { + return "", err + } + + // Preserve the path from the baseURL and append the path from the archiveURL. + newPath := path.Join(base.Path, strings.TrimPrefix(u.Path, "/")) + u.Scheme = base.Scheme + u.Host = base.Host + u.Path = newPath + + return u.String(), nil +} From 125031eb6cba8201e93155d1829d02873d3b4152 Mon Sep 17 00:00:00 2001 From: James Brookes Date: Wed, 24 Jul 2024 14:38:01 -0400 Subject: [PATCH 2/4] Create downloader_test.go --- internal/releasesjson/downloader_test.go | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 internal/releasesjson/downloader_test.go diff --git a/internal/releasesjson/downloader_test.go b/internal/releasesjson/downloader_test.go new file mode 100644 index 0000000..a4de561 --- /dev/null +++ b/internal/releasesjson/downloader_test.go @@ -0,0 +1,44 @@ +package releasesjson + +import "testing" + +func TestDetermineArchiveURL(t *testing.T) { + tests := []struct { + name string + archiveURL string + baseURL string + want string + }{ + { + name: "with custom base URL + path", + archiveURL: "https://releases.hashicorp.com/terraform/1.8.2/terraform_1.8.2_darwin_amd64.zip", + baseURL: "https://myartifactory.company.com/artifactory/hashicorp-remote", + want: "https://myartifactory.company.com/artifactory/hashicorp-remote/terraform/1.8.2/terraform_1.8.2_darwin_amd64.zip", + }, + { + name: "with custom base URL + port + path", + archiveURL: "https://releases.hashicorp.com/terraform/1.8.2/terraform_1.8.2_darwin_amd64.zip", + baseURL: "https://myartifactory.company.com:443/artifactory/hashicorp-remote", + want: "https://myartifactory.company.com:443/artifactory/hashicorp-remote/terraform/1.8.2/terraform_1.8.2_darwin_amd64.zip", + }, + { + name: "without custom base URL", + archiveURL: "https://releases.hashicorp.com/terraform/1.8.2/terraform_1.8.2_darwin_amd64.zip", + baseURL: "", + want: "https://releases.hashicorp.com/terraform/1.8.2/terraform_1.8.2_darwin_amd64.zip", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := determineArchiveURL(tt.archiveURL, tt.baseURL) + if err != nil { + t.Errorf("determineArchiveURL() error = %v", err) + return + } + if got != tt.want { + t.Errorf("determineArchiveURL() = %v, want %v", got, tt.want) + } + }) + } +} From 084160ad4031e5b9246e3fccbe06ae4eb935f67b Mon Sep 17 00:00:00 2001 From: James Brookes Date: Wed, 24 Jul 2024 14:45:35 -0400 Subject: [PATCH 3/4] Add missing Copywrite header --- internal/releasesjson/downloader_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/releasesjson/downloader_test.go b/internal/releasesjson/downloader_test.go index a4de561..043caf4 100644 --- a/internal/releasesjson/downloader_test.go +++ b/internal/releasesjson/downloader_test.go @@ -1,3 +1,6 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + package releasesjson import "testing" From 32863fd33bbafc77fc53112f239c034669df7906 Mon Sep 17 00:00:00 2001 From: James Brookes Date: Wed, 24 Jul 2024 15:09:21 -0400 Subject: [PATCH 4/4] Use `JoinPath()` to simplify logic --- internal/releasesjson/downloader.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/releasesjson/downloader.go b/internal/releasesjson/downloader.go index 82251f4..2f937f1 100644 --- a/internal/releasesjson/downloader.go +++ b/internal/releasesjson/downloader.go @@ -14,7 +14,6 @@ import ( "net/http" "net/url" "os" - "path" "path/filepath" "runtime" "strings" @@ -244,11 +243,8 @@ func determineArchiveURL(archiveURL, baseURL string) (string, error) { return "", err } - // Preserve the path from the baseURL and append the path from the archiveURL. - newPath := path.Join(base.Path, strings.TrimPrefix(u.Path, "/")) - u.Scheme = base.Scheme - u.Host = base.Host - u.Path = newPath + // Use base URL path and append the path from the archive URL. + newArchiveURL := base.JoinPath(u.Path) - return u.String(), nil + return newArchiveURL.String(), nil }