From caeb1a4acb0d35c78988fcd4e30d9ac51847f13a Mon Sep 17 00:00:00 2001 From: jklippel Date: Tue, 22 Nov 2022 21:26:10 +0000 Subject: [PATCH] Return a 404 if there is no repository (#141) If no repository is found the user expects a 404 status code instead of a dependency failed status code (as it was before). Signed-off-by: Jan Klippel Fixes: https://codeberg.org/Codeberg/Community/issues/809 Co-authored-by: Jan Klippel Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/141 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: jklippel Co-committed-by: jklippel --- server/handler/handler_sub_domain.go | 4 ++-- server/handler/handler_test.go | 35 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/server/handler/handler_sub_domain.go b/server/handler/handler_sub_domain.go index 1d769d4..2a75e9f 100644 --- a/server/handler/handler_sub_domain.go +++ b/server/handler/handler_sub_domain.go @@ -115,6 +115,6 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite // Couldn't find a valid repo/branch html.ReturnErrorPage(ctx, - fmt.Sprintf("couldn't find a valid repo[%s]", targetRepo), - http.StatusFailedDependency) + fmt.Sprintf("could not find a valid repository[%s]", targetRepo), + http.StatusNotFound) } diff --git a/server/handler/handler_test.go b/server/handler/handler_test.go index f5538c9..626564a 100644 --- a/server/handler/handler_test.go +++ b/server/handler/handler_test.go @@ -23,26 +23,27 @@ func TestHandlerPerformance(t *testing.T) { ) testCase := func(uri string, status int) { - req := httptest.NewRequest("GET", uri, nil) - w := httptest.NewRecorder() + t.Run(uri, func(t *testing.T) { + req := httptest.NewRequest("GET", uri, nil) + w := httptest.NewRecorder() - log.Printf("Start: %v\n", time.Now()) - start := time.Now() - testHandler(w, req) - end := time.Now() - log.Printf("Done: %v\n", time.Now()) + log.Printf("Start: %v\n", time.Now()) + start := time.Now() + testHandler(w, req) + end := time.Now() + log.Printf("Done: %v\n", time.Now()) - resp := w.Result() + resp := w.Result() - if resp.StatusCode != status { - t.Errorf("request failed with status code %d", resp.StatusCode) - } else { - t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds()) - } + if resp.StatusCode != status { + t.Errorf("request failed with status code %d", resp.StatusCode) + } else { + t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds()) + } + }) } - testCase("https://mondstern.codeberg.page/", 424) // TODO: expect 200 - testCase("https://mondstern.codeberg.page/", 424) // TODO: expect 200 - testCase("https://example.momar.xyz/", 424) // TODO: expect 200 - testCase("https://codeberg.page/", 424) // TODO: expect 200 + testCase("https://mondstern.codeberg.page/", 404) // TODO: expect 200 + testCase("https://codeberg.page/", 404) // TODO: expect 200 + testCase("https://example.momar.xyz/", 424) }