Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NotFound handler (#18062) #18067

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions integrations/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func TestRedirectsNoLogin(t *testing.T) {
resp := MakeRequest(t, req, http.StatusFound)
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
}

var temporaryRedirects = map[string]string{
"/user2/repo1/": "/user2/repo1",
}
for link, redirectLink := range temporaryRedirects {
req := NewRequest(t, "GET", link)
resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
}

}

func TestNoLoginNotExist(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion integrations/signout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestSignOut(t *testing.T) {
session.MakeRequest(t, req, http.StatusFound)

// try to view a private repo, should fail
req = NewRequest(t, "GET", "/user2/repo2/")
req = NewRequest(t, "GET", "/user2/repo2")
session.MakeRequest(t, req, http.StatusNotFound)

// invalidate cached cookies for user2, for subsequent tests
Expand Down
10 changes: 10 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,4 +1036,14 @@ func RegisterRoutes(m *web.Route) {
if setting.API.EnableSwagger {
m.Get("/swagger.v1.json", SwaggerV1Json)
}
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
escapedPath := req.URL.EscapedPath()
if len(escapedPath) > 1 && escapedPath[len(escapedPath)-1] == '/' {
http.Redirect(w, req, setting.AppSubURL+escapedPath[:len(escapedPath)-1], http.StatusTemporaryRedirect)
return
}
ctx := context.GetContext(req)
ctx.NotFound("", nil)
})

}