Fix resolution of relative avatar urls for Gogs published in subpath

Closes #1701
Includes a new test, and updates expected results for 3 of them (adding
the schema part)
This commit is contained in:
Sandro Santilli 2016-07-06 15:58:31 +02:00
parent 2d7c7bd860
commit cacfd0c444
2 changed files with 23 additions and 11 deletions

View file

@ -120,15 +120,23 @@ func fixMalformedAvatar(url string) string {
}
// expandAvatar is a helper function that converts a relative avatar URL to the
// abosolute url.
// absolute url.
func expandAvatar(repo, rawurl string) string {
if !strings.HasPrefix(rawurl, "/avatars/") {
return rawurl
}
url, err := url.Parse(repo)
aurl, err := url.Parse(rawurl)
if err != nil {
return rawurl
}
url.Path = rawurl
return url.String()
if aurl.IsAbs() {
// Url is already absolute
return aurl.String()
}
// Resolve to base
burl, err := url.Parse(repo)
if err != nil {
return rawurl
}
aurl = burl.ResolveReference(aurl)
return aurl.String()
}

View file

@ -47,7 +47,7 @@ func Test_parse(t *testing.T) {
g.Assert(build.Link).Equal(hook.Compare)
g.Assert(build.Branch).Equal("master")
g.Assert(build.Message).Equal(hook.Commits[0].Message)
g.Assert(build.Avatar).Equal("//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(build.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(build.Author).Equal(hook.Sender.Login)
})
@ -92,7 +92,7 @@ func Test_parse(t *testing.T) {
FullName: "gophers/hello-world",
Owner: gogs.User{
UserName: "gordon",
AvatarUrl: "//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
AvatarUrl: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
CloneUrl: "http://gogs.golang.org/gophers/hello-world.git",
HtmlUrl: "http://gogs.golang.org/gophers/hello-world",
@ -114,7 +114,7 @@ func Test_parse(t *testing.T) {
FullName: "gophers/hello-world",
Owner: gogs.User{
UserName: "gordon",
AvatarUrl: "//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
AvatarUrl: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
}
repo := toRepoLite(&from)
@ -165,7 +165,11 @@ func Test_parse(t *testing.T) {
},
{
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
"http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
{
"/gogs/avatars/2",
"http://gogs.io/gogs/avatars/2",
},
}