Skip to content

Commit

Permalink
Add tests showing that relative paths are handled properly.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 242949234
  • Loading branch information
alin04 authored and twifkak committed Apr 26, 2019
1 parent fd73301 commit 6356b02
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion transformer/internal/amphtml/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func isProtocolRelative(urlParam string) bool {

// ToAbsoluteURL absolute-ifies |urlParam|, using |baseURL| as the base if
// |urlParam| is relative. If |urlParam| contains a fragment, this method
// will return only a fragment if it's absolute URL matches |documentURL|,
// will return only a fragment if its absolute URL matches |documentURL|,
// which prevents changing an in-document navigation to a out-of-document
// navigation.
func ToAbsoluteURL(documentURL string, baseURL *url.URL,
Expand Down
13 changes: 13 additions & 0 deletions transformer/internal/amphtml/urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ func TestToAbsoluteURL(t *testing.T) {
documentURL: rootURL,
expected: "https://foo.com?q",
},
{
desc: "relative",
input: "../blah.jpg",
baseURL: fooURL + "/",
documentURL: rootURL,
expected: "https://www.example.com/blah.jpg",
},
}
for _, tc := range tcs {
baseURL, _ := url.Parse(tc.baseURL)
Expand Down Expand Up @@ -274,6 +281,12 @@ func TestGetCacheURL(t *testing.T) {
expectedImage: "https://www-example-com.cdn.ampproject.org/i/s/www.example.com/cdn.ampproject.org/blah.jpg",
expectedOther: "https://www-example-com.cdn.ampproject.org/r/s/www.example.com/cdn.ampproject.org/blah.jpg",
},
{
desc: "absolute with relative path",
input: "https://www.example.com/cdn.ampproject.org/one/two/three/../../../blah.jpg",
expectedImage: "https://www-example-com.cdn.ampproject.org/i/s/www.example.com/cdn.ampproject.org/blah.jpg",
expectedOther: "https://www-example-com.cdn.ampproject.org/r/s/www.example.com/cdn.ampproject.org/blah.jpg",
},
}
base, _ := url.Parse("https://example.com/")
for _, tc := range tcs {
Expand Down

0 comments on commit 6356b02

Please sign in to comment.