|
|
@@ -5,6 +5,7 @@ package urllib // import "miniflux.app/v2/internal/urllib"
|
|
|
|
|
|
import (
|
|
|
"net"
|
|
|
+ "net/url"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
@@ -46,7 +47,8 @@ func TestIsAbsoluteURL(t *testing.T) {
|
|
|
scenarios := map[string]bool{
|
|
|
"https://example.org/file.pdf": true,
|
|
|
"magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": true,
|
|
|
- "invalid url": false,
|
|
|
+ "invalid url": false,
|
|
|
+ "/relative/path": false,
|
|
|
}
|
|
|
|
|
|
for input, expected := range scenarios {
|
|
|
@@ -58,36 +60,78 @@ func TestIsAbsoluteURL(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestAbsoluteURL(t *testing.T) {
|
|
|
- scenarios := [][]string{
|
|
|
- {"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
|
|
|
- {"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
|
|
|
- {"https://example.org/", "https://example.org/path", "./"},
|
|
|
- {"https://example.org/folder/", "https://example.org/folder/", "./"},
|
|
|
- {"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
|
|
|
- {"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
|
|
|
- {"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
|
|
|
- {"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a"},
|
|
|
- {"magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", "https://www.example.org/", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7"},
|
|
|
+ type absoluteScenario struct {
|
|
|
+ name string
|
|
|
+ base string
|
|
|
+ relative string
|
|
|
+ expected string
|
|
|
+ wantErr bool
|
|
|
+ runWithParsed bool
|
|
|
+ useNilParsed bool
|
|
|
+ }
|
|
|
+
|
|
|
+ scenarios := []absoluteScenario{
|
|
|
+ {"absolute path", "https://example.org/folder/", "/path/file.ext", "https://example.org/path/file.ext", false, true, false},
|
|
|
+ {"relative path", "https://example.org/folder/", "path/file.ext", "https://example.org/folder/path/file.ext", false, true, false},
|
|
|
+ {"dot path root", "https://example.org/path", "./", "https://example.org/", false, true, false},
|
|
|
+ {"dot path folder", "https://example.org/folder/", "./", "https://example.org/folder/", false, true, false},
|
|
|
+ {"missing slash in base", "https://example.org/folder", "path/file.ext", "https://example.org/path/file.ext", false, true, false},
|
|
|
+ {"already absolute", "https://example.org/folder/", "https://example.org/path/file.ext", "https://example.org/path/file.ext", false, true, false},
|
|
|
+ {"protocol relative", "https://www.example.org/", "//static.example.org/path/file.ext", "https://static.example.org/path/file.ext", false, true, false},
|
|
|
+ {"magnet keeps scheme", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", false, true, false},
|
|
|
+ {"magnet with query", "https://www.example.org/", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", false, true, false},
|
|
|
+ {"empty relative returns base", "https://example.org/folder/", "", "https://example.org/folder/", false, true, false},
|
|
|
+ {"invalid base errors", "://bad", "path/file.ext", "", true, false, false},
|
|
|
+ {"absolute ignores invalid base", "://bad", "https://example.org/path/file.ext", "https://example.org/path/file.ext", false, true, true},
|
|
|
}
|
|
|
|
|
|
for _, scenario := range scenarios {
|
|
|
- actual, err := AbsoluteURL(scenario[1], scenario[2])
|
|
|
+ t.Run(scenario.name, func(t *testing.T) {
|
|
|
+ actual, err := ResolveToAbsoluteURL(scenario.base, scenario.relative)
|
|
|
+ if scenario.wantErr {
|
|
|
+ if err == nil {
|
|
|
+ t.Fatalf("expected error for base %q relative %q", scenario.base, scenario.relative)
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("unexpected error for base %q relative %q: %v", scenario.base, scenario.relative, err)
|
|
|
+ }
|
|
|
+ if actual != scenario.expected {
|
|
|
+ t.Fatalf("unexpected result, got %q instead of %q for (%q, %q)", actual, scenario.expected, scenario.base, scenario.relative)
|
|
|
+ }
|
|
|
|
|
|
- if err != nil {
|
|
|
- t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
|
|
|
- }
|
|
|
+ if scenario.runWithParsed {
|
|
|
+ var parsedBase *url.URL
|
|
|
+ if !scenario.useNilParsed && scenario.base != "" {
|
|
|
+ var parseErr error
|
|
|
+ parsedBase, parseErr = url.Parse(scenario.base)
|
|
|
+ if parseErr != nil {
|
|
|
+ t.Fatalf("unable to parse base %q: %v", scenario.base, parseErr)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if actual != scenario[0] {
|
|
|
- t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
|
|
|
- }
|
|
|
+ actualParsed, errParsed := ResolveToAbsoluteURLWithParsedBaseURL(parsedBase, scenario.relative)
|
|
|
+ if errParsed != nil {
|
|
|
+ t.Fatalf("unexpected error with parsed base for (%q, %q): %v", scenario.base, scenario.relative, errParsed)
|
|
|
+ }
|
|
|
+ if actualParsed != scenario.expected {
|
|
|
+ t.Fatalf("unexpected parsed-base result, got %q instead of %q for (%q, %q)", actualParsed, scenario.expected, scenario.base, scenario.relative)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestRootURL(t *testing.T) {
|
|
|
scenarios := map[string]string{
|
|
|
- "https://example.org/path/file.ext": "https://example.org/",
|
|
|
- "//static.example.org/path/file.ext": "https://static.example.org/",
|
|
|
- "https://example|org/path/file.ext": "https://example|org/path/file.ext",
|
|
|
+ "": "",
|
|
|
+ "https://example.org/path/file.ext": "https://example.org/",
|
|
|
+ "https://example.org/path/file.ext?test=abc": "https://example.org/",
|
|
|
+ "//static.example.org/path/file.ext": "https://static.example.org/",
|
|
|
+ "https://example|org/path/file.ext": "https://example|org/path/file.ext",
|
|
|
+ "/relative/path": "/relative/path",
|
|
|
+ "http://example.org:8080/path": "http://example.org:8080/",
|
|
|
}
|
|
|
|
|
|
for input, expected := range scenarios {
|
|
|
@@ -127,6 +171,22 @@ func TestDomain(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestDomainWithoutWWW(t *testing.T) {
|
|
|
+ scenarios := map[string]string{
|
|
|
+ "https://www.example.org/": "example.org",
|
|
|
+ "https://example.org/": "example.org",
|
|
|
+ "https://www.sub.example.org/": "sub.example.org",
|
|
|
+ "https://example|org/": "https://example|org/",
|
|
|
+ }
|
|
|
+
|
|
|
+ for input, expected := range scenarios {
|
|
|
+ actual := DomainWithoutWWW(input)
|
|
|
+ if actual != expected {
|
|
|
+ t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestJoinBaseURLAndPath(t *testing.T) {
|
|
|
type args struct {
|
|
|
baseURL string
|