Skip to content

Commit

Permalink
fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaumoron committed Feb 2, 2024
1 parent aeaa49d commit 1d24566
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 1 addition & 4 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@
package download

import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
)

var ErrPrefix = errors.New("prefix does not match")

func ApplyUrlTranformer(urlTransformer func(string) (string, error), baseURLs ...string) ([]string, error) {
transformedURLs := make([]string, 0, len(baseURLs))
for _, baseURL := range baseURLs {
Expand Down Expand Up @@ -70,7 +67,7 @@ func UrlTranformer(rewriteRule []string) func(string) (string, error) {

return func(urlValue string) (string, error) {
if len(urlValue) < prevLen || urlValue[:prevLen] != prevBaseURL {
return "", ErrPrefix
return urlValue, nil
}

return url.JoinPath(baseURL, urlValue[prevLen:]) //nolint
Expand Down
12 changes: 8 additions & 4 deletions pkg/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ func TestUrlTransformerDisabled(t *testing.T) {
func TestUrlTransformerPrefix(t *testing.T) {
urlTransformer := download.UrlTranformer([]string{"https://github.com", "https://go.dev"})

if value, err := urlTransformer("https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_darwin_amd64.zip"); err == nil {
t.Error("Should fail on erroneous prefix, get :", value)
} else if err != download.ErrPrefix {
t.Error("Incorrect error reported, get :", err)
initialValue := "https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_darwin_amd64.zip"
value, err := urlTransformer(initialValue)
if err != nil {
t.Fatal("Unexpected error :", err)
}

if value != initialValue {
t.Error("Unexpected result, get :", value)
}
}

Expand Down

0 comments on commit 1d24566

Please sign in to comment.