gddo-server: handle trailing slash in godoc.org redirect
Fixes golang/go#43979
Change-Id: I3d02cdbfbf9ad7e76ae143b2d78aa9a7dde2097d
Reviewed-on: https://go-review.googlesource.com/c/gddo/+/287794
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/gddo-server/pkgsite.go b/gddo-server/pkgsite.go
index bf683a0..9ac53a3 100644
--- a/gddo-server/pkgsite.go
+++ b/gddo-server/pkgsite.go
@@ -358,6 +358,7 @@
q.Set("q", "golang.org/x")
default:
{
+ godocURL.Path = strings.TrimSuffix(godocURL.Path, "/")
// If the import path is invalid, redirect to
// https://golang.org/issue/43036, so that the users has more context
// on why this path does not work on pkg.go.dev.
diff --git a/gddo-server/pkgsite_test.go b/gddo-server/pkgsite_test.go
index 1acfee8..061a6cd 100644
--- a/gddo-server/pkgsite_test.go
+++ b/gddo-server/pkgsite_test.go
@@ -171,18 +171,23 @@
from: "https://godoc.org/cryptoscope.co/go/specialκ",
to: "https://golang.org/issue/43036",
},
+ {
+ from: "https://godoc.org/github.com/google/go-containerregistry/",
+ to: "https://pkg.go.dev/github.com/google/go-containerregistry?utm_source=godoc",
+ },
}
for _, tc := range testCases {
- u, err := url.Parse(tc.from)
- if err != nil {
- t.Errorf("url.Parse(%q): %v", tc.from, err)
- continue
- }
- to := pkgGoDevURL(u)
- if got, want := to.String(), tc.to; got != want {
- t.Errorf("pkgGoDevURL(%q) = %q; want %q", u, got, want)
- }
+ t.Run(strings.ReplaceAll(tc.from, "/", " "), func(t *testing.T) {
+ u, err := url.Parse(tc.from)
+ if err != nil {
+ t.Fatalf("url.Parse(%q): %v", tc.from, err)
+ }
+ to := pkgGoDevURL(u)
+ if got, want := to.String(), tc.to; got != want {
+ t.Errorf("pkgGoDevURL(%q) = %q; want %q", u, got, want)
+ }
+ })
}
}