62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
|
|
package utils
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 測試 URLJoin 函數
|
||
|
|
func TestURLJoin(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
baseURL string
|
||
|
|
paths []string
|
||
|
|
expected string
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "Base URL without trailing slash and single path",
|
||
|
|
baseURL: "https://example.com",
|
||
|
|
paths: []string{"path1"},
|
||
|
|
expected: "https://example.com/path1",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Base URL with trailing slash and single path",
|
||
|
|
baseURL: "https://example.com/",
|
||
|
|
paths: []string{"path1"},
|
||
|
|
expected: "https://example.com/path1",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Base URL without trailing slash and multiple paths",
|
||
|
|
baseURL: "https://example.com",
|
||
|
|
paths: []string{"path1", "path2", "path3"},
|
||
|
|
expected: "https://example.com/path1/path2/path3",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Base URL with trailing slash and multiple paths",
|
||
|
|
baseURL: "https://example.com/",
|
||
|
|
paths: []string{"path1", "path2", "path3"},
|
||
|
|
expected: "https://example.com/path1/path2/path3",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Empty path elements",
|
||
|
|
baseURL: "https://example.com",
|
||
|
|
paths: []string{},
|
||
|
|
expected: "https://example.com",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Path elements with leading slashes",
|
||
|
|
baseURL: "https://example.com",
|
||
|
|
paths: []string{"/path1/", "/path2", "path3"},
|
||
|
|
expected: "https://example.com/path1/path2/path3",
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := URLJoin(tt.baseURL, tt.paths...)
|
||
|
|
assert.Equal(t, tt.expected, result, "Expected URL to match")
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|