mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-03 08:46:57 +00:00
161 lines
4.3 KiB
Go
161 lines
4.3 KiB
Go
package readme
|
|
|
|
import (
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetBaseURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
inputURL string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "nil URL",
|
|
inputURL: "",
|
|
expected: "",
|
|
},
|
|
{
|
|
name: "GitHub raw URL",
|
|
inputURL: "https://raw.githubusercontent.com/user/repo/main/README.md",
|
|
expected: "https://github.com/user/repo/blob/main/",
|
|
},
|
|
{
|
|
name: "GitHub raw URL with subdirectory",
|
|
inputURL: "https://raw.githubusercontent.com/user/repo/main/docs/README.md",
|
|
expected: "https://github.com/user/repo/blob/main/",
|
|
},
|
|
{
|
|
name: "GitHub raw URL with branch",
|
|
inputURL: "https://raw.githubusercontent.com/user/repo/develop/README.md",
|
|
expected: "https://github.com/user/repo/blob/develop/",
|
|
},
|
|
{
|
|
name: "regular URL",
|
|
inputURL: "https://example.com/docs/README.md",
|
|
expected: "https://example.com/docs/",
|
|
},
|
|
{
|
|
name: "URL with multiple path segments",
|
|
inputURL: "https://example.com/path/to/docs/README.md",
|
|
expected: "https://example.com/path/to/docs/",
|
|
},
|
|
{
|
|
name: "URL with root file",
|
|
inputURL: "https://example.com/README.md",
|
|
expected: "https://example.com/",
|
|
},
|
|
{
|
|
name: "URL without file",
|
|
inputURL: "https://example.com/docs/",
|
|
expected: "https://example.com/docs/",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var u *url.URL
|
|
if tt.inputURL != "" {
|
|
var err error
|
|
u, err = url.Parse(tt.inputURL)
|
|
if err != nil {
|
|
t.Fatalf("Failed to parse URL %q: %v", tt.inputURL, err)
|
|
}
|
|
}
|
|
|
|
result := getBaseURL(u)
|
|
if result != tt.expected {
|
|
t.Errorf("getBaseURL(%q) = %q, want %q", tt.inputURL, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRewriteRelativeURLs(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
html string
|
|
baseURL string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "empty baseURL",
|
|
html: `<img src="./image.png">`,
|
|
baseURL: "",
|
|
expected: `<img src="./image.png">`,
|
|
},
|
|
{
|
|
name: "invalid baseURL",
|
|
html: `<img src="./image.png">`,
|
|
baseURL: "://invalid",
|
|
expected: `<img src="./image.png">`,
|
|
},
|
|
{
|
|
name: "current directory relative src",
|
|
html: `<img src="./image.png">`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<img src="https://example.com/docs/image.png">`,
|
|
},
|
|
{
|
|
name: "current directory relative href",
|
|
html: `<a href="./page.html">link</a>`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<a href="https://example.com/docs/page.html">link</a>`,
|
|
},
|
|
{
|
|
name: "parent directory relative src",
|
|
html: `<img src="../image.png">`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<img src="https://example.com/docs/../image.png">`,
|
|
},
|
|
{
|
|
name: "parent directory relative href",
|
|
html: `<a href="../page.html">link</a>`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<a href="https://example.com/docs/../page.html">link</a>`,
|
|
},
|
|
{
|
|
name: "root-relative src",
|
|
html: `<img src="/images/logo.png">`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<img src="https://example.com/images/logo.png">`,
|
|
},
|
|
{
|
|
name: "root-relative href",
|
|
html: `<a href="/about">link</a>`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<a href="https://example.com/about">link</a>`,
|
|
},
|
|
{
|
|
name: "mixed relative URLs",
|
|
html: `<img src="./img.png"><a href="../page.html">link</a>`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<img src="https://example.com/docs/img.png"><a href="https://example.com/docs/../page.html">link</a>`,
|
|
},
|
|
{
|
|
name: "absolute URLs unchanged",
|
|
html: `<img src="https://cdn.example.com/image.png">`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<img src="https://cdn.example.com/image.png">`,
|
|
},
|
|
{
|
|
name: "protocol-relative URLs (incorrectly converted)",
|
|
html: `<img src="//cdn.example.com/image.png">`,
|
|
baseURL: "https://example.com/docs/",
|
|
expected: `<img src="https://example.com//cdn.example.com/image.png">`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := rewriteRelativeURLs(tt.html, tt.baseURL)
|
|
if result != tt.expected {
|
|
t.Errorf("rewriteRelativeURLs() = %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TODO: Add README fetching and caching tests
|