Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions internal/templater/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package templater
import (
"maps"
"math/rand/v2"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -103,8 +103,13 @@ func joinEnv(elem ...string) string {
return strings.Join(elem, string(os.PathListSeparator))
}

func joinUrl(elem ...string) string {
return path.Join(elem...)
func joinUrl(elem ...string) (string, error) {
if len(elem) == 0 {
return "", nil
}
// Use net/url.JoinPath rather than path.Join: the latter runs path.Clean,
// which collapses the "//" in a URL scheme (e.g. "http://" -> "http:/").
return url.JoinPath(elem[0], elem[1:]...)
}

func merge(base map[string]any, v ...map[string]any) map[string]any {
Expand Down
27 changes: 27 additions & 0 deletions internal/templater/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package templater

import "testing"

func TestJoinUrl(t *testing.T) {

Check failure on line 5 in internal/templater/funcs_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.25.10)

Function TestJoinUrl missing the call to method parallel (paralleltest)

Check failure on line 5 in internal/templater/funcs_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.26.x)

Function TestJoinUrl missing the call to method parallel (paralleltest)
for _, tt := range []struct {

Check failure on line 6 in internal/templater/funcs_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.25.10)

Range statement for test TestJoinUrl missing the call to method parallel in test Run (paralleltest)

Check failure on line 6 in internal/templater/funcs_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.26.x)

Range statement for test TestJoinUrl missing the call to method parallel in test Run (paralleltest)
name string
elem []string
want string
}{
// The URL scheme's "//" must be preserved, not collapsed by path.Clean.
{"scheme preserved", []string{"http://localhost", "path1", "path2"}, "http://localhost/path1/path2"},
{"https with base path", []string{"https://example.com/api", "v1", "users"}, "https://example.com/api/v1/users"},
{"trailing slash on base", []string{"http://localhost/", "path"}, "http://localhost/path"},
{"single element", []string{"http://localhost"}, "http://localhost/"},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := joinUrl(tt.elem...)
if err != nil {
t.Fatalf("joinUrl(%q) unexpected error: %v", tt.elem, err)
}
if got != tt.want {
t.Errorf("joinUrl(%q) = %q; want %q", tt.elem, got, tt.want)
}
})
}
}
Loading