1.0 quality polish — code review, tests, bug fixes, documentation #32
@ -27,8 +27,12 @@ func (b BaseURL) JoinPath(path RelFilePath) (FileURL, error) {
|
|||||||
base.Path += "/"
|
base.Path += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and encode the relative path
|
// Encode each path segment individually to preserve slashes
|
||||||
ref, err := url.Parse(url.PathEscape(string(path)))
|
segments := strings.Split(string(path), "/")
|
||||||
|
for i, seg := range segments {
|
||||||
|
segments[i] = url.PathEscape(seg)
|
||||||
|
}
|
||||||
|
ref, err := url.Parse(strings.Join(segments, "/"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
44
mfer/url_test.go
Normal file
44
mfer/url_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package mfer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBaseURLJoinPath(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
base BaseURL
|
||||||
|
path RelFilePath
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"https://example.com/dir/", "file.txt", "https://example.com/dir/file.txt"},
|
||||||
|
{"https://example.com/dir", "file.txt", "https://example.com/dir/file.txt"},
|
||||||
|
{"https://example.com/", "sub/file.txt", "https://example.com/sub/file.txt"},
|
||||||
|
{"https://example.com/dir/", "file with spaces.txt", "https://example.com/dir/file%20with%20spaces.txt"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(string(tt.base)+"+"+string(tt.path), func(t *testing.T) {
|
||||||
|
result, err := tt.base.JoinPath(tt.path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, string(result))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBaseURLString(t *testing.T) {
|
||||||
|
b := BaseURL("https://example.com/")
|
||||||
|
assert.Equal(t, "https://example.com/", b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileURLString(t *testing.T) {
|
||||||
|
f := FileURL("https://example.com/file.txt")
|
||||||
|
assert.Equal(t, "https://example.com/file.txt", f.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManifestURLString(t *testing.T) {
|
||||||
|
m := ManifestURL("https://example.com/index.mf")
|
||||||
|
assert.Equal(t, "https://example.com/index.mf", m.String())
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user