diff --git a/pkg/util/url/url.go b/pkg/util/url/url.go index 9518d489..fb1cb304 100644 --- a/pkg/util/url/url.go +++ b/pkg/util/url/url.go @@ -11,11 +11,11 @@ import ( "github.com/readium/go-toolkit/pkg/internal/extensions" ) -/** - * A Uniform Resource Locator. - * - * https://url.spec.whatwg.org/ - */ +/* +A Uniform Resource Locator. + +https://url.spec.whatwg.org/ +*/ type URL interface { Path() string // Decoded path segments identifying a location. Filename() string // Decoded filename portion of the URL path. @@ -364,14 +364,14 @@ func AbsoluteURLFromGo(url gurl.URL) (AbsoluteURL, error) { return AbsoluteURL{url: url, scheme: scheme}, nil } -/** - * According to the EPUB specification, the HREFs in the EPUB package must be valid URLs (so - * percent-encoded). Unfortunately, many EPUBs don't follow this rule, and use invalid HREFs such - * as `my chapter.html` or `/dir/my chapter.html`. - * - * As a workaround, we assume the HREFs are valid percent-encoded URLs, and fallback to decoded paths - * if we can't parse the URL. - */ +/* +According to the EPUB specification, the HREFs in the EPUB package must be valid URLs (so +percent-encoded). Unfortunately, many EPUBs don't follow this rule, and use invalid HREFs such +as `my chapter.html` or `/dir/my chapter.html`. + +As a workaround, we assume the HREFs are valid percent-encoded URLs, and fallback to decoded paths +if we can't parse the URL. +*/ func FromEPUBHref(href string) (URL, error) { u, err := URLFromString(href) if err != nil { diff --git a/pkg/util/url/url_test.go b/pkg/util/url/url_test.go index d55c889e..bd8be2a1 100644 --- a/pkg/util/url/url_test.go +++ b/pkg/util/url/url_test.go @@ -8,31 +8,16 @@ import ( "github.com/stretchr/testify/assert" ) -/*func TestCreateFromInvalidURL(t *testing.T) { +func TestCreateFromInvalidURL(t *testing.T) { urlTests := []string{ - "", - " ", - "invalid character", - "école", + "f:///////f", + ":C", } for _, urlTest := range urlTests { _, err := URLFromString(urlTest) assert.Error(t, err, "Expected error parsing URL '%s'", urlTest) } - - specificUrlTests := []string{ - " ", - "invalid character", - "école", - } - for _, urlTest := range specificUrlTests { - _, err := AbsoluteURLFromString(urlTest) - assert.Error(t, err, "Expected error parsing URL '%s' as absolute URL", urlTest) - - _, err = RelativeURLFromString(urlTest) - assert.Error(t, err, "Expected error parsing URL '%s' as relative URL", urlTest) - } -}*/ +} func TestCreateFromRelativePath(t *testing.T) { for _, urlTest := range []string{ @@ -436,9 +421,9 @@ func TestNormalize(t *testing.T) { assert.Equal(t, "http://example.com/foo", u.Normalize().String()) // Percent encoding of path is normalized. - u, _ = URLFromString("HTTP://example.com/c%27est%20valide") + u, _ = URLFromString("HTTP://example.com/c'est%20valide") assert.Equal(t, "http://example.com/c'est%20valide", u.Normalize().String()) - u, _ = URLFromString("c%27est%20valide") + u, _ = URLFromString("c'est%20valide") assert.Equal(t, "c'est%20valide", u.Normalize().String()) // Relative paths are resolved.